info.aduna.io
Class NioFile

java.lang.Object
  extended by info.aduna.io.NioFile

public final class NioFile
extends Object

File wrapper that protects against concurrent file closing events due to e.g. thread interrupts. In case the file channel that is used by this class is closed due to such an event, it will try to reopen the channel. The thread that causes the ClosedByInterruptException is not protected, assuming the interrupt is intended to end the thread's operation.

Author:
Arjohn Kampman

Constructor Summary
NioFile(File file)
           
 
Method Summary
 void close()
           
 boolean delete()
          Closed any open channels and then deletes the file.
 void force(boolean metaData)
          Performs a protected FileChannel.force(boolean) call.
 File getFile()
           
 boolean isClosed()
           
 int read(ByteBuffer buf, long offset)
          Performs a protected FileChannel.read(ByteBuffer, long) call.
 byte readByte(long offset)
           
 byte[] readBytes(long offset, int length)
           
 int readInt(long offset)
           
 long readLong(long offset)
           
 long size()
          Performs a protected FileChannel.size() call.
 long transferTo(long position, long count, WritableByteChannel target)
          Performs a protected FileChannel.transferTo(long, long, WritableByteChannel) call.
 void truncate(long size)
          Performs a protected FileChannel.truncate(long) call.
 int write(ByteBuffer buf, long offset)
          Performs a protected FileChannel.write(ByteBuffer, long) call.
 void writeByte(byte value, long offset)
           
 void writeBytes(byte[] value, long offset)
           
 void writeInt(int value, long offset)
           
 void writeLong(long value, long offset)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NioFile

public NioFile(File file)
        throws IOException
Throws:
IOException
Method Detail

close

public void close()
           throws IOException
Throws:
IOException

isClosed

public boolean isClosed()

getFile

public File getFile()

delete

public boolean delete()
               throws IOException
Closed any open channels and then deletes the file.

Returns:
true if the file has been deleted successfully, false otherwise.
Throws:
IOException - If there was a problem closing the open file channel.

force

public void force(boolean metaData)
           throws IOException
Performs a protected FileChannel.force(boolean) call.

Throws:
IOException

truncate

public void truncate(long size)
              throws IOException
Performs a protected FileChannel.truncate(long) call.

Throws:
IOException

size

public long size()
          throws IOException
Performs a protected FileChannel.size() call.

Throws:
IOException

transferTo

public long transferTo(long position,
                       long count,
                       WritableByteChannel target)
                throws IOException
Performs a protected FileChannel.transferTo(long, long, WritableByteChannel) call.

Throws:
IOException

write

public int write(ByteBuffer buf,
                 long offset)
          throws IOException
Performs a protected FileChannel.write(ByteBuffer, long) call.

Throws:
IOException

read

public int read(ByteBuffer buf,
                long offset)
         throws IOException
Performs a protected FileChannel.read(ByteBuffer, long) call.

Throws:
IOException

writeBytes

public void writeBytes(byte[] value,
                       long offset)
                throws IOException
Throws:
IOException

readBytes

public byte[] readBytes(long offset,
                        int length)
                 throws IOException
Throws:
IOException

writeByte

public void writeByte(byte value,
                      long offset)
               throws IOException
Throws:
IOException

readByte

public byte readByte(long offset)
              throws IOException
Throws:
IOException

writeLong

public void writeLong(long value,
                      long offset)
               throws IOException
Throws:
IOException

readLong

public long readLong(long offset)
              throws IOException
Throws:
IOException

writeInt

public void writeInt(int value,
                     long offset)
              throws IOException
Throws:
IOException

readInt

public int readInt(long offset)
            throws IOException
Throws:
IOException


Copyright © 2011 Aduna. All Rights Reserved.