com.google.enterprise.adaptor
Class IOHelper

java.lang.Object
  extended by com.google.enterprise.adaptor.IOHelper

public class IOHelper
extends Object

Utility class for providing useful methods when handling streams or other forms of I/O.


Method Summary
static void copyStream(InputStream in, OutputStream out)
          Copy contents of in to out.
static int readFully(InputStream is, byte[] bytes, int off, int len)
          Reads a specified number of bytes from a stream.
static byte[] readInputStreamToByteArray(InputStream is)
          Read the contents of is into a byte array.
static String readInputStreamToString(InputStream is, Charset charset)
          Form a string from the contents of is with charset charset.
static File writeToTempFile(InputStream in)
          Write contents of in to a temporary file.
static File writeToTempFile(String string, Charset charset)
          Write contents of string to a temporary file, encoded using charset.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

copyStream

public static void copyStream(InputStream in,
                              OutputStream out)
                       throws IOException
Copy contents of in to out.

Throws:
IOException

readInputStreamToByteArray

public static byte[] readInputStreamToByteArray(InputStream is)
                                         throws IOException
Read the contents of is into a byte array.

Throws:
IOException

readInputStreamToString

public static String readInputStreamToString(InputStream is,
                                             Charset charset)
                                      throws IOException
Form a string from the contents of is with charset charset.

Throws:
IOException

writeToTempFile

public static File writeToTempFile(InputStream in)
                            throws IOException
Write contents of in to a temporary file. Caller is responsible for deleting the temporary file after use.

Throws:
IOException

writeToTempFile

public static File writeToTempFile(String string,
                                   Charset charset)
                            throws IOException
Write contents of string to a temporary file, encoded using charset. Caller is responsible for deleting the temporary file after use.

Throws:
IOException

readFully

public static int readFully(InputStream is,
                            byte[] bytes,
                            int off,
                            int len)
                     throws IOException
Reads a specified number of bytes from a stream. Fewer bytes will only be returned if the end of stream has been reached.

Parameters:
is - the stream from which the bytes will be read.
bytes - the buffer into which the data is read.
off - the start offset in array b at which the data is written.
len - the number of bytes to read unless end of stream is encountered
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IOException - If the first byte cannot be read for any reason other than reaching end-of-stream, or if the input stream has been closed, or if some other I/O error occurs.
NullPointerException - If b is null.
IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off