public class FileChannelUtility extends Object
FileChannel
s.Modifier and Type | Class and Description |
---|---|
static class |
FileChannelUtility.AsyncTransfer
The AsyncTransfer class encapsulates the state required to make
asynchronous transfer requests.
|
static interface |
FileChannelUtility.IAsyncOpener
Define interface to support callback from readAllAsync
|
static class |
FileChannelUtility.ReopenerInputStream
An InputStream implementation that utilizes the resilient readAll method of this class
|
Constructor and Description |
---|
FileChannelUtility() |
Modifier and Type | Method and Description |
---|---|
static int |
readAll(FileChannel channel,
ByteBuffer src,
long pos)
Deprecated.
by
readAll(IReopenChannel, ByteBuffer, long) which
handles transparent re-opening of the store in order to
complete the read operation. |
static int |
readAll(IReopenChannel<FileChannel> opener,
ByteBuffer src,
long pos)
Reads
Buffer.remaining() bytes into the caller's
ByteBuffer from the channel starting at offset pos. |
static long |
readAllAsync(FileChannelUtility.IAsyncOpener opener,
List<FileChannelUtility.AsyncTransfer> transfers)
readAllAsync will make repeated attempts to read data into the transfer buffers defined
in the List of AsyncTransfer instances.
|
static int |
transferAll(FileChannel sourceChannel,
long fromPosition,
long count,
RandomAccessFile out,
long toPosition)
FileChannel to FileChannel transfer of count bytes
from the sourceChannel starting at the fromPosition onto
the out file starting at its current position. |
static int |
writeAll(FileChannel channel,
ByteBuffer data,
long pos)
Deprecated.
|
static int |
writeAll(IReopenChannel<FileChannel> opener,
ByteBuffer data,
long pos)
Write bytes in data from the position to the limit on the channel
starting at pos.
|
public static int readAll(FileChannel channel, ByteBuffer src, long pos) throws IOException
readAll(IReopenChannel, ByteBuffer, long)
which
handles transparent re-opening of the store in order to
complete the read operation.Buffer.remaining()
bytes into the caller's
ByteBuffer
from the channel starting at offset pos. The
position of the ByteBuffer
is advanced to the limit. The offset
of the channel is NOT modified as a side-effect.
This handles partial reads by reading until nbytes bytes have been transferred.
channel
- The source FileChannel
.src
- A ByteBuffer
into which the data will be read.pos
- The offset from which the data will be read.IOException
IllegalArgumentException
- if any parameter is null
.IllegalArgumentException
- if pos is negative.IllegalArgumentException
- if dst does not have any bytes remaining.public static int readAll(IReopenChannel<FileChannel> opener, ByteBuffer src, long pos) throws IOException
Buffer.remaining()
bytes into the caller's
ByteBuffer
from the channel starting at offset pos. The
position of the ByteBuffer
is advanced to the limit. The offset
of the channel is NOT modified as a side-effect.
This handles partial reads by reading until nbytes bytes have been transferred.
opener
- An object which knows how to re-open the FileChannel
and knows when the resource is no longer available.src
- A ByteBuffer
into which the data will be read.pos
- The offset from which the data will be read.IOException
IllegalArgumentException
- if any parameter is null
.IllegalArgumentException
- if pos is negative.IllegalArgumentException
- if dst does not have any bytes remaining.public static long readAllAsync(FileChannelUtility.IAsyncOpener opener, List<FileChannelUtility.AsyncTransfer> transfers) throws IOException
opener
- provides AsynchronouseFileChanneltransfers
- defines the list of required transfersIOException
public static int writeAll(FileChannel channel, ByteBuffer data, long pos) throws IOException
writeAll(IReopenChannel, ByteBuffer, long)
Note: I have seen count != remaining() for a single invocation of FileChannel#write(). This occurred 5 hours into a run with the write cache disabled (so lots of small record writes). All of a sudden, several writes wound up reporting too few bytes written - this persisted until the end of the run (Fedora core 6 with Sun JDK 1.6.0_03). I have since modified this code to use a loop to ensure that all bytes get written.
IOException
public static int writeAll(IReopenChannel<FileChannel> opener, ByteBuffer data, long pos) throws IOException
Note: I have seen count != remaining() for a single invocation of FileChannel#write(). This occurred 5 hours into a run with the write cache disabled (so lots of small record writes). All of a sudden, several writes wound up reporting too few bytes written - this persisted until the end of the run (Fedora core 6 with Sun JDK 1.6.0_03). I have since modified this code to use a loop to ensure that all bytes get written.
opener
- data
- pos
- IOException
public static int transferAll(FileChannel sourceChannel, long fromPosition, long count, RandomAccessFile out, long toPosition) throws IOException
FileChannel
to FileChannel
transfer of count bytes
from the sourceChannel starting at the fromPosition onto
the out file starting at its current position. The position on the
sourceChannel is updated by this method and will be the positioned
after the last byte transferred (this is done by
FileChannel.transferFrom(java.nio.channels.ReadableByteChannel, long, long)
so we have no choice about that). The position on the out file is
updated by this method and will be located after the last byte
transferred (this is done since we may have to extend the output file in
which case its position will have been changed anyway so this makes the
API more uniform).sourceChannel
- The source FileChannel
.fromPosition
- The first byte to transfer from the sourceChannel.count
- The #of bytes to transfer.out
- The output file.toPosition
- The data will be transferred onto the output channel starting
at this position.IOException
Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.