http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/95a8d486/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java b/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java index 255e57e..a962215 100644 --- a/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java +++ b/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java @@ -40,324 +40,326 @@ import org.apache.commons.crypto.utils.Utils; import static org.apache.commons.crypto.cipher.CipherTransformation.AES_CTR_NOPADDING; /** - * PositionedCryptoInputStream provides the capability to decrypt the stream starting - * at random position as well as provides the foundation for positioned read for - * decrypting. This needs a stream cipher mode such as AES CTR mode. + * PositionedCryptoInputStream provides the capability to decrypt the stream + * starting at random position as well as provides the foundation for positioned + * read for decrypting. This needs a stream cipher mode such as AES CTR mode. */ public class PositionedCryptoInputStream extends CTRCryptoInputStream { - /** - * DirectBuffer pool - */ - private final Queue<ByteBuffer> bufferPool = new - ConcurrentLinkedQueue<ByteBuffer>(); - - /** - * CryptoCipher pool - */ - private final Queue<CipherState> cipherPool = new - ConcurrentLinkedQueue<CipherState>(); - - /** - * Constructs a {@link PositionedCryptoInputStream}. - * - * @param props The <code>Properties</code> class represents a set of - * properties. - * @param in the input data. - * @param key crypto key for the cipher. - * @param iv Initialization vector for the cipher. - * @param streamOffset the start offset in the data. - * @throws IOException if an I/O error occurs. - */ - public PositionedCryptoInputStream(Properties props, Input in, byte[] key, - byte[] iv, long streamOffset) throws IOException { - this(in, Utils.getCipherInstance(AES_CTR_NOPADDING, props), - Utils.getBufferSize(props), key, iv, streamOffset); - } - - /** - * Constructs a {@link PositionedCryptoInputStream}. - * - * @param input the input data. - * @param cipher the CryptoCipher instance. - * @param bufferSize the bufferSize. - * @param key crypto key for the cipher. - * @param iv Initialization vector for the cipher. - * @param streamOffset the start offset in the data. - * @throws IOException if an I/O error occurs. - */ - public PositionedCryptoInputStream(Input input, CryptoCipher cipher, int bufferSize, - byte[] key, byte[] iv, long streamOffset) throws IOException { - super(input, cipher, bufferSize, key, iv, streamOffset); - } - - /** - * Reads up to the specified number of bytes from a given position - * within a stream and return the number of bytes read. This does not - * change the current offset of the stream, and is thread-safe. - * - * @param buffer the buffer into which the data is read. - * @param length the maximum number of bytes to read. - * @param offset the start offset in the data. - * @param position the offset from the start of the stream. - * @throws IOException if an I/O error occurs. - * @return int the total number of decrypted data bytes read into the buffer. - */ - public int read(long position, byte[] buffer, int offset, int length) - throws IOException { - checkStream(); - final int n = input.read(position, buffer, offset, length); - if (n > 0) { - // This operation does not change the current offset of the file - decrypt(position, buffer, offset, n); + /** + * DirectBuffer pool + */ + private final Queue<ByteBuffer> bufferPool = new ConcurrentLinkedQueue<ByteBuffer>(); + + /** + * CryptoCipher pool + */ + private final Queue<CipherState> cipherPool = new ConcurrentLinkedQueue<CipherState>(); + + /** + * Constructs a {@link PositionedCryptoInputStream}. + * + * @param props The <code>Properties</code> class represents a set of + * properties. + * @param in the input data. + * @param key crypto key for the cipher. + * @param iv Initialization vector for the cipher. + * @param streamOffset the start offset in the data. + * @throws IOException if an I/O error occurs. + */ + public PositionedCryptoInputStream(Properties props, Input in, byte[] key, + byte[] iv, long streamOffset) throws IOException { + this(in, Utils.getCipherInstance(AES_CTR_NOPADDING, props), Utils + .getBufferSize(props), key, iv, streamOffset); } - return n; - } - - /** - * Reads the specified number of bytes from a given position within a stream. - * This does not change the current offset of the stream and is thread-safe. - * - * @param buffer the buffer into which the data is read. - * @param length the maximum number of bytes to read. - * @param offset the start offset in the data. - * @param position the offset from the start of the stream. - * @throws IOException if an I/O error occurs. - */ - public void readFully(long position, byte[] buffer, int offset, int length) - throws IOException { - checkStream(); - IOUtils.readFully(input, position, buffer, offset, length); - if (length > 0) { - // This operation does not change the current offset of the file - decrypt(position, buffer, offset, length); + + /** + * Constructs a {@link PositionedCryptoInputStream}. + * + * @param input the input data. + * @param cipher the CryptoCipher instance. + * @param bufferSize the bufferSize. + * @param key crypto key for the cipher. + * @param iv Initialization vector for the cipher. + * @param streamOffset the start offset in the data. + * @throws IOException if an I/O error occurs. + */ + public PositionedCryptoInputStream(Input input, CryptoCipher cipher, + int bufferSize, byte[] key, byte[] iv, long streamOffset) + throws IOException { + super(input, cipher, bufferSize, key, iv, streamOffset); } - } - - /** - * Reads the specified number of bytes from a given position within a stream. - * This does not change the current offset of the stream and is thread-safe. - * - * @param position the offset from the start of the stream. - * @param buffer the buffer into which the data is read. - * @throws IOException if an I/O error occurs. - */ - public void readFully(long position, byte[] buffer) throws IOException { - readFully(position, buffer, 0, buffer.length); - } - - /** - * Decrypts length bytes in buffer starting at offset. Output is also put - * into buffer starting at offset. It is thread-safe. - * - * @param buffer the buffer into which the data is read. - * @param offset the start offset in the data. - * @param position the offset from the start of the stream. - * @param length the maximum number of bytes to read. - * @throws IOException if an I/O error occurs. - */ - protected void decrypt(long position, byte[] buffer, int offset, int length) - throws IOException { - ByteBuffer inBuffer = getBuffer(); - ByteBuffer outBuffer = getBuffer(); - CipherState state = null; - try { - state = getCipherState(); - byte[] iv = getInitIV().clone(); - resetCipher(state, position, iv); - byte padding = getPadding(position); - inBuffer.position(padding); // Set proper position for input data. - - int n = 0; - while (n < length) { - int toDecrypt = Math.min(length - n, inBuffer.remaining()); - inBuffer.put(buffer, offset + n, toDecrypt); - - // Do decryption - decrypt(state, inBuffer, outBuffer, padding); - - outBuffer.get(buffer, offset + n, toDecrypt); - n += toDecrypt; - padding = postDecryption(state, inBuffer, position + n, iv); - } - } finally { - returnBuffer(inBuffer); - returnBuffer(outBuffer); - returnCipherState(state); + + /** + * Reads up to the specified number of bytes from a given position within a + * stream and return the number of bytes read. This does not change the + * current offset of the stream, and is thread-safe. + * + * @param buffer the buffer into which the data is read. + * @param length the maximum number of bytes to read. + * @param offset the start offset in the data. + * @param position the offset from the start of the stream. + * @throws IOException if an I/O error occurs. + * @return int the total number of decrypted data bytes read into the + * buffer. + */ + public int read(long position, byte[] buffer, int offset, int length) + throws IOException { + checkStream(); + final int n = input.read(position, buffer, offset, length); + if (n > 0) { + // This operation does not change the current offset of the file + decrypt(position, buffer, offset, n); + } + return n; } - } - - /** - * Does the decryption using inBuffer as input and outBuffer as output. - * Upon return, inBuffer is cleared; the decrypted data starts at - * outBuffer.position() and ends at outBuffer.limit() - */ - private void decrypt(CipherState state, ByteBuffer inBuffer, - ByteBuffer outBuffer, byte padding) throws IOException { - Utils.checkState(inBuffer.position() >= padding); - if(inBuffer.position() == padding) { - // There is no real data in inBuffer. - return; + + /** + * Reads the specified number of bytes from a given position within a + * stream. This does not change the current offset of the stream and is + * thread-safe. + * + * @param buffer the buffer into which the data is read. + * @param length the maximum number of bytes to read. + * @param offset the start offset in the data. + * @param position the offset from the start of the stream. + * @throws IOException if an I/O error occurs. + */ + public void readFully(long position, byte[] buffer, int offset, int length) + throws IOException { + checkStream(); + IOUtils.readFully(input, position, buffer, offset, length); + if (length > 0) { + // This operation does not change the current offset of the file + decrypt(position, buffer, offset, length); + } } - inBuffer.flip(); - outBuffer.clear(); - decryptBuffer(state, inBuffer, outBuffer); - inBuffer.clear(); - outBuffer.flip(); - if (padding > 0) { - /* - * The plain text and cipher text have a 1:1 mapping, they start at the - * same position. - */ - outBuffer.position(padding); + + /** + * Reads the specified number of bytes from a given position within a + * stream. This does not change the current offset of the stream and is + * thread-safe. + * + * @param position the offset from the start of the stream. + * @param buffer the buffer into which the data is read. + * @throws IOException if an I/O error occurs. + */ + public void readFully(long position, byte[] buffer) throws IOException { + readFully(position, buffer, 0, buffer.length); } - } - - private void decryptBuffer(CipherState state, ByteBuffer inBuffer, ByteBuffer outBuffer) - throws IOException { - int inputSize = inBuffer.remaining(); - try { - int n = state.getCipher().update(inBuffer, outBuffer); - if (n < inputSize) { - /** - * Typically code will not get here. CryptoCipher#update will consume all - * input data and put result in outBuffer. - * CryptoCipher#doFinal will reset the cipher context. - */ - state.getCipher().doFinal(inBuffer, outBuffer); - state.reset(true); - } - } catch (ShortBufferException e) { - throw new IOException(e); - } catch (IllegalBlockSizeException e) { - throw new IOException(e); - } catch (BadPaddingException e) { - throw new IOException(e); + + /** + * Decrypts length bytes in buffer starting at offset. Output is also put + * into buffer starting at offset. It is thread-safe. + * + * @param buffer the buffer into which the data is read. + * @param offset the start offset in the data. + * @param position the offset from the start of the stream. + * @param length the maximum number of bytes to read. + * @throws IOException if an I/O error occurs. + */ + protected void decrypt(long position, byte[] buffer, int offset, int length) + throws IOException { + ByteBuffer inBuffer = getBuffer(); + ByteBuffer outBuffer = getBuffer(); + CipherState state = null; + try { + state = getCipherState(); + byte[] iv = getInitIV().clone(); + resetCipher(state, position, iv); + byte padding = getPadding(position); + inBuffer.position(padding); // Set proper position for input data. + + int n = 0; + while (n < length) { + int toDecrypt = Math.min(length - n, inBuffer.remaining()); + inBuffer.put(buffer, offset + n, toDecrypt); + + // Do decryption + decrypt(state, inBuffer, outBuffer, padding); + + outBuffer.get(buffer, offset + n, toDecrypt); + n += toDecrypt; + padding = postDecryption(state, inBuffer, position + n, iv); + } + } finally { + returnBuffer(inBuffer); + returnBuffer(outBuffer); + returnCipherState(state); + } } - } - - /** - * This method is executed immediately after decryption. Check whether - * cipher should be updated and recalculate padding if needed. - */ - private byte postDecryption(CipherState state, ByteBuffer inBuffer, - long position, byte[] iv) throws IOException { - byte padding = 0; - if (state.isReset()) { - /* - * This code is generally not executed since the cipher usually - * maintains cipher context (e.g. the counter) internally. However, - * some implementations can't maintain context so a re-init is necessary - * after each decryption call. - */ - resetCipher(state, position, iv); - padding = getPadding(position); - inBuffer.position(padding); + + /** + * Does the decryption using inBuffer as input and outBuffer as output. Upon + * return, inBuffer is cleared; the decrypted data starts at + * outBuffer.position() and ends at outBuffer.limit() + */ + private void decrypt(CipherState state, ByteBuffer inBuffer, + ByteBuffer outBuffer, byte padding) throws IOException { + Utils.checkState(inBuffer.position() >= padding); + if (inBuffer.position() == padding) { + // There is no real data in inBuffer. + return; + } + inBuffer.flip(); + outBuffer.clear(); + decryptBuffer(state, inBuffer, outBuffer); + inBuffer.clear(); + outBuffer.flip(); + if (padding > 0) { + /* + * The plain text and cipher text have a 1:1 mapping, they start at + * the same position. + */ + outBuffer.position(padding); + } } - return padding; - } - - /** Calculate the counter and iv, reset the cipher. */ - private void resetCipher(CipherState state, long position, byte[] iv) - throws IOException { - final long counter = getCounter(position); - Utils.calculateIV(getInitIV(), counter, iv); - try { - state.getCipher().init(CryptoCipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); - } catch (InvalidKeyException e) { - throw new IOException(e); - } catch (InvalidAlgorithmParameterException e) { - throw new IOException(e); + + private void decryptBuffer(CipherState state, ByteBuffer inBuffer, + ByteBuffer outBuffer) throws IOException { + int inputSize = inBuffer.remaining(); + try { + int n = state.getCipher().update(inBuffer, outBuffer); + if (n < inputSize) { + /** + * Typically code will not get here. CryptoCipher#update will + * consume all input data and put result in outBuffer. + * CryptoCipher#doFinal will reset the cipher context. + */ + state.getCipher().doFinal(inBuffer, outBuffer); + state.reset(true); + } + } catch (ShortBufferException e) { + throw new IOException(e); + } catch (IllegalBlockSizeException e) { + throw new IOException(e); + } catch (BadPaddingException e) { + throw new IOException(e); + } } - state.reset(false); - } - - /** Get CryptoCipher from pool */ - private CipherState getCipherState() throws IOException { - CipherState state = cipherPool.poll(); - if (state == null) { - CryptoCipher cipher; - try { - cipher = CryptoCipherFactory.getInstance(getCipher().getTransformation(), - getCipher().getProperties()); - } catch (GeneralSecurityException e) { - throw new IOException(e); - } - state = new CipherState(cipher); + + /** + * This method is executed immediately after decryption. Check whether + * cipher should be updated and recalculate padding if needed. + */ + private byte postDecryption(CipherState state, ByteBuffer inBuffer, + long position, byte[] iv) throws IOException { + byte padding = 0; + if (state.isReset()) { + /* + * This code is generally not executed since the cipher usually + * maintains cipher context (e.g. the counter) internally. However, + * some implementations can't maintain context so a re-init is + * necessary after each decryption call. + */ + resetCipher(state, position, iv); + padding = getPadding(position); + inBuffer.position(padding); + } + return padding; } - return state; - } + /** Calculate the counter and iv, reset the cipher. */ + private void resetCipher(CipherState state, long position, byte[] iv) + throws IOException { + final long counter = getCounter(position); + Utils.calculateIV(getInitIV(), counter, iv); + try { + state.getCipher().init(CryptoCipher.DECRYPT_MODE, key, + new IvParameterSpec(iv)); + } catch (InvalidKeyException e) { + throw new IOException(e); + } catch (InvalidAlgorithmParameterException e) { + throw new IOException(e); + } + state.reset(false); + } - /** Return CryptoCipher to pool */ - private void returnCipherState(CipherState state) { - if (state != null) { - cipherPool.add(state); + /** Get CryptoCipher from pool */ + private CipherState getCipherState() throws IOException { + CipherState state = cipherPool.poll(); + if (state == null) { + CryptoCipher cipher; + try { + cipher = CryptoCipherFactory.getInstance(getCipher() + .getTransformation(), getCipher().getProperties()); + } catch (GeneralSecurityException e) { + throw new IOException(e); + } + state = new CipherState(cipher); + } + + return state; } - } - /** Get direct buffer from pool */ - private ByteBuffer getBuffer() { - ByteBuffer buffer = bufferPool.poll(); - if (buffer == null) { - buffer = ByteBuffer.allocateDirect(getBufferSize()); + /** Return CryptoCipher to pool */ + private void returnCipherState(CipherState state) { + if (state != null) { + cipherPool.add(state); + } } - return buffer; - } + /** Get direct buffer from pool */ + private ByteBuffer getBuffer() { + ByteBuffer buffer = bufferPool.poll(); + if (buffer == null) { + buffer = ByteBuffer.allocateDirect(getBufferSize()); + } - /** Return direct buffer to pool */ - private void returnBuffer(ByteBuffer buf) { - if (buf != null) { - buf.clear(); - bufferPool.add(buf); + return buffer; } - } - - /** - * Overrides the {@link CryptoInputStream#close()}. - * Closes this input stream and releases any system resources associated - * with the stream. - * - * @throws IOException if an I/O error occurs. - */ - @Override - public void close() throws IOException { - if (!isOpen()) { - return; + + /** Return direct buffer to pool */ + private void returnBuffer(ByteBuffer buf) { + if (buf != null) { + buf.clear(); + bufferPool.add(buf); + } } - cleanBufferPool(); - super.close(); - } + /** + * Overrides the {@link CryptoInputStream#close()}. Closes this input stream + * and releases any system resources associated with the stream. + * + * @throws IOException if an I/O error occurs. + */ + @Override + public void close() throws IOException { + if (!isOpen()) { + return; + } + + cleanBufferPool(); + super.close(); + } - /** Clean direct buffer pool */ - private void cleanBufferPool() { - ByteBuffer buf; - while ((buf = bufferPool.poll()) != null) { - Utils.freeDirectBuffer(buf); + /** Clean direct buffer pool */ + private void cleanBufferPool() { + ByteBuffer buf; + while ((buf = bufferPool.poll()) != null) { + Utils.freeDirectBuffer(buf); + } } - } - private class CipherState { - private CryptoCipher cipher; - private boolean reset; + private class CipherState { + private CryptoCipher cipher; + private boolean reset; - public CipherState(CryptoCipher cipher) { - this.cipher = cipher; - this.reset = false; - } + public CipherState(CryptoCipher cipher) { + this.cipher = cipher; + this.reset = false; + } - public CryptoCipher getCipher() { - return cipher; - } + public CryptoCipher getCipher() { + return cipher; + } - public boolean isReset() { - return reset; - } + public boolean isReset() { + return reset; + } - public void reset(boolean reset) { - this.reset = reset; + public void reset(boolean reset) { + this.reset = reset; + } } - } }
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/95a8d486/src/main/java/org/apache/commons/crypto/stream/input/ChannelInput.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/stream/input/ChannelInput.java b/src/main/java/org/apache/commons/crypto/stream/input/ChannelInput.java index cdf13e6..dbcba57 100644 --- a/src/main/java/org/apache/commons/crypto/stream/input/ChannelInput.java +++ b/src/main/java/org/apache/commons/crypto/stream/input/ChannelInput.java @@ -23,143 +23,148 @@ import java.nio.channels.ReadableByteChannel; /** * The ChannelInput class takes a <code>ReadableByteChannel</code> object and - * wraps it as <code>Input</code> object acceptable by <code>CryptoInputStream</code>. + * wraps it as <code>Input</code> object acceptable by + * <code>CryptoInputStream</code>. */ public class ChannelInput implements Input { - private static final int SKIP_BUFFER_SIZE = 2048; + private static final int SKIP_BUFFER_SIZE = 2048; - private ByteBuffer buf; - private final ReadableByteChannel channel; + private ByteBuffer buf; + private final ReadableByteChannel channel; - /** - * Constructs the {@link org.apache.commons.crypto.stream.input.ChannelInput}. - * - * @param channel the ReadableByteChannel object. - */ - public ChannelInput( - ReadableByteChannel channel) { - this.channel = channel; - } + /** + * Constructs the + * {@link org.apache.commons.crypto.stream.input.ChannelInput}. + * + * @param channel the ReadableByteChannel object. + */ + public ChannelInput(ReadableByteChannel channel) { + this.channel = channel; + } - /** - * Overrides the {@link org.apache.commons.crypto.stream.input.Input#read(ByteBuffer)}. - * Reads a sequence of bytes from input into the given buffer. - * - * @param dst The buffer into which bytes are to be transferred. - * @return the total number of bytes read into the buffer, or - * <code>-1</code> if there is no more data because the end of - * the stream has been reached. - * @throws IOException if an I/O error occurs. - */ - @Override - public int read(ByteBuffer dst) throws IOException { - return channel.read(dst); - } + /** + * Overrides the + * {@link org.apache.commons.crypto.stream.input.Input#read(ByteBuffer)}. + * Reads a sequence of bytes from input into the given buffer. + * + * @param dst The buffer into which bytes are to be transferred. + * @return the total number of bytes read into the buffer, or + * <code>-1</code> if there is no more data because the end of the + * stream has been reached. + * @throws IOException if an I/O error occurs. + */ + @Override + public int read(ByteBuffer dst) throws IOException { + return channel.read(dst); + } - /** - * Overrides the {@link org.apache.commons.crypto.stream.input.Input#skip(long)}. - * Skips over and discards <code>n</code> bytes of data from this input - * stream. - * - * @param n the number of bytes to be skipped. - * @return the actual number of bytes skipped. - * @throws IOException if an I/O error occurs. - */ - @Override - public long skip(long n) throws IOException { - long remaining = n; - int nr; + /** + * Overrides the + * {@link org.apache.commons.crypto.stream.input.Input#skip(long)}. Skips + * over and discards <code>n</code> bytes of data from this input stream. + * + * @param n the number of bytes to be skipped. + * @return the actual number of bytes skipped. + * @throws IOException if an I/O error occurs. + */ + @Override + public long skip(long n) throws IOException { + long remaining = n; + int nr; - if (n <= 0) { - return 0; - } + if (n <= 0) { + return 0; + } - int size = (int)Math.min(SKIP_BUFFER_SIZE, remaining); - ByteBuffer skipBuffer = getSkipBuf(); - while (remaining > 0) { - skipBuffer.clear(); - skipBuffer.limit((int)Math.min(size, remaining)); - nr = read(skipBuffer); - if (nr < 0) { - break; - } - remaining -= nr; - } + int size = (int) Math.min(SKIP_BUFFER_SIZE, remaining); + ByteBuffer skipBuffer = getSkipBuf(); + while (remaining > 0) { + skipBuffer.clear(); + skipBuffer.limit((int) Math.min(size, remaining)); + nr = read(skipBuffer); + if (nr < 0) { + break; + } + remaining -= nr; + } - return n - remaining; - } + return n - remaining; + } - /** - * Overrides the {@link Input#available()}. - * Returns an estimate of the number of bytes that can be read (or - * skipped over) from this input stream without blocking by the next - * invocation of a method for this input stream. The next invocation - * might be the same thread or another thread. A single read or skip of this - * many bytes will not block, but may read or skip fewer bytes. - * - * @return an estimate of the number of bytes that can be read (or skipped - * over) from this input stream without blocking or {@code 0} when - * it reaches the end of the input stream. - * @throws IOException if an I/O error occurs. - */ - @Override - public int available() throws IOException { - return 0; - } + /** + * Overrides the {@link Input#available()}. Returns an estimate of the + * number of bytes that can be read (or skipped over) from this input stream + * without blocking by the next invocation of a method for this input + * stream. The next invocation might be the same thread or another thread. A + * single read or skip of this many bytes will not block, but may read or + * skip fewer bytes. + * + * @return an estimate of the number of bytes that can be read (or skipped + * over) from this input stream without blocking or {@code 0} when + * it reaches the end of the input stream. + * @throws IOException if an I/O error occurs. + */ + @Override + public int available() throws IOException { + return 0; + } - /** - * Overrides the {@link org.apache.commons.crypto.stream.input.Input#read(long, byte[], int, int)}. - * Reads up to <code>len</code> bytes of data from the input stream into - * an array of bytes. An attempt is made to read as many as - * <code>len</code> bytes, but a smaller number may be read. - * The number of bytes actually read is returned as an integer. - * - * @param position the given position within a stream. - * @param buffer the buffer into which the data is read. - * @param offset the start offset in array buffer. - * @param length the maximum number of bytes to read. - * @return the total number of bytes read into the buffer, or - * <code>-1</code> if there is no more data because the end of - * the stream has been reached. - * @throws IOException if an I/O error occurs. - */ - @Override - public int read(long position, byte[] buffer, int offset, int length) - throws IOException { - throw new UnsupportedOperationException( - "Positioned read is not supported by this implementation"); - } + /** + * Overrides the + * {@link org.apache.commons.crypto.stream.input.Input#read(long, byte[], int, int)} + * . Reads up to <code>len</code> bytes of data from the input stream into + * an array of bytes. An attempt is made to read as many as <code>len</code> + * bytes, but a smaller number may be read. The number of bytes actually + * read is returned as an integer. + * + * @param position the given position within a stream. + * @param buffer the buffer into which the data is read. + * @param offset the start offset in array buffer. + * @param length the maximum number of bytes to read. + * @return the total number of bytes read into the buffer, or + * <code>-1</code> if there is no more data because the end of the + * stream has been reached. + * @throws IOException if an I/O error occurs. + */ + @Override + public int read(long position, byte[] buffer, int offset, int length) + throws IOException { + throw new UnsupportedOperationException( + "Positioned read is not supported by this implementation"); + } - /** - * Overrides the {@link org.apache.commons.crypto.stream.input.Input#seek(long)}. - * Seeks to the given offset from the start of the stream. - * The next read() will be from that location. - * - * @param position the offset from the start of the stream. - * @throws IOException if an I/O error occurs. - */ - @Override - public void seek(long position) throws IOException { - throw new UnsupportedOperationException( - "Seek is not supported by this implementation"); - } + /** + * Overrides the + * {@link org.apache.commons.crypto.stream.input.Input#seek(long)}. Seeks to + * the given offset from the start of the stream. The next read() will be + * from that location. + * + * @param position the offset from the start of the stream. + * @throws IOException if an I/O error occurs. + */ + @Override + public void seek(long position) throws IOException { + throw new UnsupportedOperationException( + "Seek is not supported by this implementation"); + } - /** - * Overrides the {@link org.apache.commons.crypto.stream.input.Input#seek(long)}. - * Closes this input and releases any system resources associated - * with the under layer input. - * - * @throws IOException if an I/O error occurs. - */ - @Override - public void close() throws IOException { - channel.close(); - } + /** + * Overrides the + * {@link org.apache.commons.crypto.stream.input.Input#seek(long)}. Closes + * this input and releases any system resources associated with the under + * layer input. + * + * @throws IOException if an I/O error occurs. + */ + @Override + public void close() throws IOException { + channel.close(); + } - private ByteBuffer getSkipBuf() { - if (buf == null) { - buf = ByteBuffer.allocate(SKIP_BUFFER_SIZE); + private ByteBuffer getSkipBuf() { + if (buf == null) { + buf = ByteBuffer.allocate(SKIP_BUFFER_SIZE); + } + return buf; } - return buf; - } } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/95a8d486/src/main/java/org/apache/commons/crypto/stream/input/Input.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/stream/input/Input.java b/src/main/java/org/apache/commons/crypto/stream/input/Input.java index 1657a7c..c8e2f63 100644 --- a/src/main/java/org/apache/commons/crypto/stream/input/Input.java +++ b/src/main/java/org/apache/commons/crypto/stream/input/Input.java @@ -21,113 +21,117 @@ import java.io.IOException; import java.nio.ByteBuffer; /** - * The Input interface abstract the input source of <code>CryptoInputStream</code> so that - * different implementation of input can be used. The implementation Input interface will usually - * wraps an input mechanism such as <code>InputStream</code> or <code>ReadableByteChannel</code>. + * The Input interface abstract the input source of + * <code>CryptoInputStream</code> so that different implementation of input can + * be used. The implementation Input interface will usually wraps an input + * mechanism such as <code>InputStream</code> or + * <code>ReadableByteChannel</code>. */ public interface Input { - /** - * Reads a sequence of bytes from input into the given buffer. - * - * <p> An attempt is made to read up to <i>r</i> bytes from the input, - * where <i>r</i> is the number of bytes remaining in the buffer, that is, - * <tt>dst.remaining()</tt>, at the moment this method is invoked. - * - * <p> Suppose that a byte sequence of length <i>n</i> is read, where - * <tt>0</tt> <tt><=</tt> <i>n</i> <tt><=</tt> <i>r</i>. - * This byte sequence will be transferred into the buffer so that the first - * byte in the sequence is at index <i>p</i> and the last byte is at index - * <i>p</i> <tt>+</tt> <i>n</i> <tt>-</tt> <tt>1</tt>, - * where <i>p</i> is the buffer's position at the moment this method is - * invoked. Upon return the buffer's position will be equal to - * <i>p</i> <tt>+</tt> <i>n</i>; its limit will not have changed. - * - * @param dst - * The buffer into which bytes are to be transferred. - * @return the total number of bytes read into the buffer, or - * <code>-1</code> if there is no more data because the end of - * the stream has been reached. - * @throws IOException - * If some other I/O error occurs. - */ - int read(ByteBuffer dst) throws IOException; + /** + * Reads a sequence of bytes from input into the given buffer. + * + * <p> + * An attempt is made to read up to <i>r</i> bytes from the input, where + * <i>r</i> is the number of bytes remaining in the buffer, that is, + * <tt>dst.remaining()</tt>, at the moment this method is invoked. + * + * <p> + * Suppose that a byte sequence of length <i>n</i> is read, where <tt>0</tt> + * <tt><=</tt> <i>n</i> <tt><=</tt> <i>r</i>. + * This byte sequence will be transferred into the buffer so that the first + * byte in the sequence is at index <i>p</i> and the last byte is at index + * <i>p</i> <tt>+</tt> <i>n</i> <tt>-</tt> <tt>1</tt>, + * where <i>p</i> is the buffer's position at the moment this method is + * invoked. Upon return the buffer's position will be equal to + * <i>p</i> <tt>+</tt> <i>n</i>; its limit will not have changed. + * + * @param dst The buffer into which bytes are to be transferred. + * @return the total number of bytes read into the buffer, or + * <code>-1</code> if there is no more data because the end of the + * stream has been reached. + * @throws IOException If some other I/O error occurs. + */ + int read(ByteBuffer dst) throws IOException; - /** - * Skips over and discards <code>n</code> bytes of data from this input - * The <code>skip</code> method may, for a variety of reasons, end - * up skipping over some smaller number of bytes, possibly <code>0</code>. - * This may result from any of a number of conditions; reaching end of file - * before <code>n</code> bytes have been skipped is only one possibility. - * The actual number of bytes skipped is returned. If <code>n</code> is - * negative, no bytes are skipped. - * - * <p> The <code>skip</code> method of this class creates a - * byte array and then repeatedly reads into it until <code>n</code> bytes - * have been read or the end of the stream has been reached. Subclasses are - * encouraged to provide a more efficient implementation of this method. - * For instance, the implementation may depend on the ability to seek. - * - * @param n the number of bytes to be skipped. - * @return the actual number of bytes skipped. - * @exception IOException if the stream does not support seek, - * or if some other I/O error occurs. - */ - long skip(long n) throws IOException; + /** + * Skips over and discards <code>n</code> bytes of data from this input The + * <code>skip</code> method may, for a variety of reasons, end up skipping + * over some smaller number of bytes, possibly <code>0</code>. This may + * result from any of a number of conditions; reaching end of file before + * <code>n</code> bytes have been skipped is only one possibility. The + * actual number of bytes skipped is returned. If <code>n</code> is + * negative, no bytes are skipped. + * + * <p> + * The <code>skip</code> method of this class creates a byte array and then + * repeatedly reads into it until <code>n</code> bytes have been read or the + * end of the stream has been reached. Subclasses are encouraged to provide + * a more efficient implementation of this method. For instance, the + * implementation may depend on the ability to seek. + * + * @param n the number of bytes to be skipped. + * @return the actual number of bytes skipped. + * @exception IOException if the stream does not support seek, or if some + * other I/O error occurs. + */ + long skip(long n) throws IOException; - /** - * Returns an estimate of the number of bytes that can be read (or - * skipped over) from this input without blocking by the next - * invocation of a method for this input stream. The next invocation - * might be the same thread or another thread. A single read or skip of this - * many bytes will not block, but may read or skip fewer bytes. - * - * <p> It is never correct to use the return value of this method to allocate - * a buffer intended to hold all data in this stream. - * - * @return an estimate of the number of bytes that can be read (or skipped - * over) from this input stream without blocking or {@code 0} when - * it reaches the end of the input stream. - * @exception IOException if an I/O error occurs. - */ - int available() throws IOException; + /** + * Returns an estimate of the number of bytes that can be read (or skipped + * over) from this input without blocking by the next invocation of a method + * for this input stream. The next invocation might be the same thread or + * another thread. A single read or skip of this many bytes will not block, + * but may read or skip fewer bytes. + * + * <p> + * It is never correct to use the return value of this method to allocate a + * buffer intended to hold all data in this stream. + * + * @return an estimate of the number of bytes that can be read (or skipped + * over) from this input stream without blocking or {@code 0} when + * it reaches the end of the input stream. + * @exception IOException if an I/O error occurs. + */ + int available() throws IOException; - /** - * Reads up to the specified number of bytes from a given position within a - * stream and return the number of bytes read. - * This does not change the current offset of the stream and is thread-safe. - * - * An implementation may not support positioned read. If the implementation - * doesn't support positioned read, it throws UnsupportedOperationException. - * - * @param position the given position within a stream. - * @param buffer the buffer into which the data is read. - * @param offset the start offset in array buffer. - * @param length the maximum number of bytes to read. - * @return the total number of bytes read into the buffer, or - * <code>-1</code> if there is no more data because the end of - * the stream has been reached. - * @throws IOException if an I/O error occurs. - */ - int read(long position, byte[] buffer, int offset, int length) - throws IOException; + /** + * Reads up to the specified number of bytes from a given position within a + * stream and return the number of bytes read. This does not change the + * current offset of the stream and is thread-safe. + * + * An implementation may not support positioned read. If the implementation + * doesn't support positioned read, it throws UnsupportedOperationException. + * + * @param position the given position within a stream. + * @param buffer the buffer into which the data is read. + * @param offset the start offset in array buffer. + * @param length the maximum number of bytes to read. + * @return the total number of bytes read into the buffer, or + * <code>-1</code> if there is no more data because the end of the + * stream has been reached. + * @throws IOException if an I/O error occurs. + */ + int read(long position, byte[] buffer, int offset, int length) + throws IOException; - /** - * Seeks to the given offset from the start of the stream. - * The next read() will be from that location. - * - * An implementation may not support seek. If the implementation - * doesn't support seek, it throws UnsupportedOperationException. - * - * @param position the offset from the start of the stream. - * @throws IOException if an I/O error occurs. - */ - void seek(long position) throws IOException; + /** + * Seeks to the given offset from the start of the stream. The next read() + * will be from that location. + * + * An implementation may not support seek. If the implementation doesn't + * support seek, it throws UnsupportedOperationException. + * + * @param position the offset from the start of the stream. + * @throws IOException if an I/O error occurs. + */ + void seek(long position) throws IOException; - /** - * Closes this input and releases any system resources associated - * with the under layer input. - * - * @exception IOException if an I/O error occurs. - */ - void close() throws IOException; + /** + * Closes this input and releases any system resources associated with the + * under layer input. + * + * @exception IOException if an I/O error occurs. + */ + void close() throws IOException; } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/95a8d486/src/main/java/org/apache/commons/crypto/stream/input/StreamInput.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/stream/input/StreamInput.java b/src/main/java/org/apache/commons/crypto/stream/input/StreamInput.java index 20649de..cbbcb9a 100644 --- a/src/main/java/org/apache/commons/crypto/stream/input/StreamInput.java +++ b/src/main/java/org/apache/commons/crypto/stream/input/StreamInput.java @@ -22,136 +22,139 @@ import java.io.InputStream; import java.nio.ByteBuffer; /** - * The StreamInput class takes a <code>InputStream</code> object and - * wraps it as <code>Input</code> object acceptable by <code>CryptoInputStream</code>. + * The StreamInput class takes a <code>InputStream</code> object and wraps it as + * <code>Input</code> object acceptable by <code>CryptoInputStream</code>. */ public class StreamInput implements Input { - private final byte[] buf; - private final int bufferSize; - final InputStream in; + private final byte[] buf; + private final int bufferSize; + final InputStream in; - /** - * Constructs a {@link org.apache.commons.crypto.stream.input.StreamInput}. - * - * @param inputStream the inputstream object. - * @param bufferSize the buffersize. - */ - public StreamInput(InputStream inputStream, int bufferSize) { - this.in = inputStream; - this.bufferSize = bufferSize; - buf = new byte[bufferSize]; - } + /** + * Constructs a {@link org.apache.commons.crypto.stream.input.StreamInput}. + * + * @param inputStream the inputstream object. + * @param bufferSize the buffersize. + */ + public StreamInput(InputStream inputStream, int bufferSize) { + this.in = inputStream; + this.bufferSize = bufferSize; + buf = new byte[bufferSize]; + } - /** - * Overrides the {@link org.apache.commons.crypto.stream.input.Input#read(ByteBuffer)}. - * Reads a sequence of bytes from input into the given buffer. - * - * @param dst - * The buffer into which bytes are to be transferred. - * - * @return the total number of bytes read into the buffer, or - * <code>-1</code> if there is no more data because the end of - * the stream has been reached. - * @throws IOException if an I/O error occurs. - */ - @Override - public int read(ByteBuffer dst) throws IOException { - int remaining = dst.remaining(); - int read = 0; - while (remaining > 0) { - final int n = in.read(buf, 0, Math.min(remaining, bufferSize)); - if (n == -1) { - if (read == 0) { - read = -1; + /** + * Overrides the + * {@link org.apache.commons.crypto.stream.input.Input#read(ByteBuffer)}. + * Reads a sequence of bytes from input into the given buffer. + * + * @param dst The buffer into which bytes are to be transferred. + * + * @return the total number of bytes read into the buffer, or + * <code>-1</code> if there is no more data because the end of the + * stream has been reached. + * @throws IOException if an I/O error occurs. + */ + @Override + public int read(ByteBuffer dst) throws IOException { + int remaining = dst.remaining(); + int read = 0; + while (remaining > 0) { + final int n = in.read(buf, 0, Math.min(remaining, bufferSize)); + if (n == -1) { + if (read == 0) { + read = -1; + } + break; + } else if (n > 0) { + dst.put(buf, 0, n); + read += n; + remaining -= n; + } } - break; - } else if (n > 0) { - dst.put(buf, 0, n); - read += n; - remaining -= n; - } + return read; } - return read; - } - /** - * Overrides the {@link org.apache.commons.crypto.stream.input.Input#skip(long)}. - * Skips over and discards <code>n</code> bytes of data from this input - * stream. - * - * @param n the number of bytes to be skipped. - * @return the actual number of bytes skipped. - * @throws IOException if an I/O error occurs. - */ - @Override - public long skip(long n) throws IOException { - return in.skip(n); - } + /** + * Overrides the + * {@link org.apache.commons.crypto.stream.input.Input#skip(long)}. Skips + * over and discards <code>n</code> bytes of data from this input stream. + * + * @param n the number of bytes to be skipped. + * @return the actual number of bytes skipped. + * @throws IOException if an I/O error occurs. + */ + @Override + public long skip(long n) throws IOException { + return in.skip(n); + } - /** - * Overrides the {@link Input#available()}. - * Returns an estimate of the number of bytes that can be read (or - * skipped over) from this input stream without blocking by the next - * invocation of a method for this input stream. The next invocation - * might be the same thread or another thread. A single read or skip of this - * many bytes will not block, but may read or skip fewer bytes. - * - * @return an estimate of the number of bytes that can be read (or skipped - * over) from this input stream without blocking or {@code 0} when - * it reaches the end of the input stream. - * @throws IOException if an I/O error occurs. - */ - @Override - public int available() throws IOException { - return in.available(); - } + /** + * Overrides the {@link Input#available()}. Returns an estimate of the + * number of bytes that can be read (or skipped over) from this input stream + * without blocking by the next invocation of a method for this input + * stream. The next invocation might be the same thread or another thread. A + * single read or skip of this many bytes will not block, but may read or + * skip fewer bytes. + * + * @return an estimate of the number of bytes that can be read (or skipped + * over) from this input stream without blocking or {@code 0} when + * it reaches the end of the input stream. + * @throws IOException if an I/O error occurs. + */ + @Override + public int available() throws IOException { + return in.available(); + } - /** - * Overrides the {@link org.apache.commons.crypto.stream.input.Input#read(long, byte[], int, int)}. - * Reads up to <code>len</code> bytes of data from the input stream into - * an array of bytes. An attempt is made to read as many as - * <code>len</code> bytes, but a smaller number may be read. - * The number of bytes actually read is returned as an integer. - * - * @param position the given position within a stream. - * @param buffer the buffer into which the data is read. - * @param offset the start offset in array buffer. - * @param length the maximum number of bytes to read. - * @return the total number of bytes read into the buffer, or - * <code>-1</code> if there is no more data because the end of - * the stream has been reached. - * @throws IOException if an I/O error occurs. - */ - @Override - public int read(long position, byte[] buffer, int offset, int length) - throws IOException { - throw new UnsupportedOperationException( - "Positioned read is not supported by this implementation"); - } + /** + * Overrides the + * {@link org.apache.commons.crypto.stream.input.Input#read(long, byte[], int, int)} + * . Reads up to <code>len</code> bytes of data from the input stream into + * an array of bytes. An attempt is made to read as many as <code>len</code> + * bytes, but a smaller number may be read. The number of bytes actually + * read is returned as an integer. + * + * @param position the given position within a stream. + * @param buffer the buffer into which the data is read. + * @param offset the start offset in array buffer. + * @param length the maximum number of bytes to read. + * @return the total number of bytes read into the buffer, or + * <code>-1</code> if there is no more data because the end of the + * stream has been reached. + * @throws IOException if an I/O error occurs. + */ + @Override + public int read(long position, byte[] buffer, int offset, int length) + throws IOException { + throw new UnsupportedOperationException( + "Positioned read is not supported by this implementation"); + } - /** - * Overrides the {@link org.apache.commons.crypto.stream.input.Input#seek(long)}. - * Seeks to the given offset from the start of the stream. - * The next read() will be from that location. - * - * @param position the offset from the start of the stream. - * @throws IOException if an I/O error occurs. - */ - @Override - public void seek(long position) throws IOException { - throw new UnsupportedOperationException( - "Seek is not supported by this implementation"); - } + /** + * Overrides the + * {@link org.apache.commons.crypto.stream.input.Input#seek(long)}. Seeks to + * the given offset from the start of the stream. The next read() will be + * from that location. + * + * @param position the offset from the start of the stream. + * @throws IOException if an I/O error occurs. + */ + @Override + public void seek(long position) throws IOException { + throw new UnsupportedOperationException( + "Seek is not supported by this implementation"); + } - /** - * Overrides the {@link org.apache.commons.crypto.stream.input.Input#seek(long)}. - * Closes this input and releases any system resources associated - * with the under layer input. - * - * @throws IOException if an I/O error occurs. - */ - @Override - public void close() throws IOException { - in.close(); - } + /** + * Overrides the + * {@link org.apache.commons.crypto.stream.input.Input#seek(long)}. Closes + * this input and releases any system resources associated with the under + * layer input. + * + * @throws IOException if an I/O error occurs. + */ + @Override + public void close() throws IOException { + in.close(); + } } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/95a8d486/src/main/java/org/apache/commons/crypto/stream/input/package-info.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/stream/input/package-info.java b/src/main/java/org/apache/commons/crypto/stream/input/package-info.java index 66e7af5..877eba0 100644 --- a/src/main/java/org/apache/commons/crypto/stream/input/package-info.java +++ b/src/main/java/org/apache/commons/crypto/stream/input/package-info.java @@ -19,3 +19,4 @@ * Input classes */ package org.apache.commons.crypto.stream.input; + http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/95a8d486/src/main/java/org/apache/commons/crypto/stream/output/ChannelOutput.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/stream/output/ChannelOutput.java b/src/main/java/org/apache/commons/crypto/stream/output/ChannelOutput.java index 9805484..a28a21e 100644 --- a/src/main/java/org/apache/commons/crypto/stream/output/ChannelOutput.java +++ b/src/main/java/org/apache/commons/crypto/stream/output/ChannelOutput.java @@ -22,57 +22,58 @@ import java.nio.ByteBuffer; import java.nio.channels.WritableByteChannel; /** - * The ChannelOutput class takes a <code>WritableByteChannel</code> object and wraps it as - * <code>Output</code> object acceptable by <code>CryptoOutputStream</code> as the output target. + * The ChannelOutput class takes a <code>WritableByteChannel</code> object and + * wraps it as <code>Output</code> object acceptable by + * <code>CryptoOutputStream</code> as the output target. */ public class ChannelOutput implements Output { - private WritableByteChannel channel; + private WritableByteChannel channel; - /** - * Constructs a {@link org.apache.commons.crypto.stream.output.ChannelOutput}. - * - * @param channel the WritableByteChannel object. - */ - public ChannelOutput(WritableByteChannel channel) { - this.channel = channel; - } + /** + * Constructs a + * {@link org.apache.commons.crypto.stream.output.ChannelOutput}. + * + * @param channel the WritableByteChannel object. + */ + public ChannelOutput(WritableByteChannel channel) { + this.channel = channel; + } - /** - * Overrides the {@link org.apache.commons.crypto.stream.output.Output#write(ByteBuffer)}. - * Writes a sequence of bytes to this output from the given buffer. - * - * @param src - * The buffer from which bytes are to be retrieved. - * - * @return The number of bytes written, possibly zero. - * @throws IOException if an I/O error occurs. - */ - @Override - public int write(ByteBuffer src) throws IOException { - return channel.write(src); - } + /** + * Overrides the + * {@link org.apache.commons.crypto.stream.output.Output#write(ByteBuffer)}. + * Writes a sequence of bytes to this output from the given buffer. + * + * @param src The buffer from which bytes are to be retrieved. + * + * @return The number of bytes written, possibly zero. + * @throws IOException if an I/O error occurs. + */ + @Override + public int write(ByteBuffer src) throws IOException { + return channel.write(src); + } - /** - * Overrides the {@link Output#flush()}. - * Flushes this output and forces any buffered output bytes - * to be written out if the under layer output method support. - * - * @throws IOException if an I/O error occurs. - */ - @Override - public void flush() throws IOException { - } + /** + * Overrides the {@link Output#flush()}. Flushes this output and forces any + * buffered output bytes to be written out if the under layer output method + * support. + * + * @throws IOException if an I/O error occurs. + */ + @Override + public void flush() throws IOException { + } - /** - * Overrides the {@link Output#close()}. - * Closes this output and releases any system resources associated - * with the under layer output. - * - * @throws IOException if an I/O error occurs. - */ - @Override - public void close() throws IOException { - channel.close(); - } + /** + * Overrides the {@link Output#close()}. Closes this output and releases any + * system resources associated with the under layer output. + * + * @throws IOException if an I/O error occurs. + */ + @Override + public void close() throws IOException { + channel.close(); + } } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/95a8d486/src/main/java/org/apache/commons/crypto/stream/output/Output.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/stream/output/Output.java b/src/main/java/org/apache/commons/crypto/stream/output/Output.java index 34f1610..5dbb835 100644 --- a/src/main/java/org/apache/commons/crypto/stream/output/Output.java +++ b/src/main/java/org/apache/commons/crypto/stream/output/Output.java @@ -21,56 +21,57 @@ import java.io.IOException; import java.nio.ByteBuffer; /** - * The Output interface abstract the output target of <code>CryptoOutputStream</code> so that - * different implementation of output can be used. The implementation Output interface will usually - * wraps an output mechanism such as <code>OutputStream</code> or <code>WritableByteChannel</code>. + * The Output interface abstract the output target of + * <code>CryptoOutputStream</code> so that different implementation of output + * can be used. The implementation Output interface will usually wraps an output + * mechanism such as <code>OutputStream</code> or + * <code>WritableByteChannel</code>. */ public interface Output { - /** - * Writes a sequence of bytes to this output from the given buffer. - * - * <p> An attempt is made to write up to <i>r</i> bytes to the channel, - * where <i>r</i> is the number of bytes remaining in the buffer, that is, - * <tt>src.remaining()</tt>, at the moment this method is invoked. - * - * <p> Suppose that a byte sequence of length <i>n</i> is written, where - * <tt>0</tt> <tt><=</tt> <i>n</i> <tt><=</tt> <i>r</i>. - * This byte sequence will be transferred from the buffer starting at index - * <i>p</i>, where <i>p</i> is the buffer's position at the moment this - * method is invoked; the index of the last byte written will be - * <i>p</i> <tt>+</tt> <i>n</i> <tt>-</tt> <tt>1</tt>. - * Upon return the buffer's position will be equal to - * <i>p</i> <tt>+</tt> <i>n</i>; its limit will not have changed. - * - * @param src - * The buffer from which bytes are to be retrieved. - * - * @return The number of bytes written, possibly zero. - * - * @throws IOException - * If some other I/O error occurs. - */ - int write(ByteBuffer src) throws IOException; + /** + * Writes a sequence of bytes to this output from the given buffer. + * + * <p> + * An attempt is made to write up to <i>r</i> bytes to the channel, where + * <i>r</i> is the number of bytes remaining in the buffer, that is, + * <tt>src.remaining()</tt>, at the moment this method is invoked. + * + * <p> + * Suppose that a byte sequence of length <i>n</i> is written, where + * <tt>0</tt> <tt><=</tt> <i>n</i> <tt><=</tt> + * <i>r</i>. This byte sequence will be transferred from the buffer + * starting at index <i>p</i>, where <i>p</i> is the buffer's position at + * the moment this method is invoked; the index of the last byte written + * will be <i>p</i> <tt>+</tt> <i>n</i> <tt>-</tt> + * <tt>1</tt>. Upon return the buffer's position will be equal to + * <i>p</i> <tt>+</tt> <i>n</i>; its limit will not have changed. + * + * @param src The buffer from which bytes are to be retrieved. + * + * @return The number of bytes written, possibly zero. + * + * @throws IOException If some other I/O error occurs. + */ + int write(ByteBuffer src) throws IOException; - /** - * Flushes this output and forces any buffered output bytes - * to be written out if the under layer output method support. - * The general contract of <code>flush</code> is - * that calling it is an indication that, if any bytes previously - * written have been buffered by the implementation of the output - * stream, such bytes should immediately be written to their - * intended destination. - * - * @throws IOException if an I/O error occurs. - */ - void flush() throws IOException; + /** + * Flushes this output and forces any buffered output bytes to be written + * out if the under layer output method support. The general contract of + * <code>flush</code> is that calling it is an indication that, if any bytes + * previously written have been buffered by the implementation of the output + * stream, such bytes should immediately be written to their intended + * destination. + * + * @throws IOException if an I/O error occurs. + */ + void flush() throws IOException; - /** - * Closes this output and releases any system resources associated - * with the under layer output. - * - * @throws IOException if an I/O error occurs. - */ - void close() throws IOException; + /** + * Closes this output and releases any system resources associated with the + * under layer output. + * + * @throws IOException if an I/O error occurs. + */ + void close() throws IOException; } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/95a8d486/src/main/java/org/apache/commons/crypto/stream/output/StreamOutput.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/stream/output/StreamOutput.java b/src/main/java/org/apache/commons/crypto/stream/output/StreamOutput.java index 8c0e4d7..78c9bab 100644 --- a/src/main/java/org/apache/commons/crypto/stream/output/StreamOutput.java +++ b/src/main/java/org/apache/commons/crypto/stream/output/StreamOutput.java @@ -22,76 +22,77 @@ import java.io.OutputStream; import java.nio.ByteBuffer; /** - * The StreamOutput class takes a <code>OutputStream</code> object and wraps it as - * <code>Output</code> object acceptable by <code>CryptoOutputStream</code> as the output target. + * The StreamOutput class takes a <code>OutputStream</code> object and wraps it + * as <code>Output</code> object acceptable by <code>CryptoOutputStream</code> + * as the output target. */ public class StreamOutput implements Output { - private final byte[] buf; - private final int bufferSize; - private final OutputStream out; + private final byte[] buf; + private final int bufferSize; + private final OutputStream out; - /** - * Constructs a {@link org.apache.commons.crypto.stream.output.StreamOutput}. - * - * @param out the OutputStream object. - * @param bufferSize the buffersize. - */ - public StreamOutput(OutputStream out, int bufferSize) { - this.out = out; - this.bufferSize = bufferSize; - buf = new byte[bufferSize]; - } + /** + * Constructs a {@link org.apache.commons.crypto.stream.output.StreamOutput} + * . + * + * @param out the OutputStream object. + * @param bufferSize the buffersize. + */ + public StreamOutput(OutputStream out, int bufferSize) { + this.out = out; + this.bufferSize = bufferSize; + buf = new byte[bufferSize]; + } - /** - * Overrides the {@link org.apache.commons.crypto.stream.output.Output#write(ByteBuffer)}. - * Writes a sequence of bytes to this output from the given buffer. - * - * @param src - * The buffer from which bytes are to be retrieved. - * - * @return The number of bytes written, possibly zero. - * @throws IOException if an I/O error occurs. - */ - @Override - public int write(ByteBuffer src) throws IOException { - final int len = src.remaining(); + /** + * Overrides the + * {@link org.apache.commons.crypto.stream.output.Output#write(ByteBuffer)}. + * Writes a sequence of bytes to this output from the given buffer. + * + * @param src The buffer from which bytes are to be retrieved. + * + * @return The number of bytes written, possibly zero. + * @throws IOException if an I/O error occurs. + */ + @Override + public int write(ByteBuffer src) throws IOException { + final int len = src.remaining(); - int remaining = len; - while(remaining > 0) { - final int n = Math.min(remaining, bufferSize); - src.get(buf, 0, n); - out.write(buf, 0, n); - remaining = src.remaining(); - } + int remaining = len; + while (remaining > 0) { + final int n = Math.min(remaining, bufferSize); + src.get(buf, 0, n); + out.write(buf, 0, n); + remaining = src.remaining(); + } - return len; - } + return len; + } - /** - * Overrides the {@link Output#flush()}. - * Flushes this output and forces any buffered output bytes - * to be written out if the under layer output method support. - * - * @throws IOException if an I/O error occurs. - */ - @Override - public void flush() throws IOException { - out.flush(); - } + /** + * Overrides the {@link Output#flush()}. Flushes this output and forces any + * buffered output bytes to be written out if the under layer output method + * support. + * + * @throws IOException if an I/O error occurs. + */ + @Override + public void flush() throws IOException { + out.flush(); + } - /** - * Overrides the {@link Output#close()}. - * Closes this output and releases any system resources associated - * with the under layer output. - * - * @throws IOException if an I/O error occurs. - */ - @Override - public void close() throws IOException { - out.close(); - } + /** + * Overrides the {@link Output#close()}. Closes this output and releases any + * system resources associated with the under layer output. + * + * @throws IOException if an I/O error occurs. + */ + @Override + public void close() throws IOException { + out.close(); + } - protected OutputStream getOut() { - return out; - } + protected OutputStream getOut() { + return out; + } } http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/95a8d486/src/main/java/org/apache/commons/crypto/stream/output/package-info.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/stream/output/package-info.java b/src/main/java/org/apache/commons/crypto/stream/output/package-info.java index eca3927..c31b85e 100644 --- a/src/main/java/org/apache/commons/crypto/stream/output/package-info.java +++ b/src/main/java/org/apache/commons/crypto/stream/output/package-info.java @@ -19,3 +19,4 @@ * Output classes */ package org.apache.commons.crypto.stream.output; + http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/95a8d486/src/main/java/org/apache/commons/crypto/stream/package-info.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/stream/package-info.java b/src/main/java/org/apache/commons/crypto/stream/package-info.java index 416d78b..6ae28e3 100644 --- a/src/main/java/org/apache/commons/crypto/stream/package-info.java +++ b/src/main/java/org/apache/commons/crypto/stream/package-info.java @@ -19,3 +19,4 @@ * Stream classes */ package org.apache.commons.crypto.stream; + http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/95a8d486/src/main/java/org/apache/commons/crypto/utils/IOUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/crypto/utils/IOUtils.java b/src/main/java/org/apache/commons/crypto/utils/IOUtils.java index a3ecda4..4b0e6ee 100644 --- a/src/main/java/org/apache/commons/crypto/utils/IOUtils.java +++ b/src/main/java/org/apache/commons/crypto/utils/IOUtils.java @@ -28,71 +28,74 @@ import org.apache.commons.logging.Log; */ public class IOUtils { - private IOUtils() {} + private IOUtils() { + } - /** - * Does the readFully based on the Input read. - * - * @param in the input stream of bytes. - * @param buf the buffer to be read. - * @param off the start offset in array buffer. - * @param len the maximum number of bytes to read. - * @throws IOException if an I/O error occurs. - */ - public static void readFully(InputStream in, byte buf[], - int off, int len) throws IOException { - int toRead = len; - while (toRead > 0) { - int ret = in.read(buf, off, toRead); - if (ret < 0) { - throw new IOException( "Premature EOF from inputStream"); - } - toRead -= ret; - off += ret; + /** + * Does the readFully based on the Input read. + * + * @param in the input stream of bytes. + * @param buf the buffer to be read. + * @param off the start offset in array buffer. + * @param len the maximum number of bytes to read. + * @throws IOException if an I/O error occurs. + */ + public static void readFully(InputStream in, byte buf[], int off, int len) + throws IOException { + int toRead = len; + while (toRead > 0) { + int ret = in.read(buf, off, toRead); + if (ret < 0) { + throw new IOException("Premature EOF from inputStream"); + } + toRead -= ret; + off += ret; + } } - } - /** - * Does the readFully based on Input's positioned read. - * This does not change the current offset of the stream and is thread-safe. - * - * @param in the input source. - * @param position the given position. - * @param buffer the buffer to be read. - * @param length the maximum number of bytes to read. - * @param offset the start offset in array buffer. - * @throws IOException if an I/O error occurs. - */ - public static void readFully(Input in, long position, - byte[] buffer, int offset, int length) throws IOException { - int nread = 0; - while (nread < length) { - int nbytes = in.read(position+nread, buffer, offset+nread, length-nread); - if (nbytes < 0) { - throw new IOException("End of stream reached before reading fully."); - } - nread += nbytes; + /** + * Does the readFully based on Input's positioned read. This does not change + * the current offset of the stream and is thread-safe. + * + * @param in the input source. + * @param position the given position. + * @param buffer the buffer to be read. + * @param length the maximum number of bytes to read. + * @param offset the start offset in array buffer. + * @throws IOException if an I/O error occurs. + */ + public static void readFully(Input in, long position, byte[] buffer, + int offset, int length) throws IOException { + int nread = 0; + while (nread < length) { + int nbytes = in.read(position + nread, buffer, offset + nread, + length - nread); + if (nbytes < 0) { + throw new IOException( + "End of stream reached before reading fully."); + } + nread += nbytes; + } } - } - /** - * Closes the Closeable objects and <b>ignore</b> any {@link IOException} or - * null pointers. Must only be used for cleanup in exception handlers. - * - * @param log the log to record problems to at debug level. Can be null. - * @param closeables the objects to close. - */ - public static void cleanup(Log log, java.io.Closeable... closeables) { - for (java.io.Closeable c : closeables) { - if (c != null) { - try { - c.close(); - } catch(Throwable e) { - if (log != null && log.isDebugEnabled()) { - log.debug("Exception in closing " + c, e); - } + /** + * Closes the Closeable objects and <b>ignore</b> any {@link IOException} or + * null pointers. Must only be used for cleanup in exception handlers. + * + * @param log the log to record problems to at debug level. Can be null. + * @param closeables the objects to close. + */ + public static void cleanup(Log log, java.io.Closeable... closeables) { + for (java.io.Closeable c : closeables) { + if (c != null) { + try { + c.close(); + } catch (Throwable e) { + if (log != null && log.isDebugEnabled()) { + log.debug("Exception in closing " + c, e); + } + } + } } - } } - } }