Autopsy  4.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
AbstractFileStringIntStream.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2012 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.keywordsearch;
20 
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.charset.Charset;
24 import java.util.List;
29 import org.sleuthkit.datamodel.AbstractFile;
30 import org.sleuthkit.datamodel.TskCoreException;
31 
38 class AbstractFileStringIntStream extends InputStream {
39 
40  private static final Logger logger = Logger.getLogger(AbstractFileStringIntStream.class.getName());
41  private static final int FILE_BUF_SIZE = 1024 * 1024;
42  private AbstractFile content;
43  private final byte[] oneCharBuf = new byte[1];
44  private final StringExtract stringExtractor;
45  private final byte[] fileReadBuff = new byte[FILE_BUF_SIZE];
46  private long fileReadOffset = 0L;
47  private byte[] convertBuff; //stores extracted string encoded as bytes, before returned to user
48  private int convertBuffOffset = 0; //offset to start returning data to user on next read()
49  private int bytesInConvertBuff = 0; //amount of data currently in the buffer
50  private boolean fileEOF = false; //if file has more bytes to read
51  private boolean extractUTF8;
52  private boolean extractUTF16;
53  private Charset outCharset;
54 
55  private StringExtractResult lastExtractResult;
56 
70  public AbstractFileStringIntStream(AbstractFile content, List<SCRIPT> scripts, boolean extractUTF8,
71  boolean extractUTF16, Charset outCharset) {
72  this.content = content;
73  this.stringExtractor = new StringExtract();
74  this.stringExtractor.setEnabledScripts(scripts);
75  this.extractUTF8 = extractUTF8;
76  this.extractUTF16 = extractUTF16;
77  this.outCharset = outCharset;
78  this.stringExtractor.setEnableUTF8(extractUTF8);
79  this.stringExtractor.setEnableUTF16(extractUTF16);
80  }
81 
82  @Override
83  public int read() throws IOException {
84  if (extractUTF8 == false && extractUTF16 == false) {
85  return -1;
86  }
87  final int read = read(oneCharBuf, 0, 1);
88  if (read == 1) {
89  return oneCharBuf[0];
90  } else {
91  return -1;
92  }
93 
94  }
95 
96  @Override
97  public int read(byte[] b, int off, int len) throws IOException {
98  if (b == null) {
99  throw new NullPointerException();
100  } else if (off < 0 || len < 0 || len > b.length - off) {
101  throw new IndexOutOfBoundsException();
102  } else if (len == 0) {
103  return 0;
104  }
105 
106  if (extractUTF8 == false && extractUTF16 == false) {
107  return -1;
108  }
109 
110  long fileSize = content.getSize();
111  if (fileSize == 0) {
112  return -1;
113  }
114 
115  //read and convert until user buffer full
116  //we have data if file can be read or when byteBuff has converted strings to return
117  int bytesToUser = 0; //returned to user so far
118  int offsetUser = off;
119  while (bytesToUser < len && offsetUser < len) {
120  //check if we have enough converted strings
121  int convertBuffRemain = bytesInConvertBuff - convertBuffOffset;
122 
123  if ((convertBuff == null || convertBuffRemain == 0) && !fileEOF && fileReadOffset < fileSize) {
124  try {
125  //convert more strings, store in buffer
126  long toRead = 0;
127  //int shiftSize = 0;
128 
129  //if (lastExtractResult != null && lastExtractResult.getTextLength() != 0
130  // && (shiftSize = FILE_BUF_SIZE - lastExtractResult.getFirstUnprocessedOff()) > 0) {
135  //byte[] temp = new byte[shiftSize];
136  //System.arraycopy(fileReadBuff, lastExtractResult.getFirstUnprocessedOff(),
137  // temp, 0, shiftSize);
138  //System.arraycopy(temp, 0, fileReadBuff, 0, shiftSize);
139  //toRead = Math.min(lastExtractResult.getFirstUnprocessedOff(), fileSize - fileReadOffset);
140  //lastExtractResult = null;
141  //} else {
142  //fill up entire fileReadBuff fresh
143  toRead = Math.min(FILE_BUF_SIZE, fileSize - fileReadOffset);
144  //}
145  int read = content.read(fileReadBuff, fileReadOffset, toRead);
146  if (read == -1 || read == 0) {
147  fileEOF = true;
148  } else {
149  fileReadOffset += read;
150  if (fileReadOffset >= fileSize) {
151  fileEOF = true;
152  }
153 
154  //put converted string in convertBuff
155  convert(read);
156  convertBuffRemain = bytesInConvertBuff - convertBuffOffset;
157  }
158  } catch (TskCoreException ex) {
159  //Exceptions.printStackTrace(ex);
160  fileEOF = true;
161  }
162  }
163 
164  //nothing more to read, and no more bytes in convertBuff
165  if (convertBuff == null || convertBuffRemain == 0) {
166  if (fileEOF) {
167  return bytesToUser > 0 ? bytesToUser : -1;
168  } else {
169  //no strings extracted, try another read
170  continue;
171  }
172  }
173 
174  //return part or all of convert buff to user
175  final int toCopy = Math.min(convertBuffRemain, len - offsetUser);
176  System.arraycopy(convertBuff, convertBuffOffset, b, offsetUser, toCopy);
177 
178  //DEBUG
179  /*
180  * if (toCopy > 0) { FileOutputStream debug = new
181  * FileOutputStream("c:\\temp\\" + content.getName(), true);
182  * debug.write(b, offsetUser, toCopy); debug.close(); }
183  */
184  convertBuffOffset += toCopy;
185  offsetUser += toCopy;
186 
187  bytesToUser += toCopy;
188 
189  }
190 
191  //if more string data in convertBuff, will be consumed on next read()
192  return bytesToUser;
193  }
194 
201  private void convert(int numBytes) {
202  lastExtractResult = stringExtractor.extract(fileReadBuff, numBytes, 0);
203  convertBuff = lastExtractResult.getText().getBytes(outCharset);
204 
205  //reset tracking vars
206  if (lastExtractResult.getNumBytes() == 0) {
207  bytesInConvertBuff = 0;
208  } else {
209  bytesInConvertBuff = convertBuff.length;
210  }
211  convertBuffOffset = 0;
212  }
213 }

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