Class S3SeekableByteChannel

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.ByteChannel, java.nio.channels.Channel, java.nio.channels.ReadableByteChannel, java.nio.channels.SeekableByteChannel, java.nio.channels.WritableByteChannel

    public class S3SeekableByteChannel
    extends java.lang.Object
    implements java.nio.channels.SeekableByteChannel
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected S3SeekableByteChannel​(S3Path s3Path)  
      protected S3SeekableByteChannel​(S3Path s3Path, long startAt)
      Deprecated.
      startAt is only a valid parameter for the read mode and is therefore discouraged to be used during creation of the channel
      protected S3SeekableByteChannel​(S3Path s3Path, software.amazon.awssdk.services.s3.S3AsyncClient s3Client)  
      protected S3SeekableByteChannel​(S3Path s3Path, software.amazon.awssdk.services.s3.S3AsyncClient s3Client, long startAt)
      Deprecated.
      startAt is only a valid parameter for the read mode and is therefore discouraged to be used during creation of the channel
      protected S3SeekableByteChannel​(S3Path s3Path, software.amazon.awssdk.services.s3.S3AsyncClient s3Client, java.util.Set<? extends java.nio.file.OpenOption> options)  
      protected S3SeekableByteChannel​(S3Path s3Path, software.amazon.awssdk.services.s3.S3AsyncClient s3Client, java.util.Set<? extends java.nio.file.OpenOption> options, long timeout, java.util.concurrent.TimeUnit timeUnit)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes this channel.
      protected java.nio.channels.ReadableByteChannel getReadDelegate()
      Access the underlying ReadableByteChannel used for reading
      protected java.nio.channels.WritableByteChannel getWriteDelegate()
      Access the underlying WritableByteChannel used for writing
      boolean isOpen()
      Tells whether this channel is open.
      long position()
      Returns this channel's position.
      java.nio.channels.SeekableByteChannel position​(long newPosition)
      Sets this channel's position.
      int read​(java.nio.ByteBuffer dst)
      Reads a sequence of bytes from this channel into the given buffer.
      long size()
      Returns the current size of entity to which this channel is connected.
      java.nio.channels.SeekableByteChannel truncate​(long size)
      Truncates the entity, to which this channel is connected, to the given size.
      int write​(java.nio.ByteBuffer src)
      Writes a sequence of bytes to this channel from the given buffer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • S3SeekableByteChannel

        protected S3SeekableByteChannel​(S3Path s3Path,
                                        software.amazon.awssdk.services.s3.S3AsyncClient s3Client)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • S3SeekableByteChannel

        @Deprecated
        protected S3SeekableByteChannel​(S3Path s3Path,
                                        software.amazon.awssdk.services.s3.S3AsyncClient s3Client,
                                        long startAt)
                                 throws java.io.IOException
        Deprecated.
        startAt is only a valid parameter for the read mode and is therefore discouraged to be used during creation of the channel
        Parameters:
        s3Client - the s3 client to use to obtain bytes for the byte channel
        s3Path - the path to the file
        startAt - the position to start at
        Throws:
        java.io.IOException - if there is an error creating the channel
      • S3SeekableByteChannel

        @Deprecated
        protected S3SeekableByteChannel​(S3Path s3Path,
                                        long startAt)
                                 throws java.io.IOException
        Deprecated.
        startAt is only a valid parameter for the read mode and is therefore discouraged to be used during creation of the channel
        Parameters:
        s3Path - the path to the file
        startAt - the position to start at
        Throws:
        java.io.IOException - if there is an error creating the channel
      • S3SeekableByteChannel

        protected S3SeekableByteChannel​(S3Path s3Path)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • S3SeekableByteChannel

        protected S3SeekableByteChannel​(S3Path s3Path,
                                        software.amazon.awssdk.services.s3.S3AsyncClient s3Client,
                                        java.util.Set<? extends java.nio.file.OpenOption> options)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • S3SeekableByteChannel

        protected S3SeekableByteChannel​(S3Path s3Path,
                                        software.amazon.awssdk.services.s3.S3AsyncClient s3Client,
                                        java.util.Set<? extends java.nio.file.OpenOption> options,
                                        long timeout,
                                        java.util.concurrent.TimeUnit timeUnit)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • read

        public int read​(java.nio.ByteBuffer dst)
                 throws java.io.IOException
        Reads a sequence of bytes from this channel into the given buffer.

        Bytes are read starting at this channel's current position, and then the position is updated with the number of bytes actually read. Otherwise, this method behaves exactly as specified in the ReadableByteChannel interface.

        Specified by:
        read in interface java.nio.channels.ReadableByteChannel
        Specified by:
        read in interface java.nio.channels.SeekableByteChannel
        Parameters:
        dst - the destination buffer
        Returns:
        the number of bytes read or -1 if no more bytes can be read.
        Throws:
        java.io.IOException
      • write

        public int write​(java.nio.ByteBuffer src)
                  throws java.io.IOException
        Writes a sequence of bytes to this channel from the given buffer.

        Bytes are written starting at this channel's current position, unless the channel is connected to an entity such as a file that is opened with the APPEND option, in which case the position is first advanced to the end. The entity to which the channel is connected will grow to accommodate the written bytes, and the position updates with the number of bytes actually written. Otherwise, this method behaves exactly as specified by the WritableByteChannel interface.

        Specified by:
        write in interface java.nio.channels.SeekableByteChannel
        Specified by:
        write in interface java.nio.channels.WritableByteChannel
        Parameters:
        src - the src of the bytes to write to this channel
        Throws:
        java.io.IOException
      • position

        public long position()
                      throws java.io.IOException
        Returns this channel's position.
        Specified by:
        position in interface java.nio.channels.SeekableByteChannel
        Returns:
        This channel's position, a non-negative integer counting the number of bytes from the beginning of the entity to the current position
        Throws:
        java.io.IOException
      • position

        public java.nio.channels.SeekableByteChannel position​(long newPosition)
                                                       throws java.io.IOException
        Sets this channel's position.

        Setting the position to a value that is greater than the current size is legal but does not change the size of the entity. 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 entity to grow to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.

        Setting the channel's position is not recommended when connected to an entity, typically a file, that is opened with the APPEND option. When opened for append, the position is first advanced to the end before writing.

        Specified by:
        position in interface java.nio.channels.SeekableByteChannel
        Parameters:
        newPosition - The new position, a non-negative integer counting the number of bytes from the beginning of the entity
        Returns:
        This channel
        Throws:
        java.nio.channels.ClosedChannelException - If this channel is closed
        java.lang.IllegalArgumentException - If the new position is negative
        java.io.IOException - If some other I/O error occurs
      • size

        public long size()
                  throws java.io.IOException
        Returns the current size of entity to which this channel is connected.
        Specified by:
        size in interface java.nio.channels.SeekableByteChannel
        Returns:
        The current size, measured in bytes
        Throws:
        java.io.IOException - If some other I/O error occurs
      • truncate

        public java.nio.channels.SeekableByteChannel truncate​(long size)
        Truncates the entity, to which this channel is connected, to the given size.

        If the given size is less than the current size then the entity is truncated, discarding any bytes beyond the new end. If the given size is greater than or equal to the current size then the entity is not modified. In either case, if the current position is greater than the given size then it is set to that size.

        An implementation of this interface may prohibit truncation when connected to an entity, typically a file, opened with the APPEND option.

        Specified by:
        truncate in interface java.nio.channels.SeekableByteChannel
        Parameters:
        size - The new size, a non-negative byte count
        Returns:
        This channel
      • isOpen

        public boolean isOpen()
        Tells whether this channel is open.
        Specified by:
        isOpen in interface java.nio.channels.Channel
        Returns:
        true if, and only if, this channels delegate is open
      • close

        public void close()
                   throws java.io.IOException
        Closes this channel.

        After a channel is closed, any further attempt to invoke I/O operations upon it will cause a ClosedChannelException to be thrown.

        If this channel is already closed then invoking this method has no effect.

        This method may be invoked at any time. If some other thread has already invoked it, however, then another invocation will block until the first invocation is complete, after which it will return without effect.

        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • getReadDelegate

        protected java.nio.channels.ReadableByteChannel getReadDelegate()
        Access the underlying ReadableByteChannel used for reading
        Returns:
        the channel. May be null if opened for writing only
      • getWriteDelegate

        protected java.nio.channels.WritableByteChannel getWriteDelegate()
        Access the underlying WritableByteChannel used for writing
        Returns:
        the channel. May be null if opened for reading only