Skip to content

Instantly share code, notes, and snippets.

@arnobroekhof
Last active June 8, 2016 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnobroekhof/bd3a00d1e7a3a364fb5447b43b0bb891 to your computer and use it in GitHub Desktop.
Save arnobroekhof/bd3a00d1e7a3a364fb5447b43b0bb891 to your computer and use it in GitHub Desktop.
inputstream to bytearray with md5 hashing ioctx
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