org.jscience.io
Class IOUtils

java.lang.Object
  extended by org.jscience.io.IOUtils

public final class IOUtils
extends java.lang.Object

The class IOUtils contains some useful static functions to manipulate data.


Field Summary
static int BUFFER_SIZE
          standard buffer size used (2048)
static java.io.OutputStream devNullStream
          implements an outputstream that writes nowhere
 
Method Summary
static byte[] asByteArray(java.io.InputStream stream)
           
static int count(java.io.InputStream stream, byte[] pattern)
          returns how many times the given byte pattern occurs within the file
static long countEqualBytes(java.io.InputStream stream1, java.io.InputStream stream2)
          returns the number of bytes that are equal in both streams; -1 is returned if both streams are equal until both ends.
static long countEqualBytes(java.io.InputStream stream1, java.io.InputStream stream2, long maxBytes)
          if maxBytes <=0, the streams are tested to their ends
static long countEqualChars(java.io.Reader reader1, java.io.Reader reader2)
          returns the number of characters that are equal in both readers; -1 is returned if both readers are equal until both ends.
static long countEqualChars(java.io.Reader reader1, java.io.Reader reader2, long maxChars)
          if maxChars <=0, the readers are tested to their ends
static java.lang.Object deserialize(byte[] bytes)
          de-serializes the given serialized bytes back to an object
static boolean equals(java.io.InputStream stream1, java.io.InputStream stream2)
          compares the content of the two streams and returns true only if both streams have equal content
static boolean equals(java.io.InputStream stream1, java.io.InputStream stream2, long maxBytes)
          compares the content of the two streams and returns true only if both streams have equal content for maxByte number of bytes.
static java.lang.String getConsoleInput()
          reads a line from the console and returns the keyboard input as a string after 'Enter' was pressed
static java.lang.String getConsoleInput(java.lang.String question)
          prints the given question and returns getConsoleInput()
static long getSerialVersionUID(java.lang.Class c)
           
static long getSerialVersionUID(java.lang.Object obj)
           
static int indexOf(byte[] source, byte[] pattern)
          returns indexOf(source, pattern, 0, source.length)
static int indexOf(byte[] source1, byte[] source2, byte[] pattern)
          searches for an occurrence of the given byte pattern only within the 'intersection' of the given two byte sources.
static int indexOf(byte[] source, byte[] pattern, int beginIndex, int endIndex)
          searches for the first occurrence of the given byte pattern within the given byte source.
static int indexOf(char[] source, char[] pattern)
          returns indexOf(source, pattern, 0, source.length)
static int indexOf(char[] source1, char[] source2, char[] pattern)
          searches for an occurrence of the given byte pattern only within the 'intersection' of the given two byte sources.
static int indexOf(char[] source, char[] pattern, int beginIndex, int endIndex)
          searches for the first occurrence of the given character pattern within the given byte source.
static long indexOf(java.io.InputStream in, byte[] pattern)
           
static byte[] serialize(java.lang.Object obj)
          serializes the given object and returnes the serialized data as byte array
static long transfer(java.io.InputStream from, java.io.OutputStream to)
          returns number of bytes transfered; closes both streams and flushes
static long transfer(java.io.InputStream from, java.io.OutputStream to, boolean flushAndClose)
          transfers the inputstream to the outputstream
static long transfer(java.io.InputStream from, java.io.OutputStream to, byte[] buffer, boolean flushAndClose)
          transfers the inputstream to the outputstream
static long transfer(java.io.InputStream from, java.io.OutputStream to, long maxBytes)
          transfers the given number of bytes from one stream to the other
static long transfer(java.io.InputStream from, java.io.OutputStream to, long maxBytes, boolean flushAndClose)
           
static long transfer(java.io.InputStream from, java.io.OutputStream to, long maxBytes, byte[] buffer, boolean flushAndClose)
          transfers the bytes from the given inputstream to the outputstream.
static long transfer(java.io.Reader from, java.io.Writer to)
          transfers the reader to the writer.
static long transfer(java.io.Reader from, java.io.Writer to, long maxChars, char[] buffer, boolean flushAndClose)
          transfers the characters from the given reader to the writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

devNullStream

public static final java.io.OutputStream devNullStream
implements an outputstream that writes nowhere


BUFFER_SIZE

public static final int BUFFER_SIZE
standard buffer size used (2048)

See Also:
Constant Field Values
Method Detail

getConsoleInput

public static java.lang.String getConsoleInput()
reads a line from the console and returns the keyboard input as a string after 'Enter' was pressed


getConsoleInput

public static java.lang.String getConsoleInput(java.lang.String question)
prints the given question and returns getConsoleInput()


deserialize

public static java.lang.Object deserialize(byte[] bytes)
                                    throws java.lang.ClassNotFoundException,
                                           java.io.IOException
de-serializes the given serialized bytes back to an object

Throws:
java.lang.ClassNotFoundException
java.io.IOException

serialize

public static byte[] serialize(java.lang.Object obj)
                        throws java.io.IOException
serializes the given object and returnes the serialized data as byte array

Parameters:
obj - must be Serializable
Throws:
java.io.IOException

indexOf

public static int indexOf(byte[] source,
                          byte[] pattern)
returns indexOf(source, pattern, 0, source.length)

See Also:
indexOf(byte[],byte[],int,int)

indexOf

public static int indexOf(byte[] source,
                          byte[] pattern,
                          int beginIndex,
                          int endIndex)
searches for the first occurrence of the given byte pattern within the given byte source.

Parameters:
source - a byte array containing the data that is searched for the occurrence of a pattern
pattern - a (non-empty) byte array containing the search pattern
beginIndex - the index to begin the pattern search within the source array (between 0 and source pattern length and smaller than endIndex)
endIndex - the index to end the pattern search within the source array (between 0 and source pattern length and greater than beginIndex)
Returns:
the starting position of the first occurrence of the given pattern or -1 if the pattern is not present
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the parameter pattern is an empty array
java.lang.NullPointerException - if any of the given parameters are null

indexOf

public static int indexOf(byte[] source1,
                          byte[] source2,
                          byte[] pattern)
searches for an occurrence of the given byte pattern only within the 'intersection' of the given two byte sources.

Returns:
the starting position of the first occurrence of the given pattern as a position of the first source or -1 if the pattern is not present
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the parameter pattern is an empty array
java.lang.NullPointerException - if any of the given parameters are null
See Also:
indexOf(byte[],byte[])

indexOf

public static int indexOf(char[] source,
                          char[] pattern)
returns indexOf(source, pattern, 0, source.length)

See Also:
indexOf(char[],char[],int,int)

indexOf

public static int indexOf(char[] source,
                          char[] pattern,
                          int beginIndex,
                          int endIndex)
searches for the first occurrence of the given character pattern within the given byte source.

Parameters:
source - a byte array containing the data that is searched for the occurrence of a pattern
pattern - a (non-empty) character array containing the search pattern
beginIndex - the index to begin the pattern search within the source array (between 0 and source pattern length and smaller than endIndex)
endIndex - the index to end the pattern search within the source array (between 0 and source pattern length and greater than beginIndex)
Returns:
the starting position of the first occurrence of the given pattern or -1 if the pattern is not present
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the parameter pattern is an empty array
java.lang.NullPointerException - if any of the given parameters are null

indexOf

public static int indexOf(char[] source1,
                          char[] source2,
                          char[] pattern)
searches for an occurrence of the given byte pattern only within the 'intersection' of the given two byte sources.

Returns:
the starting position of the first occurrence of the given pattern as a position of the first source or -1 if the pattern is not present
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the parameter pattern is an empty array
java.lang.NullPointerException - if any of the given parameters are null
See Also:
indexOf(byte[],byte[])

indexOf

public static long indexOf(java.io.InputStream in,
                           byte[] pattern)
                    throws java.io.IOException
Throws:
java.io.IOException

transfer

public static long transfer(java.io.InputStream from,
                            java.io.OutputStream to)
                     throws java.io.IOException
returns number of bytes transfered; closes both streams and flushes

Throws:
java.io.IOException

transfer

public static long transfer(java.io.InputStream from,
                            java.io.OutputStream to,
                            boolean flushAndClose)
                     throws java.io.IOException
transfers the inputstream to the outputstream

Throws:
java.io.IOException

transfer

public static long transfer(java.io.InputStream from,
                            java.io.OutputStream to,
                            byte[] buffer,
                            boolean flushAndClose)
                     throws java.io.IOException
transfers the inputstream to the outputstream

Throws:
java.io.IOException

transfer

public static long transfer(java.io.InputStream from,
                            java.io.OutputStream to,
                            long maxBytes)
                     throws java.io.IOException
transfers the given number of bytes from one stream to the other

Throws:
java.io.IOException

transfer

public static long transfer(java.io.InputStream from,
                            java.io.OutputStream to,
                            long maxBytes,
                            boolean flushAndClose)
                     throws java.io.IOException
Throws:
java.io.IOException

transfer

public static long transfer(java.io.InputStream from,
                            java.io.OutputStream to,
                            long maxBytes,
                            byte[] buffer,
                            boolean flushAndClose)
                     throws java.io.IOException
transfers the bytes from the given inputstream to the outputstream.

Parameters:
from - the InputStream from which to read the data
to - the OutputStream to which to write the data
buffer - the byte buffer to be used; must have a length greater than zero
flushAndClose - if true, the method also flushes and closes both streams
maxBytes - maximum number of bytes to be transfered; if less than 1, the entire inputstream is transfered
Returns:
the number of bytes actually transfered
Throws:
java.io.IOException

transfer

public static long transfer(java.io.Reader from,
                            java.io.Writer to)
                     throws java.io.IOException
transfers the reader to the writer. Flushes writer and closes reader and writer.

Returns:
number of characters transfered;
Throws:
java.io.IOException

transfer

public static long transfer(java.io.Reader from,
                            java.io.Writer to,
                            long maxChars,
                            char[] buffer,
                            boolean flushAndClose)
                     throws java.io.IOException
transfers the characters from the given reader to the writer.

Parameters:
from - the Reader from which to read the data
to - the Writer to which to write the data
buffer - the char buffer to be used; must have a length greater than zero
flushAndClose - if true, the method also flushes and closes both: reader and writer
maxChars - maximum number of characters to be transfered; if less than 1, the entire inputstream is transfered
Returns:
the number of characters actually transfered
Throws:
java.io.IOException

asByteArray

public static byte[] asByteArray(java.io.InputStream stream)
                          throws java.io.IOException
Throws:
java.io.IOException

equals

public static boolean equals(java.io.InputStream stream1,
                             java.io.InputStream stream2)
                      throws java.io.IOException
compares the content of the two streams and returns true only if both streams have equal content

Throws:
java.io.IOException

equals

public static boolean equals(java.io.InputStream stream1,
                             java.io.InputStream stream2,
                             long maxBytes)
                      throws java.io.IOException
compares the content of the two streams and returns true only if both streams have equal content for maxByte number of bytes. If maxBytes is equal or less than 0, its value is ignored and the stream is compared to its end.

Throws:
java.io.IOException

count

public static int count(java.io.InputStream stream,
                        byte[] pattern)
                 throws java.io.IOException
returns how many times the given byte pattern occurs within the file

Throws:
java.io.IOException

countEqualBytes

public static long countEqualBytes(java.io.InputStream stream1,
                                   java.io.InputStream stream2)
                            throws java.io.IOException
returns the number of bytes that are equal in both streams; -1 is returned if both streams are equal until both ends. If the markSupported() method returns true on both streams, the method uses the mark(int) method to allow to reset both streams to the point where both streams were still equal; otherwise, both streams have already read the first unequal byte when the method returns - or they are at the end of one or both streams. If both streams have equal content but one stream is longer than the other, the length of the shorter stream is returned.

Throws:
java.io.IOException

countEqualBytes

public static long countEqualBytes(java.io.InputStream stream1,
                                   java.io.InputStream stream2,
                                   long maxBytes)
                            throws java.io.IOException
if maxBytes <=0, the streams are tested to their ends

Throws:
java.io.IOException

countEqualChars

public static long countEqualChars(java.io.Reader reader1,
                                   java.io.Reader reader2)
                            throws java.io.IOException
returns the number of characters that are equal in both readers; -1 is returned if both readers are equal until both ends. If the markSupported() method returns true on both readers, the method uses the mark(int) method to allow to reset both readers to the point where both readers were still equal; otherwise, both readers have already read the first unequal character when the method returns - or they are at the end of one or both readers. If both readers have equal content but one reader is longer than the other, the length of the shorter reader is returned.

Throws:
java.io.IOException

countEqualChars

public static long countEqualChars(java.io.Reader reader1,
                                   java.io.Reader reader2,
                                   long maxChars)
                            throws java.io.IOException
if maxChars <=0, the readers are tested to their ends

Throws:
java.io.IOException

getSerialVersionUID

public static long getSerialVersionUID(java.lang.Object obj)

getSerialVersionUID

public static long getSerialVersionUID(java.lang.Class c)