View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *  http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * File implementation of {@link DocumentSource}.
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  }