au.com.prunge.jgenfile.impl.local.channels
Class RandomAccessFileChannel

java.lang.Object
  |
  +--au.com.prunge.jgenfile.impl.local.channels.RandomAccessFileChannel
All Implemented Interfaces:
ByteChannel, Channel, InterruptibleChannel, RandomAccessByteChannel, RandomAccessChannel, RandomAccessReadableByteChannel, RandomAccessWritableByteChannel, ReadableByteChannel, WrappedByteChannel, WritableByteChannel

public class RandomAccessFileChannel
extends Object
implements RandomAccessByteChannel, RandomAccessReadableByteChannel, RandomAccessWritableByteChannel, WrappedByteChannel, InterruptibleChannel

A wrapper for a file channel that implements the random access set of interfaces of JGenFile.

Version:
1.1
Author:
Peter Runge

Constructor Summary
RandomAccessFileChannel(FileChannel fileChannel)
          Constructs a RandomAccessFileChannel.
 
Method Summary
 void close()
           
 boolean equals(Object obj)
           
 void force(boolean metaData)
           
 ByteChannel getUnderlyingChannel()
          Returns the byte channel that this channel is wrapping.
 int hashCode()
           
 boolean isOpen()
           
 FileLock lock()
           
 FileLock lock(long position, long size, boolean shared)
           
 MappedByteBuffer map(FileChannel.MapMode mode, long position, long size)
           
 long position()
          Returns the channel's position.
 RandomAccessChannel position(long newPosition)
          Sets this channel's position.
 int read(ByteBuffer dst)
           
 long read(ByteBuffer[] dsts)
           
 long read(ByteBuffer[] dsts, int offset, int length)
           
 int read(ByteBuffer dst, long position)
           
 long size()
          Returns the size of this channel.
 String toString()
           
 long transferFrom(ReadableByteChannel src, long position, long count)
           
 long transferTo(long position, long count, WritableByteChannel target)
           
 FileChannel truncate(long size)
           
 FileLock tryLock()
           
 FileLock tryLock(long position, long size, boolean shared)
           
 int write(ByteBuffer src)
           
 long write(ByteBuffer[] srcs)
           
 long write(ByteBuffer[] srcs, int offset, int length)
           
 int write(ByteBuffer src, long position)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RandomAccessFileChannel

public RandomAccessFileChannel(FileChannel fileChannel)
                        throws NullPointerException
Constructs a RandomAccessFileChannel.

Parameters:
fileChannel - the underlying file channel.
Throws:
NullPointerException - if fileChannel is null.
Method Detail

equals

public boolean equals(Object obj)
Overrides:
equals in class Object
See Also:
Object.equals(java.lang.Object)

force

public void force(boolean metaData)
           throws IOException
Parameters:
metaData -
Throws:
IOException

hashCode

public int hashCode()
Overrides:
hashCode in class Object
See Also:
Object.hashCode()

lock

public FileLock lock()
              throws IOException
Returns:
Throws:
IOException

lock

public FileLock lock(long position,
                     long size,
                     boolean shared)
              throws IOException
Parameters:
position -
size -
shared -
Returns:
Throws:
IOException

map

public MappedByteBuffer map(FileChannel.MapMode mode,
                            long position,
                            long size)
                     throws IOException
Parameters:
mode -
position -
size -
Returns:
Throws:
IOException

read

public int read(ByteBuffer dst,
                long position)
         throws IOException
Parameters:
dst -
position -
Returns:
Throws:
IOException

read

public long read(ByteBuffer[] dsts)
          throws IOException
Parameters:
dsts -
Returns:
Throws:
IOException

read

public long read(ByteBuffer[] dsts,
                 int offset,
                 int length)
          throws IOException
Parameters:
dsts -
offset -
length -
Returns:
Throws:
IOException

toString

public String toString()
Overrides:
toString in class Object
See Also:
Object.toString()

transferFrom

public long transferFrom(ReadableByteChannel src,
                         long position,
                         long count)
                  throws IOException
Parameters:
src -
position -
count -
Returns:
Throws:
IOException

transferTo

public long transferTo(long position,
                       long count,
                       WritableByteChannel target)
                throws IOException
Parameters:
position -
count -
target -
Returns:
Throws:
IOException

truncate

public FileChannel truncate(long size)
                     throws IOException
Parameters:
size -
Returns:
Throws:
IOException

tryLock

public FileLock tryLock()
                 throws IOException
Returns:
Throws:
IOException

tryLock

public FileLock tryLock(long position,
                        long size,
                        boolean shared)
                 throws IOException
Parameters:
position -
size -
shared -
Returns:
Throws:
IOException

write

public int write(ByteBuffer src,
                 long position)
          throws IOException
Parameters:
src -
position -
Returns:
Throws:
IOException

write

public long write(ByteBuffer[] srcs)
           throws IOException
Parameters:
srcs -
Returns:
Throws:
IOException

write

public long write(ByteBuffer[] srcs,
                  int offset,
                  int length)
           throws IOException
Parameters:
srcs -
offset -
length -
Returns:
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Channel
Throws:
IOException

isOpen

public boolean isOpen()
Specified by:
isOpen in interface Channel
Returns:

position

public long position()
              throws IOException
Description copied from interface: RandomAccessChannel
Returns the channel's position.

Specified by:
position in interface RandomAccessChannel
Returns:
Throws:
IOException

position

public RandomAccessChannel position(long newPosition)
                             throws IOException
Description copied from interface: RandomAccessChannel
Sets this channel's position.

Setting the position to a value that is greater than the channel's current size is legal but does not change its size. A later attempt to read bytes at such a position will immediately return an end-of-file indication. A later attempt to write bytes at such a position will cause the channel to be grown to accomodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.

Specified by:
position in interface RandomAccessChannel
Parameters:
newPosition -
Returns:
Throws:
IOException

read

public int read(ByteBuffer dst)
         throws IOException
Specified by:
read in interface ReadableByteChannel
Parameters:
dst -
Returns:
Throws:
IOException

size

public long size()
          throws IOException
Description copied from interface: RandomAccessChannel
Returns the size of this channel.

Specified by:
size in interface RandomAccessChannel
Returns:
Throws:
IOException

write

public int write(ByteBuffer src)
          throws IOException
Specified by:
write in interface WritableByteChannel
Parameters:
src -
Returns:
Throws:
IOException

getUnderlyingChannel

public ByteChannel getUnderlyingChannel()
Description copied from interface: WrappedByteChannel
Returns the byte channel that this channel is wrapping.

Specified by:
getUnderlyingChannel in interface WrappedByteChannel
Returns:
the underlying byte channel.
See Also:
WrappedByteChannel.getUnderlyingChannel()