Holger's
Java API

com.antelmann.io
Class Data

java.lang.Object
  extended by com.antelmann.io.Data

public final class Data
extends Object

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

Author:
Holger Antelmann

Field Summary
static int BUFFER_SIZE
          standard buffer size used (4096)
static OutputStream SINK
          implements an outputstream that writes nowhere
static DataFlavor URL_FLAVOR
           
 
Method Summary
static InputStream asInputStream(Reader reader, String charset)
          since the entire stream is read into memory, this is only suitable for small streams
static int count(InputStream stream, byte[] pattern)
          returns how many times the given byte pattern occurs within the file
static long countEqualBytes(InputStream stream1, 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(InputStream stream1, InputStream stream2, long maxBytes)
          if maxBytes <=0, the streams are tested to their ends
static long countEqualChars(Reader reader1, 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(Reader reader1, Reader reader2, long maxChars)
          if maxChars <=0, the readers are tested to their ends
static void decodeBase64(InputStream in, OutputStream out)
           
static byte[] decodeBase64(String s)
           
static
<T> T
deserialize(byte[] bytes)
          de-serializes the given serialized bytes back to an object
static byte[] digest(InputStream stream, String algorithm)
          returns a hashed value of the given stream based on the given algorithm
static byte[] digestMD5(InputStream stream)
          digests the stream using MD5
static byte[] digestSHA1(InputStream stream)
          digests the stream using SHA1
static Thread drain(InputStream in, OutputStream out, boolean closeOutput)
          starts a new thread that drains the input into the output for as long as there is input available; returns immediately
static void drainOutput(Process p)
          drains the output of the process to the console
static String encodeBase64(byte[] b)
           
static void encodeBase64(InputStream in, OutputStream out)
           
static boolean equals(InputStream stream1, InputStream stream2)
          compares the content of the two streams and returns true only if both streams have equal content
static boolean equals(InputStream stream1, 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 byte[] generateSalt(int length)
          generates a randomly generated array of bytes to be used as salt for hashing algorithms
static long getSerialVersionUID(Class<?> c)
           
static long getSerialVersionUID(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(InputStream in, byte[] pattern)
           
static byte[] serialize(Object obj)
          serializes the given object and returns the serialized data as byte array
static byte[] toByteArray(InputStream stream)
          transfers the given stream into a ByteArrayOutputStream and returns its embedded array; all streams will be closed.
static long transfer(InputStream from, OutputStream to)
          returns number of bytes transfered; closes both streams and flushes
static long transfer(InputStream from, OutputStream to, boolean flushAndClose)
          transfers the inputstream to the outputstream
static long transfer(InputStream from, OutputStream to, boolean flushAndClose, Monitor mon)
           
static long transfer(InputStream from, OutputStream to, byte[] buffer, boolean flushAndClose)
          transfers the InputStream to the OutputStream
static long transfer(InputStream from, OutputStream to, long maxBytes)
          the streams are neither flushed nor closed
static long transfer(InputStream from, OutputStream to, long maxBytes, boolean flushAndClose)
           
static long transfer(InputStream from, OutputStream to, long maxBytes, byte[] buffer, boolean flushAndClose)
          transfers the given number of bytes from one stream to the other
static long transfer(InputStream from, OutputStream to, long maxBytes, byte[] buffer, boolean flushAndClose, Monitor mon)
          transfers the bytes from the given inputstream to the outputstream.
static long transfer(InputStream from, OutputStream to, Monitor mon)
           
static long transfer(Reader from, Writer to)
          transfers the reader to the writer.
static long transfer(Reader from, Writer to, long maxChars, char[] buffer, boolean flushAndClose)
          transfers the characters from the given reader to the writer.
static int unsignedByteToInt(byte b)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

URL_FLAVOR

public static final DataFlavor URL_FLAVOR

SINK

public static final OutputStream SINK
implements an outputstream that writes nowhere


BUFFER_SIZE

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

See Also:
Constant Field Values
Method Detail

unsignedByteToInt

public static int unsignedByteToInt(byte b)

deserialize

public static <T> T deserialize(byte[] bytes)
                     throws ClassNotFoundException,
                            IOException,
                            ClassCastException
de-serializes the given serialized bytes back to an object

Throws:
ClassNotFoundException
IOException
ClassCastException

serialize

public static byte[] serialize(Object obj)
                        throws IOException
serializes the given object and returns the serialized data as byte array

Parameters:
obj - must be Serializable
Throws:
IOException

digestMD5

public static byte[] digestMD5(InputStream stream)
                        throws IOException
digests the stream using MD5

Throws:
IOException
See Also:
digest(java.io.InputStream, String)

digestSHA1

public static byte[] digestSHA1(InputStream stream)
                         throws IOException
digests the stream using SHA1

Throws:
IOException
See Also:
digest(java.io.InputStream, String)

digest

public static byte[] digest(InputStream stream,
                            String algorithm)
                     throws IOException,
                            NoSuchAlgorithmException
returns a hashed value of the given stream based on the given algorithm

Throws:
IOException
NoSuchAlgorithmException

generateSalt

public static byte[] generateSalt(int length)
generates a randomly generated array of bytes to be used as salt for hashing algorithms

See Also:
Strings.secureHash(String, byte[]), SecureRandom

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:
ArrayIndexOutOfBoundsException - if the parameter pattern is an empty array
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:
ArrayIndexOutOfBoundsException - if the parameter pattern is an empty array
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:
ArrayIndexOutOfBoundsException - if the parameter pattern is an empty array
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:
ArrayIndexOutOfBoundsException - if the parameter pattern is an empty array
NullPointerException - if any of the given parameters are null
See Also:
indexOf(byte[], byte[])

indexOf

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

drainOutput

public static void drainOutput(Process p)
drains the output of the process to the console


drain

public static Thread drain(InputStream in,
                           OutputStream out,
                           boolean closeOutput)
starts a new thread that drains the input into the output for as long as there is input available; returns immediately

Parameters:
in - the input to be drained
out - the 'sink'
Returns:
the started thread that 'drains' the given input stream
See Also:
SINK

transfer

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

Throws:
IOException

transfer

public static long transfer(InputStream from,
                            OutputStream to,
                            Monitor mon)
                     throws IOException
Throws:
IOException

transfer

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

Throws:
IOException

transfer

public static long transfer(InputStream from,
                            OutputStream to,
                            boolean flushAndClose,
                            Monitor mon)
                     throws IOException
Throws:
IOException

transfer

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

Throws:
IOException

transfer

public static long transfer(InputStream from,
                            OutputStream to,
                            long maxBytes)
                     throws IOException
the streams are neither flushed nor closed

Throws:
IOException

transfer

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

transfer

public static long transfer(InputStream from,
                            OutputStream to,
                            long maxBytes,
                            byte[] buffer,
                            boolean flushAndClose)
                     throws IOException
transfers the given number of bytes from one stream to the other

Throws:
IOException

transfer

public static long transfer(InputStream from,
                            OutputStream to,
                            long maxBytes,
                            byte[] buffer,
                            boolean flushAndClose,
                            Monitor mon)
                     throws 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 input stream is transfered
mon - allows to cancel the transfer operation by disabling the monitor (may be null)
Returns:
the number of bytes actually transfered
Throws:
IOException

transfer

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

Returns:
number of characters transfered;
Throws:
IOException

transfer

public static long transfer(Reader from,
                            Writer to,
                            long maxChars,
                            char[] buffer,
                            boolean flushAndClose)
                     throws 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:
IOException

asInputStream

public static InputStream asInputStream(Reader reader,
                                        String charset)
                                 throws IOException
since the entire stream is read into memory, this is only suitable for small streams

Throws:
IOException

toByteArray

public static byte[] toByteArray(InputStream stream)
                          throws IOException
transfers the given stream into a ByteArrayOutputStream and returns its embedded array; all streams will be closed.

Throws:
IOException

equals

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

Throws:
IOException

equals

public static boolean equals(InputStream stream1,
                             InputStream stream2,
                             long maxBytes)
                      throws 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:
IOException

count

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

Throws:
IOException

countEqualBytes

public static long countEqualBytes(InputStream stream1,
                                   InputStream stream2)
                            throws 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:
IOException

countEqualBytes

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

Throws:
IOException

countEqualChars

public static long countEqualChars(Reader reader1,
                                   Reader reader2)
                            throws 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:
IOException

countEqualChars

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

Throws:
IOException

encodeBase64

public static void encodeBase64(InputStream in,
                                OutputStream out)
                         throws IOException
Throws:
IOException

decodeBase64

public static void decodeBase64(InputStream in,
                                OutputStream out)
                         throws IOException
Throws:
IOException

encodeBase64

public static String encodeBase64(byte[] b)

decodeBase64

public static byte[] decodeBase64(String s)

getSerialVersionUID

public static long getSerialVersionUID(Object obj)

getSerialVersionUID

public static long getSerialVersionUID(Class<?> c)


(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads