-
Notifications
You must be signed in to change notification settings - Fork 487
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
harshavardhana
merged 1 commit into
minio:master
from
DevFactory:rel/source-files-should-not-have-any-duplicated-blocks-fix-1
Jun 9, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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. | ||
*/ | ||
|
@@ -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, | ||
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(); | ||
|
@@ -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() {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.