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 import java.nio.charset.StandardCharsets;
27
28
29
30
31 public class FileDocumentSource implements DocumentSource {
32
33 private final File file;
34
35 private final String uri;
36
37 public FileDocumentSource(File file) {
38 this.file = file;
39 this.uri = file.toURI().toString();
40 }
41
42 public FileDocumentSource(File file, String baseIRI) {
43 this.file = file;
44 this.uri = baseIRI;
45 }
46
47 public InputStream openInputStream() throws IOException {
48 return new BufferedInputStream(new FileInputStream(file));
49 }
50
51 public long getContentLength() {
52 return file.length();
53 }
54
55 public String getDocumentIRI() {
56 return uri;
57 }
58
59 public String getContentType() {
60 return null;
61 }
62
63 public boolean isLocal() {
64 return true;
65 }
66
67 public String readStream() throws IOException {
68 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
69 InputStream is = openInputStream();
70 try {
71 int c;
72 while ((c = is.read()) != -1) {
73 baos.write(c);
74 }
75 } finally {
76 is.close();
77 }
78 return new String(baos.toByteArray(), StandardCharsets.UTF_8);
79 }
80 }