de.netseeker.ejoe.io
Class IOUtil

java.lang.Object
  extended by de.netseeker.ejoe.io.IOUtil

public final class IOUtil
extends java.lang.Object

Some useful methods for closing io streams, io readers, channels and Selector quitely, as well as methods for non-blocking, semi-blocking and full blocking io read/write operations.

Since:
0.3.1
Author:
netseeker aka Michael Manske

Constructor Summary
IOUtil()
           
 
Method Summary
static java.lang.Object adapterDeserialize(SerializeAdapter adapter, java.io.InputStream in, boolean compressed)
          Invokes a (De)SerializeAdapter for the given Object and handles decompression and buffering
static void adapterSerialize(SerializeAdapter adapter, java.io.OutputStream out, java.lang.Object obj, boolean compressed, int compressionLevel)
          Invokes a SerializeAdapter for the given Object and handles compression and buffering
static byte bBitsToByte(boolean[] bits)
          Converts a array of boolean to a byte
static java.lang.String bBitsToSBits(boolean[] bBits)
          Converts a array of boolean into a bit-like String representation
static boolean[] byteToBBits(byte b)
          Converts a byte to an array of boolean
static void closeQuiet(java.nio.channels.Channel channel)
          Tries to close an NIO Channel and handles null values and IOExceptions quietly
static void closeQuiet(java.io.InputStream in)
          Tries to close an InputStream and handles null values and IOExceptions quietly
static void closeQuiet(java.io.OutputStream out)
          Tries to close an OutputStream and handles null values and IOExceptions quietly
static void closeQuiet(java.io.Reader reader)
          Tries to close a Reader and handles null values and IOExceptions quietly
static void closeQuiet(java.nio.channels.Selector selector)
          Tries to close a NIO Selector and handles null values and IOExceptions quietly
static void closeQuiet(java.io.Writer out)
          Tries to close a Writer and handles null values and IOExceptions quietly
static java.lang.String decodeToString(java.nio.ByteBuffer dataBuf)
           
static java.lang.String decodeToString(java.nio.ByteBuffer dataBuf, java.lang.String charset)
           
static java.nio.ByteBuffer encodeToByteBuffer(java.lang.String str)
          Converts the given string into a UTF8-encoded ByteBuffer.
static byte[] encodeToBytes(java.lang.String str)
          Converts the given string into a UTF8-encoded array of bytes.
static byte[] encodeToBytes(java.lang.String str, java.lang.String cs)
          Converts the given string into a encoded array of bytes.
static java.nio.ByteBuffer readDirect(java.io.InputStream in)
          Reads and returns a ByteBuffer from an InputStream
static boolean[] sBitsToBBits(java.lang.String sBits)
          Converts a bit-like String representation into an array of boolean
static void setReceiveBufferSize(java.net.Socket socket, int size)
          Set the SO_RCVBUF hint on a connected socket to the size of the data which are expected to be read next time
static void setSendBufferSize(java.net.Socket socket, int size)
          Set the SO_SNDBUF hint on a connected socket to the size of the data which are expected to be written next time
static void writeDirect(java.io.OutputStream out, java.nio.ByteBuffer buf)
          Writes a ByteBuffer directly into an OutputStream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IOUtil

public IOUtil()
Method Detail

closeQuiet

public static void closeQuiet(java.io.OutputStream out)
Tries to close an OutputStream and handles null values and IOExceptions quietly

Parameters:
out - The output stream to close quietly

closeQuiet

public static void closeQuiet(java.io.Writer out)
Tries to close a Writer and handles null values and IOExceptions quietly

Parameters:
out - The writer to close quietly

closeQuiet

public static void closeQuiet(java.io.InputStream in)
Tries to close an InputStream and handles null values and IOExceptions quietly

Parameters:
in - the input stream to close quietly

closeQuiet

public static void closeQuiet(java.io.Reader reader)
Tries to close a Reader and handles null values and IOExceptions quietly

Parameters:
reader - the reader to close quietly

closeQuiet

public static void closeQuiet(java.nio.channels.Channel channel)
Tries to close an NIO Channel and handles null values and IOExceptions quietly

Parameters:
channel - the channel to close quietly

closeQuiet

public static void closeQuiet(java.nio.channels.Selector selector)
Tries to close a NIO Selector and handles null values and IOExceptions quietly

Parameters:
selector - the selector to close quietly

adapterSerialize

public static void adapterSerialize(SerializeAdapter adapter,
                                    java.io.OutputStream out,
                                    java.lang.Object obj,
                                    boolean compressed,
                                    int compressionLevel)
                             throws java.lang.Exception
Invokes a SerializeAdapter for the given Object and handles compression and buffering

Parameters:
adapter -
out -
obj -
buffered -
compressed -
Throws:
java.lang.Exception

adapterDeserialize

public static java.lang.Object adapterDeserialize(SerializeAdapter adapter,
                                                  java.io.InputStream in,
                                                  boolean compressed)
                                           throws java.lang.Exception
Invokes a (De)SerializeAdapter for the given Object and handles decompression and buffering

Parameters:
adapter -
in -
buffered -
compressed -
Returns:
Throws:
java.lang.Exception

readDirect

public static java.nio.ByteBuffer readDirect(java.io.InputStream in)
                                      throws java.io.IOException
Reads and returns a ByteBuffer from an InputStream

Parameters:
in - InputStream from which read a ByteBuffer
Returns:
a ByteBuffer read from the InputStream
Throws:
java.io.IOException

writeDirect

public static void writeDirect(java.io.OutputStream out,
                               java.nio.ByteBuffer buf)
                        throws java.io.IOException
Writes a ByteBuffer directly into an OutputStream

Parameters:
out -
buf -
Throws:
java.io.IOException

setSendBufferSize

public static void setSendBufferSize(java.net.Socket socket,
                                     int size)
                              throws java.net.SocketException
Set the SO_SNDBUF hint on a connected socket to the size of the data which are expected to be written next time

Parameters:
socket - the connected socket
size - size to set for SO_SNDBUF
Throws:
java.net.SocketException

setReceiveBufferSize

public static void setReceiveBufferSize(java.net.Socket socket,
                                        int size)
                                 throws java.net.SocketException
Set the SO_RCVBUF hint on a connected socket to the size of the data which are expected to be read next time

Parameters:
socket - the connected socket
size - size to set for SO_RCVBUF
Throws:
java.net.SocketException

encodeToBytes

public static byte[] encodeToBytes(java.lang.String str)
Converts the given string into a UTF8-encoded array of bytes. If UTF8 is not supported the default charset is used.

Parameters:
str -
Returns:

encodeToBytes

public static byte[] encodeToBytes(java.lang.String str,
                                   java.lang.String cs)
Converts the given string into a encoded array of bytes. For encoding the given charSet is used. If the given charSet not supported the default charset is used.

Parameters:
str - string to be encoded
cs - Charset to use for encoding
Returns:

encodeToByteBuffer

public static java.nio.ByteBuffer encodeToByteBuffer(java.lang.String str)
Converts the given string into a UTF8-encoded ByteBuffer. If UTF8 is not supported the default charset is used.

Parameters:
str -
Returns:

decodeToString

public static java.lang.String decodeToString(java.nio.ByteBuffer dataBuf)
Parameters:
dataBuf -
Returns:

decodeToString

public static java.lang.String decodeToString(java.nio.ByteBuffer dataBuf,
                                              java.lang.String charset)
Parameters:
dataBuf -
charset -
Returns:

byteToBBits

public static boolean[] byteToBBits(byte b)
Converts a byte to an array of boolean

Parameters:
b - a byte
Returns:
representation of the given byte as array of booleans

bBitsToByte

public static byte bBitsToByte(boolean[] bits)
Converts a array of boolean to a byte

Parameters:
bits - array of booleans representing the bits of a byte
Returns:
byte representation of the given array

sBitsToBBits

public static boolean[] sBitsToBBits(java.lang.String sBits)
Converts a bit-like String representation into an array of boolean

Parameters:
sBits - bit-like String representation of bits
Returns:
array of boolean, each item represents one bit

bBitsToSBits

public static java.lang.String bBitsToSBits(boolean[] bBits)
Converts a array of boolean into a bit-like String representation

Parameters:
bBits - array of boolean to convert
Returns:
bit-like String representation of the given array of boolean


Copyright © 2005-2007 netseeker. All Rights Reserved.