Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TOTAL_SHARDS, Resource leak Warning & a couple of Typos #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/backblaze/erasure/ReedSolomon.java
Expand Up @@ -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);
}
}

Expand Down
30 changes: 19 additions & 11 deletions src/main/java/com/backblaze/erasure/SampleEncoder.java
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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();

Expand All @@ -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];
Expand All @@ -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();
}
}
}