Skip to content
Snippets Groups Projects
Commit 48da2032 authored by apriestman's avatar apriestman
Browse files

Stop hashing if EOF is found.

Keep track of how many bytes are written to the EncodedFileOutputStream.
parent 6f2d2818
No related branches found
No related tags found
No related merge requests found
...@@ -30,7 +30,8 @@ ...@@ -30,7 +30,8 @@
*/ */
public class EncodedFileOutputStream extends BufferedOutputStream { public class EncodedFileOutputStream extends BufferedOutputStream {
private TskData.EncodingType type; private final TskData.EncodingType type;
private long encodedDataLength;
/** /**
* Create an encoded output stream using the specified encoding. * Create an encoded output stream using the specified encoding.
...@@ -43,6 +44,7 @@ public class EncodedFileOutputStream extends BufferedOutputStream { ...@@ -43,6 +44,7 @@ public class EncodedFileOutputStream extends BufferedOutputStream {
public EncodedFileOutputStream(OutputStream out, TskData.EncodingType type) throws IOException { public EncodedFileOutputStream(OutputStream out, TskData.EncodingType type) throws IOException {
super(out); super(out);
this.type = type; this.type = type;
encodedDataLength = 0;
writeHeader(); writeHeader();
} }
...@@ -65,11 +67,13 @@ public EncodedFileOutputStream(OutputStream out, int size, TskData.EncodingType ...@@ -65,11 +67,13 @@ public EncodedFileOutputStream(OutputStream out, int size, TskData.EncodingType
private void writeHeader() throws IOException { private void writeHeader() throws IOException {
// We get the encoded header here so it will be in plaintext after encoding // We get the encoded header here so it will be in plaintext after encoding
write(EncodedFileUtil.getEncodedHeader(type), 0, EncodedFileUtil.getHeaderLength()); write(EncodedFileUtil.getEncodedHeader(type), 0, EncodedFileUtil.getHeaderLength());
encodedDataLength -= EncodedFileUtil.getHeaderLength();
} }
@Override @Override
public void write(int b) throws IOException { public void write(int b) throws IOException {
super.write((int) EncodedFileUtil.encodeByte((byte) b, type)); super.write((int) EncodedFileUtil.encodeByte((byte) b, type));
encodedDataLength++;
} }
@Override @Override
...@@ -83,5 +87,17 @@ public void write(byte[] b, ...@@ -83,5 +87,17 @@ public void write(byte[] b,
} }
super.write(encodedData, off, len); super.write(encodedData, off, len);
encodedDataLength += len;
} }
}
/**
* Get the number of bytes written to the file, excluding header bytes.
* This is needed for storing the original length of the file in the
* tsk_files table in cases where we don't know the size in advance.
*
* @return the number of bytes written to the stream, excluding the header.
*/
public long getBytesWritten() {
return encodedDataLength;
}
}
\ No newline at end of file
...@@ -68,6 +68,11 @@ static public List<HashResult> calculateHashes(Content content, Collection<HashT ...@@ -68,6 +68,11 @@ static public List<HashResult> calculateHashes(Content content, Collection<HashT
} catch (TskCoreException ex) { } catch (TskCoreException ex) {
throw new TskCoreException("Error reading data at address " + i * BUFFER_SIZE + " from content with ID: " + content.getId(), ex); throw new TskCoreException("Error reading data at address " + i * BUFFER_SIZE + " from content with ID: " + content.getId(), ex);
} }
// Check for EOF
if (read == -1) {
break;
}
// Only update with the read bytes. // Only update with the read bytes.
if (read == BUFFER_SIZE) { if (read == BUFFER_SIZE) {
...@@ -228,4 +233,4 @@ static public String calculateMd5Hash(Content content) throws IOException { ...@@ -228,4 +233,4 @@ static public String calculateMd5Hash(Content content) throws IOException {
throw new IOException(ex); throw new IOException(ex);
} }
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment