List of usage examples for javax.crypto CipherOutputStream flush
public void flush() throws IOException
From source file:com.microsoft.azure.storage.core.Utility.java
/** * Encrypts an input stream up to a given length. * Exits early if the encrypted data is longer than the abandon length. * //from w w w.jav a 2 s. com * @param sourceStream * A <code>InputStream</code> object that represents the stream to measure. * @param targetStream * A <code>ByteArrayOutputStream</code> object that represents the stream to write the encrypted data. * @param cipher * The <code>Cipher</code> to use to encrypt the data. * @param writeLength * The number of bytes to read and encrypt from the sourceStream. * @param abandonLength * The number of bytes to read before the analysis is abandoned. Set this value to <code>-1</code> to * force the entire stream to be read. This parameter is provided to support upload thresholds. * @return * The size of the encrypted stream, or -1 if the encrypted stream would be over the abandonLength. * @throws IOException * If an I/O error occurs. */ public static long encryptStreamIfUnderThreshold(final InputStream sourceStream, final ByteArrayOutputStream targetStream, Cipher cipher, long writeLength, long abandonLength) throws IOException { if (abandonLength < 0) { abandonLength = Long.MAX_VALUE; } if (!sourceStream.markSupported()) { throw new IllegalArgumentException(SR.INPUT_STREAM_SHOULD_BE_MARKABLE); } sourceStream.mark(Constants.MAX_MARK_LENGTH); if (writeLength < 0) { writeLength = Long.MAX_VALUE; } CipherOutputStream encryptStream = new CipherOutputStream(targetStream, cipher); int count = -1; long totalEncryptedLength = targetStream.size(); final byte[] retrievedBuff = new byte[Constants.BUFFER_COPY_LENGTH]; int nextCopy = (int) Math.min(retrievedBuff.length, writeLength - totalEncryptedLength); count = sourceStream.read(retrievedBuff, 0, nextCopy); while (nextCopy > 0 && count != -1) { // Note: We are flushing the CryptoStream on every write here. This way, we don't end up encrypting more data than we intend here, if // we go over the abandonLength. encryptStream.write(retrievedBuff, 0, count); encryptStream.flush(); totalEncryptedLength = targetStream.size(); if (totalEncryptedLength > abandonLength) { // Abandon operation break; } nextCopy = (int) Math.min(retrievedBuff.length, writeLength - totalEncryptedLength); count = sourceStream.read(retrievedBuff, 0, nextCopy); } sourceStream.reset(); sourceStream.mark(Constants.MAX_MARK_LENGTH); encryptStream.close(); totalEncryptedLength = targetStream.size(); if (totalEncryptedLength > abandonLength) { totalEncryptedLength = -1; } return totalEncryptedLength; }
From source file:com.zacwolf.commons.crypto._CRYPTOfactory.java
public final void encryptToOutputStream(final byte[] bytes, final OutputStream outputStream) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IOException { ready();// ww w . j av a2s . c o m activecrypts++; try { final Cipher ecipher = crypter.getEcipher(); final byte[] salt = ecipher.getIV(); if (salt == null) { outputStream.write(0); } else { outputStream.write(salt.length); outputStream.write(salt); } outputStream.flush(); CipherOutputStream cop = null; try { cop = new CipherOutputStream(outputStream, ecipher) { /* * WebSphere 7 has a known bug with it's implementation of ibmjceprovider.jar * concerning writing byte-arrays in a serialized object when the byte-array length * is zero. * see: http://www.ibm.com/developerworks/forums/thread.jspa?messageID=14597510 * * Added an override of the CipherOutputStream write method so that it is only called when * the byte array has length > 0 */ @Override public void write(final byte[] b, final int off, final int len) throws IOException { if (len > 0) { super.write(b, off, len); //super.flush(); Do NOT flush here, as it slows the process down exponentially } } }; cop.write(bytes); cop.flush(); } finally { if (cop != null) { cop.flush(); cop.close(); } outputStream.flush(); outputStream.close(); } } finally { activecrypts--; } }