Home | FAQ | Contact me

Slurping a stream...

This is some code I wrote to slurp bytes out of an InputStream such as might come back via request.getInputStream(). I show this as a private, helper method inside another class since that's how I used it.

Inside this code

In terms of beginning Java samples you have:

  • Use of classes StringBuffer and String.
  • Use of byte as an intermediary.
  • Use of common method toString().

Eclipse tip: Call Hierarchy

Double-click in the editor to highlight a method, then hold down the Crtl and Alt buttons while typing an 'h' to see, in the Call Hierarchy pane, other methods that call this one.

Note that some stream handlers don't require such intermediary. For example:

import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.JDomDriver; ... void someMethod( InputStream is ) { XStream xs - new XStream( new JDomDriver() ); ... xs.alias( method, whichClass ); String string = ( String ) xs.fromXML( is ); ... }

Here's a solution:

package com.etretatlogiciels.samples.streams;

import java.io.InputStream;
import java.lang.StringBuffer;
import java.io.IOException;

public class SomeClass
{
  .
  .
  .

  /**
   * This sips at a stream turning the whole thing into a String.
   *
   * @param in - the in-coming stream (InputStream)
   * @return a new string with the contents of the input stream (String)
   * @throws IOException
   */
  private static String slurp( InputStream in ) throws IOException
  {
    int          n   = 0;
    StringBuffer out = new StringBuffer();
    byte[]       b   = new byte[ 4096 ];

    while( ( n = in.read( b ) ) != -1 )
        out.append( new String( b, 0, n ) );

    return out.toString();
  }

  .
  .
  .
}

A complete solution as a library utility

StreamUtilities.java:
package com.etretatlogiciels.utilities;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class StreamUtilities
{
  /**
   * "Slurp" all the bytes on the stream into a string and return it.
   * @param is the input stream.
   * @param bufferSize pass -1 if you don't care how much is read at a time.
   * @return the bytes in the stream as a string.
   * @throws IOException
   */
  public static String slurp( final InputStream is, final int bufferSize ) throws IOException
  {
    int                 howMuch = ( bufferSize == -1 ) ? 4096 : bufferSize;
    final char[]        buffer  = new char[ howMuch ];
    final StringBuilder out     = new StringBuilder();
    final Reader        in      = new InputStreamReader( is, "UTF-8" );

    for( ; ; )
    {
      int chunkLength = in.read( buffer, 0, buffer.length );

      if( chunkLength < 0 )
        break;

      out.append( buffer, 0, chunkLength );
    }

    return out.toString();
  }

  /**
   * Slurp the stream and upon any error, just return null.
   * @param stream input stream, presumably non-empty.
   * @return what's in it.
   */
  public static String slurpTheStream( InputStream stream )
  {
    if( stream == null )
      return null;

    String whatsInTheStream;

    try
    {
      whatsInTheStream = StreamUtilities.slurp( stream, -1 );
    }
    catch( IOException e )
    {
      return null;
    }

    return whatsInTheStream;
  }
}