org.carrot2.util
Class StreamUtils

java.lang.Object
  extended by org.carrot2.util.StreamUtils

public final class StreamUtils
extends Object

A set of common helper methods used with input streams and file objects.


Field Summary
static Writer NULL_WRITER
          A dummy writer with empty implementations of Writer methods.
 
Method Summary
static void copy(InputStream in, OutputStream out, int bufSize)
          Copies all available data from the input stream to the output stream.
static void copyAndClose(InputStream in, OutputStream out, int bufSize)
          Copies all available data from the input stream to the output stream.
static InputStream prefetch(InputStream stream)
          Prefetch the entire content of stream, close it and return an InputStream to an in-memory ByteArrayInputStream.
static byte[] readFully(InputStream input)
          Read the contents of an opened stream until EOF (or an exception is thrown).
static char[] readFully(Reader input)
          Read the contents of an opened reader until EOF (or an exception is thrown).
static byte[] readFullyAndClose(InputStream input)
          Read the contents of an opened stream until EOF (or an exception is thrown).
static char[] readFullyAndClose(Reader input)
          Read the contents of an opened Reader until EOF (or an exception is thrown).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NULL_WRITER

public static final Writer NULL_WRITER
A dummy writer with empty implementations of Writer methods.

Method Detail

readFullyAndClose

public static byte[] readFullyAndClose(InputStream input)
                                throws IOException
Read the contents of an opened stream until EOF (or an exception is thrown). Note that this method does close the input stream, regardless whether exception has been thrown or not.

Parameters:
input - Input stream to be read.
Throws:
IOException - propagated from the underlying stream.

readFully

public static byte[] readFully(InputStream input)
                        throws IOException
Read the contents of an opened stream until EOF (or an exception is thrown). Note that this method does NOT close the stream.

Parameters:
input - InputStream from which data is to be read.
Throws:
IOException - propagated from the underlying stream.

readFullyAndClose

public static char[] readFullyAndClose(Reader input)
                                throws IOException
Read the contents of an opened Reader until EOF (or an exception is thrown). Note that this method does close the input stream, regardless whether exception has been thrown or not.

Parameters:
input - Reader to be read.
Throws:
IOException - propagated from the underlying stream.

readFully

public static char[] readFully(Reader input)
                        throws IOException
Read the contents of an opened reader until EOF (or an exception is thrown). Note that this method does NOT close the stream.

Parameters:
input - Reader from which data will be read.
Throws:
IOException - propagated from the underlying stream.

prefetch

public static InputStream prefetch(InputStream stream)
                            throws IOException
Prefetch the entire content of stream, close it and return an InputStream to an in-memory ByteArrayInputStream. If stream is already a ByteArrayInputStream, it is returned immediately.

Throws:
IOException

copy

public static void copy(InputStream in,
                        OutputStream out,
                        int bufSize)
                 throws IOException
Copies all available data from the input stream to the output stream. Data is internally buffered. Neither of the streams will be closed.

Throws:
IOException

copyAndClose

public static void copyAndClose(InputStream in,
                                OutputStream out,
                                int bufSize)
                         throws IOException
Copies all available data from the input stream to the output stream. Data is internally buffered. Both stream will be closed before the method returns.

Throws:
IOException


Copyright (c) Dawid Weiss, Stanislaw Osinski