use of java.security.DigestOutputStream in project jackrabbit by apache.
the class FileDataStore method addRecord.
/**
* Creates a new data record.
* The stream is first consumed and the contents are saved in a temporary file
* and the {@link #DIGEST} message digest of the stream is calculated. If a
* record with the same {@link #DIGEST} digest (and length) is found then it is
* returned. Otherwise the temporary file is moved in place to become
* the new data record that gets returned.
*
* @param input binary stream
* @return data record that contains the given stream
* @throws DataStoreException if the record could not be created
*/
public DataRecord addRecord(InputStream input) throws DataStoreException {
File temporary = null;
try {
temporary = newTemporaryFile();
DataIdentifier tempId = new DataIdentifier(temporary.getName());
usesIdentifier(tempId);
// Copy the stream to the temporary file and calculate the
// stream length and the message digest of the stream
long length = 0;
MessageDigest digest = MessageDigest.getInstance(DIGEST);
OutputStream output = new DigestOutputStream(new FileOutputStream(temporary), digest);
try {
length = IOUtils.copyLarge(input, output);
} finally {
output.close();
}
DataIdentifier identifier = new DataIdentifier(encodeHexString(digest.digest()));
File file;
synchronized (this) {
// Check if the same record already exists, or
// move the temporary file in place if needed
usesIdentifier(identifier);
file = getFile(identifier);
if (!file.exists()) {
File parent = file.getParentFile();
parent.mkdirs();
if (temporary.renameTo(file)) {
// no longer need to delete the temporary file
temporary = null;
} else {
throw new IOException("Can not rename " + temporary.getAbsolutePath() + " to " + file.getAbsolutePath() + " (media read only?)");
}
} else {
long now = System.currentTimeMillis();
if (getLastModified(file) < now + ACCESS_TIME_RESOLUTION) {
setLastModified(file, now + ACCESS_TIME_RESOLUTION);
}
}
if (file.length() != length) {
// but better safe than sorry...
if (!file.isFile()) {
throw new IOException("Not a file: " + file);
}
throw new IOException(DIGEST + " collision: " + file);
}
}
// this will also make sure that
// tempId is not garbage collected until here
inUse.remove(tempId);
return new FileDataRecord(this, identifier, file);
} catch (NoSuchAlgorithmException e) {
throw new DataStoreException(DIGEST + " not available", e);
} catch (IOException e) {
throw new DataStoreException("Could not add record", e);
} finally {
if (temporary != null) {
temporary.delete();
}
}
}
use of java.security.DigestOutputStream in project jackrabbit by apache.
the class CachingDataStore method addRecord.
/**
* Creates a new data record in {@link Backend}. The stream is first
* consumed and the contents are saved in a temporary file and the {@link #DIGEST}
* message digest of the stream is calculated. If a record with the same
* {@link #DIGEST} digest (and length) is found then it is returned. Otherwise new
* record is created in {@link Backend} and the temporary file is moved in
* place to {@link LocalCache}.
*
* @param input
* binary stream
* @return {@link CachingDataRecord}
* @throws DataStoreException
* if the record could not be created.
*/
@Override
public DataRecord addRecord(InputStream input) throws DataStoreException {
File temporary = null;
long startTime = System.currentTimeMillis();
long length = 0;
try {
temporary = newTemporaryFile();
DataIdentifier tempId = new DataIdentifier(temporary.getName());
usesIdentifier(tempId);
// Copy the stream to the temporary file and calculate the
// stream length and the message digest of the stream
MessageDigest digest = MessageDigest.getInstance(DIGEST);
OutputStream output = new DigestOutputStream(new FileOutputStream(temporary), digest);
try {
length = IOUtils.copyLarge(input, output);
} finally {
output.close();
}
long currTime = System.currentTimeMillis();
DataIdentifier identifier = new DataIdentifier(encodeHexString(digest.digest()));
LOG.debug("Digest of [{}], length =[{}] took [{}]ms ", new Object[] { identifier, length, (currTime - startTime) });
String fileName = getFileName(identifier);
AsyncUploadCacheResult result = null;
synchronized (this) {
usesIdentifier(identifier);
// check if async upload is already in progress
if (!asyncWriteCache.hasEntry(fileName, true)) {
result = cache.store(fileName, temporary, true);
}
}
LOG.debug("storing [{}] in localCache took [{}] ms", identifier, (System.currentTimeMillis() - currTime));
if (result != null) {
if (result.canAsyncUpload()) {
backend.writeAsync(identifier, result.getFile(), this);
} else {
backend.write(identifier, result.getFile());
}
}
// this will also make sure that
// tempId is not garbage collected until here
inUse.remove(tempId);
LOG.debug("addRecord [{}] of length [{}] took [{}]ms.", new Object[] { identifier, length, (System.currentTimeMillis() - startTime) });
return new CachingDataRecord(this, identifier);
} catch (NoSuchAlgorithmException e) {
throw new DataStoreException(DIGEST + " not available", e);
} catch (IOException e) {
throw new DataStoreException("Could not add record", e);
} finally {
if (temporary != null) {
// try to delete - but it's not a big deal if we can't
temporary.delete();
}
}
}
use of java.security.DigestOutputStream in project Conversations by siacs.
the class FileBackend method getPepAvatar.
private Avatar getPepAvatar(Bitmap bitmap, Bitmap.CompressFormat format, int quality) {
try {
ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
MessageDigest digest = MessageDigest.getInstance("SHA-1");
DigestOutputStream mDigestOutputStream = new DigestOutputStream(mBase64OutputStream, digest);
if (!bitmap.compress(format, quality, mDigestOutputStream)) {
return null;
}
mDigestOutputStream.flush();
mDigestOutputStream.close();
long chars = mByteArrayOutputStream.size();
if (format != Bitmap.CompressFormat.PNG && quality >= 50 && chars >= Config.AVATAR_CHAR_LIMIT) {
int q = quality - 2;
Log.d(Config.LOGTAG, "avatar char length was " + chars + " reducing quality to " + q);
return getPepAvatar(bitmap, format, q);
}
Log.d(Config.LOGTAG, "settled on char length " + chars + " with quality=" + quality);
final Avatar avatar = new Avatar();
avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
avatar.image = new String(mByteArrayOutputStream.toByteArray());
if (format.equals(Bitmap.CompressFormat.WEBP)) {
avatar.type = "image/webp";
} else if (format.equals(Bitmap.CompressFormat.JPEG)) {
avatar.type = "image/jpeg";
} else if (format.equals(Bitmap.CompressFormat.PNG)) {
avatar.type = "image/png";
}
avatar.width = bitmap.getWidth();
avatar.height = bitmap.getHeight();
return avatar;
} catch (OutOfMemoryError e) {
Log.d(Config.LOGTAG, "unable to convert avatar to base64 due to low memory");
return null;
} catch (Exception e) {
return null;
}
}
use of java.security.DigestOutputStream in project Conversations by siacs.
the class FileBackend method save.
public boolean save(final Avatar avatar) {
File file;
if (isAvatarCached(avatar)) {
file = getAvatarFile(avatar.getFilename());
avatar.size = file.length();
} else {
file = new File(mXmppConnectionService.getCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString());
if (file.getParentFile().mkdirs()) {
Log.d(Config.LOGTAG, "created cache directory");
}
OutputStream os = null;
try {
if (!file.createNewFile()) {
Log.d(Config.LOGTAG, "unable to create temporary file " + file.getAbsolutePath());
}
os = new FileOutputStream(file);
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.reset();
DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
final byte[] bytes = avatar.getImageAsBytes();
mDigestOutputStream.write(bytes);
mDigestOutputStream.flush();
mDigestOutputStream.close();
String sha1sum = CryptoHelper.bytesToHex(digest.digest());
if (sha1sum.equals(avatar.sha1sum)) {
final File outputFile = getAvatarFile(avatar.getFilename());
if (outputFile.getParentFile().mkdirs()) {
Log.d(Config.LOGTAG, "created avatar directory");
}
final File avatarFile = getAvatarFile(avatar.getFilename());
if (!file.renameTo(avatarFile)) {
Log.d(Config.LOGTAG, "unable to rename " + file.getAbsolutePath() + " to " + outputFile);
return false;
}
} else {
Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
if (!file.delete()) {
Log.d(Config.LOGTAG, "unable to delete temporary file");
}
return false;
}
avatar.size = bytes.length;
} catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
return false;
} finally {
close(os);
}
}
return true;
}
use of java.security.DigestOutputStream in project j2objc by google.
the class DigestOutputStreamTest method testWriteint05.
/**
* Test #5 for <code>write(int)</code> method<br>
* Test #2 for <code>on(boolean)</code> method<br>
*
* Assertion: broken <code>DigestOutputStream</code>instance:
* associated <code>MessageDigest</code> not set.
* <code>write(int)</code> must work when digest
* functionality is off
*/
public final void testWriteint05() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
DigestOutputStream dos = new DigestOutputStream(bos, null);
// set digest functionality to off
dos.on(false);
// the following must pass without any exception
for (int i = 0; i < MY_MESSAGE_LEN; i++) {
dos.write(myMessage[i]);
}
// check that bytes have been written correctly
assertTrue(Arrays.equals(MDGoldenData.getMessage(), bos.toByteArray()));
}
Aggregations