org.apache.abdera.util.io
Class ReadWriteByteChannel

java.lang.Object
  extended by org.apache.abdera.util.io.ReadWriteByteChannel
All Implemented Interfaces:
java.io.Closeable, java.io.Serializable, java.lang.Cloneable, java.nio.channels.Channel, java.nio.channels.ReadableByteChannel, java.nio.channels.WritableByteChannel

public class ReadWriteByteChannel
extends java.lang.Object
implements java.nio.channels.ReadableByteChannel, java.nio.channels.WritableByteChannel, java.lang.Cloneable, java.io.Serializable

Implements a buffer that provides a slightly more efficient way of writing, and then reading a stream of bytes. To use: ReadWriteByteChannel rw = new ReadWriteByteChannel(); OutputStream out = rw.getOutputStream(); out.write(bytes); out.close(); InputStream in = rw.getInputStream(); int i = -1; while ((i = in.read()) != -1) {...} By default, closing the OutputStream will automatically cause it to flip over to Read mode, locking the buffer from further writes and setting the read position to 0. Once the Buffer has been fully read, it must be reset, which sets it back into write mode and moves the position pointer back to 0;

See Also:
Serialized Form

Field Summary
protected  byte[] buffer
           
protected  boolean closed
           
protected  boolean flipOnClose
           
protected  boolean flipped
           
protected  int position
           
protected  int scale
           
 
Constructor Summary
ReadWriteByteChannel()
           
ReadWriteByteChannel(int capacity)
           
ReadWriteByteChannel(int capacity, boolean flipOnClose)
           
 
Method Summary
 java.lang.Object clone()
           
 void close()
           
 void flip()
           
 byte[] getBuffer()
           
 java.io.InputStream getInputStream()
           
 CodepointIterator getIterator()
           
 CodepointIterator getIterator(java.lang.String charset)
           
 java.io.OutputStream getOutputStream()
           
 java.io.Reader getReader(java.lang.String charset)
           
 java.io.Writer getWriter(java.lang.String charset)
           
 boolean isOpen()
           
 int read(java.nio.ByteBuffer dst)
           
 void reset()
           
 void rewind()
           
 void transferTo(java.nio.channels.WritableByteChannel channel)
           
 int write(java.nio.ByteBuffer src)
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

position

protected int position

scale

protected int scale

closed

protected boolean closed

buffer

protected byte[] buffer

flipped

protected boolean flipped

flipOnClose

protected boolean flipOnClose
Constructor Detail

ReadWriteByteChannel

public ReadWriteByteChannel()

ReadWriteByteChannel

public ReadWriteByteChannel(int capacity)

ReadWriteByteChannel

public ReadWriteByteChannel(int capacity,
                            boolean flipOnClose)
Method Detail

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

getInputStream

public java.io.InputStream getInputStream()

getOutputStream

public java.io.OutputStream getOutputStream()

getWriter

public java.io.Writer getWriter(java.lang.String charset)

getReader

public java.io.Reader getReader(java.lang.String charset)

getBuffer

public byte[] getBuffer()

getIterator

public CodepointIterator getIterator()

getIterator

public CodepointIterator getIterator(java.lang.String charset)

read

public int read(java.nio.ByteBuffer dst)
         throws java.io.IOException
Specified by:
read in interface java.nio.channels.ReadableByteChannel
Throws:
java.io.IOException

flip

public void flip()

rewind

public void rewind()

reset

public void reset()

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Specified by:
close in interface java.nio.channels.Channel
Throws:
java.io.IOException

isOpen

public boolean isOpen()
Specified by:
isOpen in interface java.nio.channels.Channel

write

public int write(java.nio.ByteBuffer src)
          throws java.io.IOException
Specified by:
write in interface java.nio.channels.WritableByteChannel
Throws:
java.io.IOException

transferTo

public void transferTo(java.nio.channels.WritableByteChannel channel)
                throws java.io.IOException
Throws:
java.io.IOException