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

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.