19 package org.sleuthkit.autopsy.keywordsearch;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.charset.Charset;
24 import java.util.List;
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;
47 private int convertBuffOffset = 0;
48 private int bytesInConvertBuff = 0;
49 private boolean fileEOF =
false;
50 private boolean extractUTF8;
51 private boolean extractUTF16;
52 private Charset outCharset;
54 private StringExtractResult lastExtractResult;
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);
80 public int read() throws IOException {
81 if (extractUTF8 ==
false && extractUTF16 ==
false) {
84 final int read = read(oneCharBuf, 0, 1);
94 public int read(byte[] b,
int off,
int len)
throws IOException {
96 throw new NullPointerException();
97 }
else if (off < 0 || len < 0 || len > b.length - off) {
98 throw new IndexOutOfBoundsException();
99 }
else if (len == 0) {
103 if (extractUTF8 ==
false && extractUTF16 ==
false) {
107 long fileSize = content.getSize();
116 int offsetUser = off;
117 while (bytesToUser < len && offsetUser < len) {
119 int convertBuffRemain = bytesInConvertBuff - convertBuffOffset;
121 if ((convertBuff == null || convertBuffRemain == 0) && !fileEOF && fileReadOffset < fileSize) {
141 toRead = Math.min(FILE_BUF_SIZE, fileSize - fileReadOffset);
143 int read = content.read(fileReadBuff, fileReadOffset, toRead);
144 if (read == -1 || read == 0) {
147 fileReadOffset += read;
148 if (fileReadOffset >= fileSize) {
154 convertBuffRemain = bytesInConvertBuff - convertBuffOffset;
156 }
catch (TskCoreException ex) {
163 if (convertBuff == null || convertBuffRemain == 0) {
165 return bytesToUser > 0 ? bytesToUser : -1;
173 final int toCopy = Math.min(convertBuffRemain, len - offsetUser);
174 System.arraycopy(convertBuff, convertBuffOffset, b, offsetUser, toCopy);
185 convertBuffOffset += toCopy;
186 offsetUser += toCopy;
188 bytesToUser += toCopy;
204 private void convert(
int numBytes) {
205 lastExtractResult = stringExtractor.extract(fileReadBuff, numBytes, 0);
206 convertBuff = lastExtractResult.getText().getBytes(outCharset);
209 if (lastExtractResult.getNumBytes() == 0) {
210 bytesInConvertBuff = 0;
212 bytesInConvertBuff = convertBuff.length;
214 convertBuffOffset = 0;