1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.any23.source; |
19 | |
|
20 | |
import java.io.BufferedInputStream; |
21 | |
import java.io.ByteArrayOutputStream; |
22 | |
import java.io.File; |
23 | |
import java.io.FileInputStream; |
24 | |
import java.io.IOException; |
25 | |
import java.io.InputStream; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class FileDocumentSource implements DocumentSource { |
31 | |
|
32 | |
private final File file; |
33 | |
|
34 | |
private final String uri; |
35 | |
|
36 | 0 | public FileDocumentSource(File file) { |
37 | 0 | this.file = file; |
38 | 0 | this.uri = file.toURI().toString(); |
39 | 0 | } |
40 | |
|
41 | 0 | public FileDocumentSource(File file, String baseURI) { |
42 | 0 | this.file = file; |
43 | 0 | this.uri = baseURI; |
44 | 0 | } |
45 | |
|
46 | |
public InputStream openInputStream() throws IOException { |
47 | 0 | return new BufferedInputStream( new FileInputStream(file) ); |
48 | |
} |
49 | |
|
50 | |
public long getContentLength() { |
51 | 0 | return file.length(); |
52 | |
} |
53 | |
|
54 | |
public String getDocumentURI() { |
55 | 0 | return uri; |
56 | |
} |
57 | |
|
58 | |
public String getContentType() { |
59 | 0 | return null; |
60 | |
} |
61 | |
|
62 | |
public boolean isLocal() { |
63 | 0 | return true; |
64 | |
} |
65 | |
|
66 | |
public String readStream() throws IOException { |
67 | 0 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
68 | 0 | InputStream is = openInputStream(); |
69 | |
try { |
70 | |
int c; |
71 | 0 | while ((c = is.read()) != -1) { |
72 | 0 | baos.write(c); |
73 | |
} |
74 | |
} finally { |
75 | 0 | is.close(); |
76 | 0 | } |
77 | 0 | return new String( baos.toByteArray() ); |
78 | |
} |
79 | |
} |