Last active
June 8, 2016 09:58
-
-
Save arnobroekhof/bd3a00d1e7a3a364fb5447b43b0bb891 to your computer and use it in GitHub Desktop.
inputstream to bytearray with md5 hashing ioctx
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
public void putObject(final InputStream inputStream, final String objectName, final int bufferSize) throws Exception { | |
int length = inputStream.available(); | |
byte[] buffer = new byte[bufferSize]; | |
int bytesRead; | |
int offset = 0; | |
logger.info("Copying file with objectName: {}", objectName); | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
while ((bytesRead = inputStream.read(buffer)) > 0) { | |
md.update(buffer, 0, bytesRead); | |
logger.debug("Bytes read: {} offset: {} length: {}", bytesRead, offset, (length - offset)); | |
ioContext.truncate(objectName, offset); | |
ioContext.write(objectName, buffer, offset); | |
offset += bytesRead; | |
} | |
byte[] digest = md.digest(); | |
ioContext.writeAttribute(objectName, "MD5", new String(Hex.encodeHex(digest))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment