From 65ac7726c9ea793171d2dd2eb718a7bb5dbe4fdb Mon Sep 17 00:00:00 2001 From: Dave Date: Mon, 17 Apr 2023 14:00:56 +0200 Subject: [PATCH 1/2] TOTAL_SHARDS & Resource leak Warning --- .../com/backblaze/erasure/SampleEncoder.java | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/backblaze/erasure/SampleEncoder.java b/src/main/java/com/backblaze/erasure/SampleEncoder.java index e903397..fe61ca3 100644 --- a/src/main/java/com/backblaze/erasure/SampleEncoder.java +++ b/src/main/java/com/backblaze/erasure/SampleEncoder.java @@ -6,11 +6,11 @@ package com.backblaze.erasure; +import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; @@ -30,9 +30,9 @@ */ public class SampleEncoder { - public static final int DATA_SHARDS = 4; + public static final int DATA_SHARDS = 4; public static final int PARITY_SHARDS = 2; - public static final int TOTAL_SHARDS = 6; + public static final int TOTAL_SHARDS = DATA_SHARDS + PARITY_SHARDS; public static final int BYTES_IN_INT = 4; @@ -49,7 +49,7 @@ public static void main(String [] arguments) throws IOException { return; } - // Get the size of the input file. (Files bigger that + // Get the size of the input file. (Files bigger than // Integer.MAX_VALUE will fail here!) final int fileSize = (int) inputFile.length(); @@ -62,13 +62,8 @@ public static void main(String [] arguments) throws IOException { // the contents of the file. final int bufferSize = shardSize * DATA_SHARDS; final byte [] allBytes = new byte[bufferSize]; - ByteBuffer.wrap(allBytes).putInt(fileSize); - InputStream in = new FileInputStream(inputFile); - int bytesRead = in.read(allBytes, BYTES_IN_INT, fileSize); - if (bytesRead != fileSize) { - throw new IOException("not enough bytes read"); - } - in.close(); + + readAllBytesPrefixedWithFileSize(inputFile, fileSize, allBytes); // Make the buffers to hold the shards. byte [] [] shards = new byte [TOTAL_SHARDS] [shardSize]; @@ -93,4 +88,17 @@ public static void main(String [] arguments) throws IOException { System.out.println("wrote " + outputFile); } } + + private static void readAllBytesPrefixedWithFileSize(final File inputFile, final int fileSize, final byte [] allBytes) throws IOException { + + final ByteBuffer buffer = ByteBuffer.wrap(allBytes).putInt(fileSize); + + final FileInputStream fileInputStream = new FileInputStream(inputFile); + final DataInputStream dataInputStream = new DataInputStream(fileInputStream); + try { + dataInputStream.readFully(buffer.array(), buffer.position(), fileSize); + } finally { + dataInputStream.close(); + } + } } From 3a4ffa3e273fb9910f5502b442ad6b3c4a814288 Mon Sep 17 00:00:00 2001 From: Dave Date: Tue, 18 Apr 2023 20:51:02 +0200 Subject: [PATCH 2/2] Typo.: to -> too --- src/main/java/com/backblaze/erasure/ReedSolomon.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/backblaze/erasure/ReedSolomon.java b/src/main/java/com/backblaze/erasure/ReedSolomon.java index 09f2194..f088412 100644 --- a/src/main/java/com/backblaze/erasure/ReedSolomon.java +++ b/src/main/java/com/backblaze/erasure/ReedSolomon.java @@ -296,7 +296,7 @@ private void checkBuffersAndSizes(byte [] [] shards, int offset, int byteCount) throw new IllegalArgumentException("byteCount is negative: " + byteCount); } if (shardLength < offset + byteCount) { - throw new IllegalArgumentException("buffers to small: " + byteCount + offset); + throw new IllegalArgumentException("buffers too small: " + byteCount + offset); } }