Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
TextFileExtractor.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2018-2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.textextractors;
20 
21 import java.io.BufferedInputStream;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.io.Reader;
25 import java.nio.charset.Charset;
26 import java.nio.charset.StandardCharsets;
27 import java.util.logging.Level;
30 import org.sleuthkit.datamodel.AbstractFile;
31 import org.sleuthkit.datamodel.ReadContentInputStream;
32 import org.sleuthkit.datamodel.TskCoreException;
33 
37 public final class TextFileExtractor implements TextExtractor {
38 
39  private static final Logger logger = Logger.getLogger(TextFileExtractor.class.getName());
40  private final AbstractFile file;
41 
42  private Charset encoding = null;
43 
49  public TextFileExtractor(AbstractFile file) {
50  this.file = file;
51  }
52 
53  @Override
54  public Reader getReader() throws InitReaderException {
55  if(encoding == null) {
56  try {
57  encoding = EncodingUtils.getEncoding(file);
58  if(encoding == EncodingUtils.UNKNOWN_CHARSET) {
59  encoding = StandardCharsets.UTF_8;
60  }
61  } catch (TskCoreException | IOException ex) {
62  logger.log(Level.SEVERE, String.format("Error detecting the "
63  + "encoding for %s (objID=%d)", file.getName(), file.getId()), ex);
64  encoding = StandardCharsets.UTF_8;
65  }
66  }
67 
68  return getReader(encoding);
69  }
70 
71  private Reader getReader(Charset encoding) {
72  return new InputStreamReader(new BufferedInputStream(new ReadContentInputStream(file)), encoding);
73  }
74 
75  @Override
76  public boolean isSupported() {
77  return file.getMIMEType().equals("text/plain");
78  }
79 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.