Convert a ByteArrayOutputStream into a ByteArrayInputStream

Often while programming you find yourself glueing pre-existing code fragments together. This works out sometimes but even more often it is rather annoying. These days i had to encrypt a pdf and do some other stuff with it in java and therefore put together some libraries i found. One of these libraries offered a method to encrypt the pdf and write the result into a ByteArrayOutputStream. Later then i had to push the encrypted pdf into another method that needed it as a ByteArrayInputStream. First i simply copied the content of the ByteArrayOutputStream as byte array into the ByteArrayInputStream, but this didn’t satisfy me since i knew that the ByteArrayInputStream just needed to be initialized with the internal buffer of the ByteArrayOutputStream. Since this internal buffer is hidden from outside i made up the ByteArrayInOutStream which is a ByteArrayOutputStream that has an additional method that returns a ByteArrayInputStream which is initialized with the internal buffer of the ByteArrayOutputStream. This allows for a instant ‘conversion’ that does not use up more memory than required.

Long story short a java class to ‘convert’ a ByteArrayOutputStream into a ByteArrayInputStream:

package info.whitebyte.utils;
 * This class extends the ByteArrayOutputStream by 
 * providing a method that returns a new ByteArrayInputStream
 * which uses the internal byte array buffer. This buffer
 * is not copied, so no additional memory is used. After
 * creating the ByteArrayInputStream the instance of the
 * ByteArrayInOutStream can not be used anymore.
 * <p>
 * The ByteArrayInputStream can be retrieved using <code>getInputStream()</code>.
 * @author Nick Russler
public class ByteArrayInOutStream extends ByteArrayOutputStream {
     * Creates a new ByteArrayInOutStream. The buffer capacity is
     * initially 32 bytes, though its size increases if necessary.
	public ByteArrayInOutStream() {
     * Creates a new ByteArrayInOutStream, with a buffer capacity of
     * the specified size, in bytes.
     * @param   size   the initial size.
     * @exception  IllegalArgumentException if size is negative.
	public ByteArrayInOutStream(int size) {
	 * Creates a new ByteArrayInputStream that uses the internal byte array buffer 
	 * of this ByteArrayInOutStream instance as its buffer array. The initial value 
	 * of pos is set to zero and the initial value of count is the number of bytes 
	 * that can be read from the byte array. The buffer array is not copied. This 
	 * instance of ByteArrayInOutStream can not be used anymore after calling this
	 * method.
	 * @return the ByteArrayInputStream instance
	public ByteArrayInputStream getInputStream() {
		// create new ByteArrayInputStream that respect the current count
		ByteArrayInputStream in = new ByteArrayInputStream(this.buf, 0, this.count);
		// set the buffer of the ByteArrayOutputStream 
		// to null so it can't be altered anymore
		this.buf = null;
		return in;

You can find the code on github, i would be glad to receive a pull request with improvements: on github

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.