com.faunos.util.io
Class SubFileChannel

java.lang.Object
  extended by java.nio.channels.spi.AbstractInterruptibleChannel
      extended by java.nio.channels.FileChannel
          extended by com.faunos.util.io.FilterFileChannel
              extended by com.faunos.util.io.CloseProtectedFileChannel
                  extended by com.faunos.util.io.SubFileChannel
All Implemented Interfaces:
Closeable, ByteChannel, Channel, GatheringByteChannel, InterruptibleChannel, ReadableByteChannel, ScatteringByteChannel, WritableByteChannel

public class SubFileChannel
extends CloseProtectedFileChannel

A view of a region of an underlying FileChannel. The position of the this view is independent of that of the underlying FileChannel, except for the case involving truncation.

Access modes

Three access modes are supported. They are
  1. READ_ONLY. The instance can only read from the channel.
  2. BOUNDED_UPDATE. The instance can read and write to the channel, but may not change the file size.
  3. WRITE. The instance can read and write to to the channel, may grow the file size, or alternatively truncate it. This is the least protected view of the underlying FileChannel.

Author:
Babak Farhang

Nested Class Summary
static class SubFileChannel.AccessMode
          Defines the access modes supported by SubFileChannel.
 
Nested classes/interfaces inherited from class com.faunos.util.io.FilterFileChannel
FilterFileChannel.FilterFileLock
 
Nested classes/interfaces inherited from class java.nio.channels.FileChannel
FileChannel.MapMode
 
Field Summary
 
Fields inherited from class com.faunos.util.io.FilterFileChannel
inner
 
Constructor Summary
SubFileChannel(FileChannel file, SubFileChannel.AccessMode mode, long startPosition, long endPosition)
          Creates a new instance with the specified underlying file channel, access mode, and view boundaries.
 
Method Summary
protected  void checkAccess()
          Checks that the instance has read access.
protected  void checkWriteAccess()
          Checks that the instance has write access.
 void force(boolean metaData)
           
protected  FilterFileChannel.FilterFileLock implCreateFileLock(FileLock innerLock)
          Creates and returns a position-shifted FileLock.
 FileLock lock(long position, long size, boolean shared)
           
 MappedByteBuffer map(FileChannel.MapMode mode, long position, long size)
           
static SubFileChannel newReadOnlyView(FileChannel file, long startPosition, long endPosition)
           
 long position()
           
 FileChannel position(long newPosition)
           
 int read(ByteBuffer dst)
           
 long read(ByteBuffer[] dsts, int offset, int length)
           
 int read(ByteBuffer dst, long position)
           
 long size()
           
 long transferFrom(ReadableByteChannel src, long position, long count)
           
 long transferTo(long position, long count, WritableByteChannel target)
           
 FileChannel truncate(long size)
           
 FileLock tryLock(long position, long size, boolean shared)
           
 int write(ByteBuffer src)
           
 long write(ByteBuffer[] srcs, int offset, int length)
           
 int write(ByteBuffer src, long position)
           
 
Methods inherited from class com.faunos.util.io.CloseProtectedFileChannel
implCloseChannel
 
Methods inherited from class com.faunos.util.io.FilterFileChannel
checkReadAccess
 
Methods inherited from class java.nio.channels.FileChannel
lock, read, tryLock, write
 
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel
begin, close, end, isOpen
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.nio.channels.Channel
close, isOpen
 
Methods inherited from interface java.nio.channels.Channel
close, isOpen
 
Methods inherited from interface java.nio.channels.Channel
close, isOpen
 
Methods inherited from interface java.nio.channels.Channel
close, isOpen
 

Constructor Detail

SubFileChannel

public SubFileChannel(FileChannel file,
                      SubFileChannel.AccessMode mode,
                      long startPosition,
                      long endPosition)
               throws IOException
Creates a new instance with the specified underlying file channel, access mode, and view boundaries.

Parameters:
startPosition - the lower bounds of the view
endPosition - the upper bounds of the view
Throws:
IOException
Method Detail

newReadOnlyView

public static SubFileChannel newReadOnlyView(FileChannel file,
                                             long startPosition,
                                             long endPosition)
                                      throws IOException
Throws:
IOException

force

public void force(boolean metaData)
           throws IOException
Overrides:
force in class FilterFileChannel
Throws:
IOException

lock

public FileLock lock(long position,
                     long size,
                     boolean shared)
              throws IOException
Overrides:
lock in class FilterFileChannel
Throws:
IOException

map

public MappedByteBuffer map(FileChannel.MapMode mode,
                            long position,
                            long size)
                     throws IOException
Overrides:
map in class FilterFileChannel
Throws:
IOException

position

public long position()
              throws IOException
Overrides:
position in class FilterFileChannel
Throws:
IOException

position

public FileChannel position(long newPosition)
                     throws IOException
Overrides:
position in class FilterFileChannel
Throws:
IOException

read

public int read(ByteBuffer dst,
                long position)
         throws IOException
Overrides:
read in class FilterFileChannel
Throws:
IOException

read

public int read(ByteBuffer dst)
         throws IOException
Specified by:
read in interface ReadableByteChannel
Overrides:
read in class FilterFileChannel
Throws:
IOException

read

public long read(ByteBuffer[] dsts,
                 int offset,
                 int length)
          throws IOException
Specified by:
read in interface ScatteringByteChannel
Overrides:
read in class FilterFileChannel
Throws:
IOException

size

public long size()
          throws IOException
Overrides:
size in class FilterFileChannel
Throws:
IOException

transferFrom

public long transferFrom(ReadableByteChannel src,
                         long position,
                         long count)
                  throws IOException
Overrides:
transferFrom in class FilterFileChannel
Throws:
IOException

transferTo

public long transferTo(long position,
                       long count,
                       WritableByteChannel target)
                throws IOException
Overrides:
transferTo in class FilterFileChannel
Throws:
IOException

truncate

public FileChannel truncate(long size)
                     throws IOException
Overrides:
truncate in class FilterFileChannel
Throws:
IOException

tryLock

public FileLock tryLock(long position,
                        long size,
                        boolean shared)
                 throws IOException
Overrides:
tryLock in class FilterFileChannel
Throws:
IOException

write

public int write(ByteBuffer src,
                 long position)
          throws IOException
Overrides:
write in class FilterFileChannel
Throws:
IOException

write

public int write(ByteBuffer src)
          throws IOException
Specified by:
write in interface WritableByteChannel
Overrides:
write in class FilterFileChannel
Throws:
IOException

write

public long write(ByteBuffer[] srcs,
                  int offset,
                  int length)
           throws IOException
Specified by:
write in interface GatheringByteChannel
Overrides:
write in class FilterFileChannel
Throws:
IOException

implCreateFileLock

protected FilterFileChannel.FilterFileLock implCreateFileLock(FileLock innerLock)
                                                       throws IOException
Creates and returns a position-shifted FileLock.

Overrides:
implCreateFileLock in class FilterFileChannel
Throws:
IOException

checkWriteAccess

protected void checkWriteAccess()
                         throws IOException
Checks that the instance has write access. Called on methods that may attempt to change the contents of the channel.

This implementation simply checks that the access mode at instantiation was not SubFileChannel.AccessMode.READ_ONLY; o.w. an java.io.IOException is thrown.

Overrides:
checkWriteAccess in class FilterFileChannel
Throws:
IOException

checkAccess

protected void checkAccess()
                    throws IOException
Checks that the instance has read access. Called on methods that attempt to read from the channel. The base implementation does nothing.

Subclasses overriding this method should ensure that if an instance has write access (checkWriteAccess() does not throw an exception), then it also has read access.

Throws:
IOException - thrown if the instance does not have read access


SourceForge.net Logo