Class S3FileChannel
- java.lang.Object
-
- java.nio.channels.spi.AbstractInterruptibleChannel
-
- java.nio.channels.FileChannel
-
- software.amazon.nio.spi.s3.S3FileChannel
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.nio.channels.ByteChannel,java.nio.channels.Channel,java.nio.channels.GatheringByteChannel,java.nio.channels.InterruptibleChannel,java.nio.channels.ReadableByteChannel,java.nio.channels.ScatteringByteChannel,java.nio.channels.SeekableByteChannel,java.nio.channels.WritableByteChannel
public class S3FileChannel extends java.nio.channels.FileChannel
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidforce(boolean metaData)Forces any updates to this channel's file to be written to the storage device that contains it.protected voidimplCloseChannel()Closes this channel.java.nio.channels.FileLocklock(long position, long size, boolean shared)Acquires a lock on the given region of this channel's file.java.nio.MappedByteBuffermap(java.nio.channels.FileChannel.MapMode mode, long position, long size)This method is not supported by this implementation, and theIOExceptionthrown always includes the message "This library current doesn't support MappedByteBuffers".longposition()Returns this channel's file position.java.nio.channels.FileChannelposition(long newPosition)Sets this channel's file position.intread(java.nio.ByteBuffer dst)Reads a sequence of bytes from this channel into the given buffer.longread(java.nio.ByteBuffer[] dsts, int offset, int length)Reads a sequence of bytes from this channel into a subsequence of the given buffers.intread(java.nio.ByteBuffer dst, long position)Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.longsize()Returns the current size of this channel's file.longtransferFrom(java.nio.channels.ReadableByteChannel src, long position, long count)Transfers bytes into this channel's file from the given readable byte channel.longtransferTo(long position, long count, java.nio.channels.WritableByteChannel target)Transfers bytes from this channel's file to the given writable byte channel.java.nio.channels.FileChanneltruncate(long size)Truncates this channel's file to the given size.java.nio.channels.FileLocktryLock(long position, long size, boolean shared)Attempts to acquire a lock on the given region of this channel's file.intwrite(java.nio.ByteBuffer src)Writes a sequence of bytes to this channel from the given buffer.longwrite(java.nio.ByteBuffer[] srcs, int offset, int length)Writes a sequence of bytes to this channel from a subsequence of the given buffers.intwrite(java.nio.ByteBuffer src, long position)Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.-
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel
begin, close, end, isOpen
-
-
-
-
Method Detail
-
read
public int read(java.nio.ByteBuffer dst) throws java.io.IOExceptionReads a sequence of bytes from this channel into the given buffer.Bytes are read starting at this channel's current file position, and then the file position is updated with the number of bytes actually read. Otherwise this method behaves exactly as specified in the
ReadableByteChannelinterface.- Specified by:
readin interfacejava.nio.channels.ReadableByteChannel- Specified by:
readin interfacejava.nio.channels.SeekableByteChannel- Specified by:
readin classjava.nio.channels.FileChannel- Parameters:
dst- the destination to read bytes into.- Throws:
java.io.IOException
-
read
public long read(java.nio.ByteBuffer[] dsts, int offset, int length) throws java.io.IOExceptionReads a sequence of bytes from this channel into a subsequence of the given buffers.Bytes are read starting at this channel's current file position, and then the file position is updated with the number of bytes actually read. Otherwise this method behaves exactly as specified in the
ScatteringByteChannelinterface.- Specified by:
readin interfacejava.nio.channels.ScatteringByteChannel- Specified by:
readin classjava.nio.channels.FileChannel- Parameters:
dsts- the buffer arrayoffset- the index of the destination buffer in the buffer array to start reading intolength- the number of buffers in the array to read bytes into- Returns:
- the number of bytes read into the buffer
- Throws:
java.io.IOException
-
write
public int write(java.nio.ByteBuffer src) throws java.io.IOExceptionWrites a sequence of bytes to this channel from the given buffer.Bytes are written starting at this channel's current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file. The file is grown, if necessary, to accommodate the written bytes, and then the file position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified by the
WritableByteChannelinterface.- Specified by:
writein interfacejava.nio.channels.SeekableByteChannel- Specified by:
writein interfacejava.nio.channels.WritableByteChannel- Specified by:
writein classjava.nio.channels.FileChannel- Parameters:
src- the source of the bytes to write into this channel- Returns:
- the number of bytes actually written
- Throws:
java.io.IOException
-
write
public long write(java.nio.ByteBuffer[] srcs, int offset, int length) throws java.io.IOExceptionWrites a sequence of bytes to this channel from a subsequence of the given buffers.Bytes are written starting at this channel's current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file. The file is grown, if necessary, to accommodate the written bytes, and then the file position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified in the
GatheringByteChannelinterface.- Specified by:
writein interfacejava.nio.channels.GatheringByteChannel- Specified by:
writein classjava.nio.channels.FileChannel- Parameters:
srcs- the source of the bytes to write into this channeloffset- the index of the first buffer in the buffer array to write bytes fromlength- the maximum number of buffers to write bytes from- Returns:
- the number of bytes actually written
- Throws:
java.io.IOException
-
position
public long position() throws java.io.IOExceptionReturns this channel's file position.- Specified by:
positionin interfacejava.nio.channels.SeekableByteChannel- Specified by:
positionin classjava.nio.channels.FileChannel- Returns:
- This channel's file position, a non-negative integer counting the number of bytes from the beginning of the file to the current position
- Throws:
java.nio.channels.ClosedChannelException- If this channel is closedjava.io.IOException- If some other I/O error occurs
-
position
public java.nio.channels.FileChannel position(long newPosition) throws java.io.IOExceptionSets this channel's file position.Setting the position to a value that is greater than the file's current size is legal but does not change the size of the file. 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 file to be grown to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.
- Specified by:
positionin interfacejava.nio.channels.SeekableByteChannel- Specified by:
positionin classjava.nio.channels.FileChannel- Parameters:
newPosition- The new position, a non-negative integer counting the number of bytes from the beginning of the file- Returns:
- This file channel
- Throws:
java.nio.channels.ClosedChannelException- If this channel is closedjava.lang.IllegalArgumentException- If the new position is negativejava.io.IOException- If some other I/O error occurs
-
size
public long size() throws java.io.IOExceptionReturns the current size of this channel's file.- Specified by:
sizein interfacejava.nio.channels.SeekableByteChannel- Specified by:
sizein classjava.nio.channels.FileChannel- Returns:
- The current size of this channel's file, measured in bytes
- Throws:
java.nio.channels.ClosedChannelException- If this channel is closedjava.io.IOException- If some other I/O error occurs
-
truncate
public java.nio.channels.FileChannel truncate(long size) throws java.io.IOExceptionTruncates this channel's file to the given size.If the given size is less than the file's current size then the file is truncated, discarding any bytes beyond the new end of the file. If the given size is greater than or equal to the file's current size then the file is not modified. In either case, if this channel's file position is greater than the given size then it is set to that size.
- Specified by:
truncatein interfacejava.nio.channels.SeekableByteChannel- Specified by:
truncatein classjava.nio.channels.FileChannel- Parameters:
size- The new size, a non-negative byte count- Returns:
- This file channel
- Throws:
java.nio.channels.NonWritableChannelException- If this channel was not opened for writingjava.nio.channels.ClosedChannelException- If this channel is closedjava.lang.IllegalArgumentException- If the new size is negativejava.io.IOException- If some other I/O error occurs
-
force
public void force(boolean metaData) throws java.io.IOExceptionForces any updates to this channel's file to be written to the storage device that contains it.If this channel's file resides on a local storage device then when this method returns it is guaranteed that all changes made to the file since this channel was created, or since this method was last invoked, will have been written to that device. This is useful for ensuring that critical information is not lost in the event of a system crash.
If the file does not reside on a local device then no such guarantee is made.
The
metaDataparameter can be used to limit the number of I/O operations that this method is required to perform. Passingfalsefor this parameter indicates that only updates to the file's content need be written to storage; passingtrueindicates that updates to both the file's content and metadata must be written, which generally requires at least one more I/O operation. Whether this parameter actually has any effect is dependent upon the underlying operating system and is therefore unspecified.Invoking this method may cause an I/O operation to occur even if the channel was only opened for reading. Some operating systems, for example, maintain a last-access time as part of a file's metadata, and this time is updated whenever the file is read. Whether or not this is actually done is system-dependent and is therefore unspecified.
This method is only guaranteed to force changes that were made to this channel's file via the methods defined in this class. It may or may not force changes that were made by modifying the content of a
mapped byte bufferobtained by invoking themapmethod. Invoking theforcemethod of the mapped byte buffer will force changes made to the buffer's content to be written.- Specified by:
forcein classjava.nio.channels.FileChannel- Parameters:
metaData- Iftruethen this method is required to force changes to both the file's content and metadata to be written to storage; otherwise, it need only force content changes to be written- Throws:
java.nio.channels.ClosedChannelException- If this channel is closedjava.io.IOException- If some other I/O error occurs
-
transferTo
public long transferTo(long position, long count, java.nio.channels.WritableByteChannel target) throws java.io.IOExceptionTransfers bytes from this channel's file to the given writable byte channel.An attempt is made to read up to
countbytes starting at the givenpositionin this channel's file and write them to the target channel. An invocation of this method may or may not transfer all of the requested bytes; whether or not it does so depends upon the natures and states of the channels. Fewer than the requested number of bytes are transferred if this channel's file contains fewer thancountbytes starting at the givenposition, or if the target channel is non-blocking and it has fewer thancountbytes free in its output buffer.This method does not modify this channel's position. If the given position is greater than the file's current size then no bytes are transferred. If the target channel has a position then bytes are written starting at that position and then the position is incremented by the number of bytes written.
This method is potentially much more efficient than a simple loop that reads from this channel and writes to the target channel. Many operating systems can transfer bytes directly from the filesystem cache to the target channel without actually copying them.
- Specified by:
transferToin classjava.nio.channels.FileChannel- Parameters:
position- The position within the file at which the transfer is to begin; must be non-negativecount- The maximum number of bytes to be transferred; must be non-negativetarget- The target channel- Returns:
- The number of bytes, possibly zero, that were actually transferred
- Throws:
java.lang.IllegalArgumentException- If the preconditions on the parameters do not holdjava.nio.channels.NonReadableChannelException- If this channel was not opened for readingjava.nio.channels.NonWritableChannelException- If the target channel was not opened for writingjava.nio.channels.ClosedChannelException- If either this channel or the target channel is closedjava.nio.channels.AsynchronousCloseException- If another thread closes either channel while the transfer is in progressjava.nio.channels.ClosedByInterruptException- If another thread interrupts the current thread while the transfer is in progress, thereby closing both channels and setting the current thread's interrupt statusjava.io.IOException- If some other I/O error occurs
-
transferFrom
public long transferFrom(java.nio.channels.ReadableByteChannel src, long position, long count) throws java.io.IOExceptionTransfers bytes into this channel's file from the given readable byte channel.An attempt is made to read up to
countbytes from the source channel and write them to this channel's file starting at the givenposition. An invocation of this method may or may not transfer all of the requested bytes; whether or not it does so depends upon the natures and states of the channels. Fewer than the requested number of bytes will be transferred if the source channel has fewer thancountbytes remaining, or if the source channel is non-blocking and has fewer thancountbytes immediately available in its input buffer.This method does not modify this channel's position. If the given position is greater than the file's current size then no bytes are transferred. If the source channel has a position then bytes are read starting at that position and then the position is incremented by the number of bytes read.
This method is potentially much more efficient than a simple loop that reads from the source channel and writes to this channel. Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them.
- Specified by:
transferFromin classjava.nio.channels.FileChannel- Parameters:
src- The source channelposition- The position within the file at which the transfer is to begin; must be non-negativecount- The maximum number of bytes to be transferred; must be non-negative- Returns:
- The number of bytes, possibly zero, that were actually transferred
- Throws:
java.lang.IllegalArgumentException- If the preconditions on the parameters do not holdjava.nio.channels.NonReadableChannelException- If the source channel was not opened for readingjava.nio.channels.NonWritableChannelException- If this channel was not opened for writingjava.nio.channels.ClosedChannelException- If either this channel or the source channel is closedjava.nio.channels.AsynchronousCloseException- If another thread closes either channel while the transfer is in progressjava.nio.channels.ClosedByInterruptException- If another thread interrupts the current thread while the transfer is in progress, thereby closing both channels and setting the current thread's interrupt statusjava.io.IOException- If some other I/O error occurs
-
read
public int read(java.nio.ByteBuffer dst, long position) throws java.io.IOExceptionReads a sequence of bytes from this channel into the given buffer, starting at the given file position.This method works in the same manner as the
read(ByteBuffer)method, except that bytes are read starting at the given file position rather than at the channel's current position. This method does not modify this channel's position. If the given position is greater than the file's current size then no bytes are read.- Specified by:
readin classjava.nio.channels.FileChannel- Parameters:
dst- The buffer into which bytes are to be transferredposition- The file position at which the transfer is to begin; must be non-negative- Returns:
- The number of bytes read, possibly zero, or
-1if the given position is greater than or equal to the file's current size - Throws:
java.lang.IllegalArgumentException- If the position is negativejava.nio.channels.NonReadableChannelException- If this channel was not opened for readingjava.nio.channels.ClosedChannelException- If this channel is closedjava.nio.channels.AsynchronousCloseException- If another thread closes this channel while the read operation is in progressjava.nio.channels.ClosedByInterruptException- If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt statusjava.io.IOException- If some other I/O error occurs
-
write
public int write(java.nio.ByteBuffer src, long position) throws java.io.IOExceptionWrites a sequence of bytes to this channel from the given buffer, starting at the given file position.This method works in the same manner as the
write(ByteBuffer)method, except that bytes are written starting at the given file position rather than at the channel's current position. This method does not modify this channel's position. If the given position is greater than the file's current size then the file will be grown to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.- Specified by:
writein classjava.nio.channels.FileChannel- Parameters:
src- The buffer from which bytes are to be transferredposition- The file position at which the transfer is to begin; must be non-negative- Returns:
- The number of bytes written, possibly zero
- Throws:
java.lang.IllegalArgumentException- If the position is negativejava.nio.channels.NonWritableChannelException- If this channel was not opened for writingjava.nio.channels.ClosedChannelException- If this channel is closedjava.nio.channels.AsynchronousCloseException- If another thread closes this channel while the write operation is in progressjava.nio.channels.ClosedByInterruptException- If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt statusjava.io.IOException- If some other I/O error occurs
-
map
public java.nio.MappedByteBuffer map(java.nio.channels.FileChannel.MapMode mode, long position, long size) throws java.io.IOExceptionThis method is not supported by this implementation, and theIOExceptionthrown always includes the message "This library current doesn't support MappedByteBuffers".- Specified by:
mapin classjava.nio.channels.FileChannel- Parameters:
mode- One of the constantsREAD_ONLY,READ_WRITE, orPRIVATEdefined in theFileChannel.MapModeclass, according to whether the file is to be mapped read-only, read/write, or privately (copy-on-write), respectivelyposition- The position within the file at which the mapped region is to start; must be non-negativesize- The size of the region to be mapped; must be non-negative and no greater thanInteger.MAX_VALUE- Returns:
- Never returns, always throws an exception
- Throws:
java.io.IOException- Always throws an exception
-
lock
public java.nio.channels.FileLock lock(long position, long size, boolean shared) throws java.io.IOExceptionAcquires a lock on the given region of this channel's file.An invocation of this method will block until the region can be locked, this channel is closed, or the invoking thread is interrupted, whichever comes first.
If this channel is closed by another thread during an invocation of this method then an
AsynchronousCloseExceptionwill be thrown.If the invoking thread is interrupted while waiting to acquire the lock then its interrupt status will be set and a
FileLockInterruptionExceptionwill be thrown. If the invoker's interrupt status is set when this method is invoked then that exception will be thrown immediately; the thread's interrupt status will not be changed.The region specified by the
positionandsizeparameters need not be contained within, or even overlap, the actual underlying file. Lock regions are fixed in size; if a locked region initially contains the end of the file and the file grows beyond the region then the new portion of the file will not be covered by the lock. If a file is expected to grow in size and a lock on the entire file is required then a region starting at zero, and no smaller than the expected maximum size of the file, should be locked. The zero-argumentFileChannel.lock()method simply locks a region of sizeLong.MAX_VALUE.Some operating systems do not support shared locks, in which case a request for a shared lock is automatically converted into a request for an exclusive lock. Whether the newly-acquired lock is shared or exclusive may be tested by invoking the resulting lock object's
isSharedmethod.File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.
- Specified by:
lockin classjava.nio.channels.FileChannel- Parameters:
position- The position at which the locked region is to start; must be non-negativesize- The size of the locked region; must be non-negative, and the sumposition+sizemust be non-negativeshared-trueto request a shared lock, in which case this channel must be open for reading (and possibly writing);falseto request an exclusive lock, in which case this channel must be open for writing (and possibly reading)- Returns:
- A lock object representing the newly-acquired lock
- Throws:
java.lang.IllegalArgumentException- If the preconditions on the parameters do not holdjava.nio.channels.ClosedChannelException- If this channel is closedjava.nio.channels.AsynchronousCloseException- If another thread closes this channel while the invoking thread is blocked in this methodjava.nio.channels.FileLockInterruptionException- If the invoking thread is interrupted while blocked in this methodjava.nio.channels.OverlappingFileLockException- If a lock that overlaps the requested region is already held by this Java virtual machine, or if another thread is already blocked in this method and is attempting to lock an overlapping regionjava.nio.channels.NonReadableChannelException- Ifsharedistruethis channel was not opened for readingjava.nio.channels.NonWritableChannelException- Ifsharedisfalsebut this channel was not opened for writingjava.io.IOException- If some other I/O error occurs- See Also:
FileChannel.lock(),FileChannel.tryLock(),tryLock(long, long, boolean)
-
tryLock
public java.nio.channels.FileLock tryLock(long position, long size, boolean shared) throws java.io.IOExceptionAttempts to acquire a lock on the given region of this channel's file.This method does not block. An invocation always returns immediately, either having acquired a lock on the requested region or having failed to do so. If it fails to acquire a lock because an overlapping lock is held by another program then it returns
null. If it fails to acquire a lock for any other reason then an appropriate exception is thrown.The region specified by the
positionandsizeparameters need not be contained within, or even overlap, the actual underlying file. Lock regions are fixed in size; if a locked region initially contains the end of the file and the file grows beyond the region then the new portion of the file will not be covered by the lock. If a file is expected to grow in size and a lock on the entire file is required then a region starting at zero, and no smaller than the expected maximum size of the file, should be locked. The zero-argumentFileChannel.tryLock()method simply locks a region of sizeLong.MAX_VALUE.Some operating systems do not support shared locks, in which case a request for a shared lock is automatically converted into a request for an exclusive lock. Whether the newly-acquired lock is shared or exclusive may be tested by invoking the resulting lock object's
isSharedmethod.File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.
- Specified by:
tryLockin classjava.nio.channels.FileChannel- Parameters:
position- The position at which the locked region is to start; must be non-negativesize- The size of the locked region; must be non-negative, and the sumposition+sizemust be non-negativeshared-trueto request a shared lock,falseto request an exclusive lock- Returns:
- A lock object representing the newly-acquired lock,
or
nullif the lock could not be acquired because another program holds an overlapping lock - Throws:
java.lang.IllegalArgumentException- If the preconditions on the parameters do not holdjava.nio.channels.ClosedChannelException- If this channel is closedjava.nio.channels.OverlappingFileLockException- If a lock that overlaps the requested region is already held by this Java virtual machine, or if another thread is already blocked in this method and is attempting to lock an overlapping region of the same filejava.io.IOException- If some other I/O error occurs- See Also:
FileChannel.lock(),lock(long, long, boolean),FileChannel.tryLock()
-
implCloseChannel
protected void implCloseChannel() throws java.io.IOExceptionCloses this channel.This method is invoked by the
closemethod in order to perform the actual work of closing the channel. This method is only invoked if the channel has not yet been closed, and it is never invoked more than once.An implementation of this method must arrange for any other thread that is blocked in an I/O operation upon this channel to return immediately, either by throwing an exception or by returning normally.
- Specified by:
implCloseChannelin classjava.nio.channels.spi.AbstractInterruptibleChannel- Throws:
java.io.IOException- If an I/O error occurs while closing the channel
-
-