diff --git a/bindings/java/src/org/sleuthkit/datamodel/EncodedFileOutputStream.java b/bindings/java/src/org/sleuthkit/datamodel/EncodedFileOutputStream.java index f181b936f0dcf888cb92ede4d87c201aa03b522e..7c0183e1dd8682bee1eee1d9a2960b7f5014ad2f 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/EncodedFileOutputStream.java +++ b/bindings/java/src/org/sleuthkit/datamodel/EncodedFileOutputStream.java @@ -30,7 +30,8 @@ */ 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. @@ -43,6 +44,7 @@ public class EncodedFileOutputStream extends BufferedOutputStream { public EncodedFileOutputStream(OutputStream out, TskData.EncodingType type) throws IOException { super(out); this.type = type; + encodedDataLength = 0; writeHeader(); } @@ -65,11 +67,13 @@ public EncodedFileOutputStream(OutputStream out, int size, TskData.EncodingType private void writeHeader() throws IOException { // We get the encoded header here so it will be in plaintext after encoding write(EncodedFileUtil.getEncodedHeader(type), 0, EncodedFileUtil.getHeaderLength()); + encodedDataLength -= EncodedFileUtil.getHeaderLength(); } @Override public void write(int b) throws IOException { super.write((int) EncodedFileUtil.encodeByte((byte) b, type)); + encodedDataLength++; } @Override @@ -83,5 +87,17 @@ public void write(byte[] b, } 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 diff --git a/bindings/java/src/org/sleuthkit/datamodel/HashUtility.java b/bindings/java/src/org/sleuthkit/datamodel/HashUtility.java index d8618b0ee606a08b9875d2a7f0e06dba818f62b9..4ce6e192eea18ad4fcb1b32c5ed5ff4056071073 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/HashUtility.java +++ b/bindings/java/src/org/sleuthkit/datamodel/HashUtility.java @@ -68,6 +68,11 @@ static public List<HashResult> calculateHashes(Content content, Collection<HashT } catch (TskCoreException 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. if (read == BUFFER_SIZE) { @@ -228,4 +233,4 @@ static public String calculateMd5Hash(Content content) throws IOException { throw new IOException(ex); } } -} +} \ No newline at end of file