本文整理汇总了Java中java.security.DigestOutputStream.flush方法的典型用法代码示例。如果您正苦于以下问题:Java DigestOutputStream.flush方法的具体用法?Java DigestOutputStream.flush怎么用?Java DigestOutputStream.flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.DigestOutputStream
的用法示例。
在下文中一共展示了DigestOutputStream.flush方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: genContent
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* Generate random contents for the image and store it together with the md5
* for later comparison.
*/
private ContentBody genContent(long txid) throws IOException {
MessageDigest digester = MD5Hash.getDigester();
// write through digester so we can roll the written image
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
DigestOutputStream ds = new DigestOutputStream(bos, digester);
// generate random bytes
new Random().nextBytes(randomBytes);
ds.write(randomBytes);
ds.flush();
// get written hash
MD5Hash hash = new MD5Hash(digester.digest());
// store contents and digest
digests.put(txid, hash);
content.put(txid, Arrays.copyOf(randomBytes, randomBytes.length));
return new ByteArrayBody(bos.toByteArray(), "filename");
}
示例2: writeRandomFile
import java.security.DigestOutputStream; //导入方法依赖的package包/类
public static byte [] writeRandomFile(int bytes, OutputStream out) throws IOException {
try {
DigestOutputStream dos = new DigestOutputStream(out, MessageDigest.getInstance(CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM));
byte [] buf = new byte[BUF_SIZE];
int count = 0;
int towrite = 0;
while (count < bytes) {
random.nextBytes(buf);
towrite = ((bytes - count) > buf.length) ? buf.length : (bytes - count);
dos.write(buf, 0, towrite);
count += towrite;
}
dos.flush();
dos.close();
return dos.getMessageDigest().digest();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Cannot find digest algorithm: " + CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM);
}
}
示例3: digestContent
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* Encode and digest the object's content in order to detect changes made
* outside of the object's own interface (for example, if the data is accessed
* using data() and then modified).
* @return
* @throws ContentEncodingException if there is a problem encoding the content
* @throws IOException if there is a problem writing the object to the stream.
*/
protected byte [] digestContent() throws ContentEncodingException, IOException {
try {
// Otherwise, might have been written when we weren't looking (someone accessed
// data and then changed it).
DigestOutputStream dos = new DigestOutputStream(new NullOutputStream(), MessageDigest.getInstance(DEFAULT_CHECKSUM_ALGORITHM));
writeObjectImpl(dos);
dos.flush();
dos.close();
byte [] currentValue = dos.getMessageDigest().digest();
return currentValue;
} catch (NoSuchAlgorithmException e) {
Log.warning("No pre-configured algorithm {0} available -- configuration error!", DEFAULT_CHECKSUM_ALGORITHM);
throw new RuntimeException("No pre-configured algorithm " + DEFAULT_CHECKSUM_ALGORITHM + " available -- configuration error!");
}
}
示例4: getHashValue
import java.security.DigestOutputStream; //导入方法依赖的package包/类
private byte[] getHashValue(File file) throws Exception {
MessageDigest sha = MessageDigest.getInstance("SHA-256");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DigestOutputStream dout = new DigestOutputStream(baos, sha);
byte[] data = new byte[1024];
BufferedInputStream is = new BufferedInputStream(file.toURL().openStream());
while (true) {
int bytesRead = is.read(data);
if (bytesRead < 0)
break;
dout.write(data, 0, bytesRead);
}
dout.flush();
byte[] result = dout.getMessageDigest().digest();
return result;
}
示例5: save
import java.security.DigestOutputStream; //导入方法依赖的package包/类
public boolean save(Avatar avatar) {
File file;
if (isAvatarCached(avatar)) {
file = new File(getAvatarPath(avatar.getFilename()));
avatar.size = file.length();
} else {
String filename = getAvatarPath(avatar.getFilename());
file = new File(filename + ".tmp");
file.getParentFile().mkdirs();
OutputStream os = null;
try {
file.createNewFile();
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)) {
file.renameTo(new File(filename));
} else {
Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
file.delete();
return false;
}
avatar.size = bytes.length;
} catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
return false;
} finally {
close(os);
}
}
return true;
}
示例6: save
import java.security.DigestOutputStream; //导入方法依赖的package包/类
public boolean save(Avatar avatar) {
File file;
if (isAvatarCached(avatar)) {
file = new File(getAvatarPath(avatar.getFilename()));
} else {
String filename = getAvatarPath(avatar.getFilename());
file = new File(filename + ".tmp");
file.getParentFile().mkdirs();
OutputStream os = null;
try {
file.createNewFile();
os = new FileOutputStream(file);
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.reset();
DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
mDigestOutputStream.write(avatar.getImageAsBytes());
mDigestOutputStream.flush();
mDigestOutputStream.close();
String sha1sum = CryptoHelper.bytesToHex(digest.digest());
if (sha1sum.equals(avatar.sha1sum)) {
file.renameTo(new File(filename));
} else {
Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
file.delete();
return false;
}
} catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
return false;
} finally {
close(os);
}
}
avatar.size = file.length();
return true;
}
示例7: getPepAvatar
import java.security.DigestOutputStream; //导入方法依赖的package包/类
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 (Exception e) {
return null;
}
}
示例8: save
import java.security.DigestOutputStream; //导入方法依赖的package包/类
public boolean save(Avatar avatar) {
File file;
if (isAvatarCached(avatar)) {
file = new File(getAvatarPath(avatar.getFilename()));
avatar.size = file.length();
} else {
String filename = getAvatarPath(avatar.getFilename());
file = new File(filename + ".tmp");
file.getParentFile().mkdirs();
OutputStream os = null;
try {
file.createNewFile();
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)) {
file.renameTo(new File(filename));
} else {
Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
file.delete();
return false;
}
avatar.size = bytes.length;
} catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
return false;
} finally {
close(os);
}
}
return true;
}
示例9: getPepAvatar
import java.security.DigestOutputStream; //导入方法依赖的package包/类
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 (Exception e) {
return null;
}
}
示例10: writeFile
import java.security.DigestOutputStream; //导入方法依赖的package包/类
public static int writeFile(ContentName name, boolean random, int size, CCNHandle handle) throws Exception {
int segmentsToWrite = 0;
RepositoryFileOutputStream rfos = new RepositoryFileOutputStream(name.append("randomFile"), handle);
DigestOutputStream dos = new DigestOutputStream(rfos, MessageDigest.getInstance(CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM));
byte [] buf = new byte[BUF_SIZE];
int count = 0;
int towrite = 0;
Random rand = new Random();
int bytes = 0;
if (random) {
bytes = rand.nextInt(maxBytes) + 1;
} else
bytes = size;
double block = (double)bytes/(double)SystemConfiguration.BLOCK_SIZE;
segmentsToWrite = (int) (Math.ceil(block) + 1);
Log.fine(Log.FAC_TEST, "bytes: {0} block size: {1} div: {2} ceil: {3}", bytes, SystemConfiguration.BLOCK_SIZE, block, (int)Math.ceil(block));
Log.fine(Log.FAC_TEST, "will write out a {0} byte file, will have {1} segments (1 is a header)", bytes, segmentsToWrite);
while (count < bytes) {
rand.nextBytes(buf);
towrite = ((bytes - count) > buf.length) ? buf.length : (bytes - count);
dos.write(buf, 0, towrite);
count += towrite;
}
dos.flush();
dos.close();
Log.info(Log.FAC_TEST, "Wrote file to repository: {0} with {1} segments", rfos.getBaseName(), segmentsToWrite);
return segmentsToWrite;
}
示例11: writeFile
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* @param completeName
* @param fileLength
* @param randBytes
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
*/
public static byte [] writeFile(Flosser flosser, ContentName completeName, int fileLength, Random randBytes) throws IOException, NoSuchAlgorithmException {
flosser.handleNamespace(completeName);
CCNOutputStream stockOutputStream = new CCNOutputStream(completeName, outputHandle);
DigestOutputStream digestStreamWrapper = new DigestOutputStream(stockOutputStream, MessageDigest.getInstance("SHA1"));
byte [] bytes = new byte[BUF_SIZE];
int elapsed = 0;
int nextBufSize = 0;
Log.info(Log.FAC_TEST, "Writing file: " + completeName + " bytes: " + fileLength);
final double probFlush = .3;
while (elapsed < fileLength) {
nextBufSize = ((fileLength - elapsed) > BUF_SIZE) ? BUF_SIZE : (fileLength - elapsed);
randBytes.nextBytes(bytes);
digestStreamWrapper.write(bytes, 0, nextBufSize);
elapsed += nextBufSize;
Log.info(completeName + " wrote " + elapsed + " out of " + fileLength + " bytes.");
if (randBytes.nextDouble() < probFlush) {
Log.info(Log.FAC_TEST, "Flushing buffers.");
digestStreamWrapper.flush();
}
}
digestStreamWrapper.close();
Log.info(Log.FAC_TEST, "Finished writing file " + completeName);
return digestStreamWrapper.getMessageDigest().digest();
}
示例12: internalWriteObject
import java.security.DigestOutputStream; //导入方法依赖的package包/类
/**
* Save the object and update the internal tracking digest of its last-saved content.
* @param output stream to write to.
* @throws ContentEncodingException if there is an error encoding the object
* @throws IOException if there is an error writing the object to the network
*/
protected synchronized void internalWriteObject(OutputStream output) throws ContentEncodingException, IOException {
try {
DigestOutputStream dos = new DigestOutputStream(output, MessageDigest.getInstance(DEFAULT_CHECKSUM_ALGORITHM));
writeObjectImpl(dos);
dos.flush(); // do not close dos, as it will close the output, allow caller to close
_lastSaved = dos.getMessageDigest().digest();
setDirty(false);
} catch (NoSuchAlgorithmException e) {
Log.warning("No pre-configured algorithm {0} available -- configuration error!", DEFAULT_CHECKSUM_ALGORITHM);
throw new RuntimeException("No pre-configured algorithm " + DEFAULT_CHECKSUM_ALGORITHM + " available -- configuration error!");
}
}