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

common-java:DuplicatedBlocks - Source files should not have any duplicated blocks #394

Merged
merged 1 commit into from
Jun 9, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 48 additions & 58 deletions src/main/java/io/minio/Digest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,45 +106,9 @@ public static String md5Hash(Object inputStream, int len)
throw new IllegalArgumentException("unsupported input stream object");
}

MessageDigest md5Digest;
md5Digest = MessageDigest.getInstance("MD5");

// 16KiB buffer for optimization
byte[] buf = new byte[16384];
int bytesToRead = buf.length;
int bytesRead;
int totalBytesRead = 0;
int length = len;
long pos = 0;

if (file != null) {
pos = file.getFilePointer();
} else {
stream.mark(len);
}

do {
if ((length - totalBytesRead) < bytesToRead) {
bytesToRead = length - totalBytesRead;
}

if (file != null) {
bytesRead = file.read(buf, 0, bytesToRead);
} else {
bytesRead = stream.read(buf, 0, bytesToRead);
}

if (bytesRead < 0) {
throw new InsufficientDataException("Insufficient data. bytes read " + totalBytesRead + " expected "
+ length);
} else if (bytesRead == 0) {
continue;
}

md5Digest.update(buf, 0, bytesRead);

totalBytesRead += bytesRead;
} while (totalBytesRead < length);
MessageDigest md5Digest = MessageDigest.getInstance("MD5");
pos = readBytes(len, file, stream, pos, null, md5Digest);

if (file != null) {
file.seek(pos);
Expand All @@ -155,7 +119,6 @@ public static String md5Hash(Object inputStream, int len)
return BaseEncoding.base64().encode(md5Digest.digest());
}


/**
* Returns SHA-256 and MD5 hashes for given string.
*/
Expand Down Expand Up @@ -200,19 +163,49 @@ public static String[] sha256md5Hashes(Object inputStream, int len)
throw new IllegalArgumentException("unsupported input stream object");
}

MessageDigest sha256Digest;
MessageDigest md5Digest;
MessageDigest sha256Digest = MessageDigest.getInstance("SHA-256");
MessageDigest md5Digest = MessageDigest.getInstance("MD5");
long pos = 0;
pos = readBytes(len, file, stream, pos, sha256Digest, md5Digest);

sha256Digest = MessageDigest.getInstance("SHA-256");
md5Digest = MessageDigest.getInstance("MD5");
if (file != null) {
file.seek(pos);
} else {
stream.reset();
}

String[] hashes = { BaseEncoding.base16().encode(sha256Digest.digest()).toLowerCase(),
BaseEncoding.base64().encode(md5Digest.digest()) };

return hashes;
}


/**
* Updated MessageDigest with bytes read from file and stream
*
* @param len
* @param file
* @param stream
* @param pos
* @param sha256Digest
* @param md5Digest
* @return
* @throws IOException
* @throws InsufficientDataException
*/
private static long readBytes(int len,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment about this function.

RandomAccessFile file,
BufferedInputStream stream,
long pos,
MessageDigest sha256Digest,
MessageDigest md5Digest) throws IOException, InsufficientDataException {
// 16KiB buffer for optimization
byte[] buf = new byte[16384];
int bytesToRead = buf.length;
int bytesRead;
int totalBytesRead = 0;
int length = len;
long pos = 0;

if (file != null) {
pos = file.getFilePointer();
Expand All @@ -233,28 +226,25 @@ public static String[] sha256md5Hashes(Object inputStream, int len)

if (bytesRead < 0) {
throw new InsufficientDataException("Insufficient data. bytes read " + totalBytesRead + " expected "
+ length);
+ length);
} else if (bytesRead == 0) {
continue;
}

sha256Digest.update(buf, 0, bytesRead);
md5Digest.update(buf, 0, bytesRead);
if(sha256Digest != null) {
sha256Digest.update(buf, 0, bytesRead);
}
if(md5Digest != null) {
md5Digest.update(buf, 0, bytesRead);
}

totalBytesRead += bytesRead;
} while (totalBytesRead < length);

if (file != null) {
file.seek(pos);
} else {
stream.reset();
}

String[] hashes = { BaseEncoding.base16().encode(sha256Digest.digest()).toLowerCase(),
BaseEncoding.base64().encode(md5Digest.digest()) };

return hashes;
return pos;
}

/**
* Private constructor
*/
private Digest() {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment about this empty constructor. Please leave two newlines between member functions.

}