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); } } 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(); + } + } }