org.apache.commons.compress.utils

## Class SeekableInMemoryByteChannel

- java.lang.Object
- - org.apache.commons.compress.utils.SeekableInMemoryByteChannel

- All Implemented Interfaces:
    Closeable, AutoCloseable, ByteChannel, Channel, ReadableByteChannel, SeekableByteChannel, WritableByteChannel

      public class SeekableInMemoryByteChannel
      extends Object
      implements SeekableByteChannel

  A `SeekableByteChannel` implementation that wraps a byte[].

  When this channel is used for writing an internal buffer grows to accommodate incoming data. A natural size limit is the value of `Integer.MAX_VALUE`. Internal buffer can be accessed via `array()`.

  Since:
    1.13
  This class is not thread-safe

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `SeekableInMemoryByteChannel()` Parameterless constructor - allocates internal buffer by itself.
    `SeekableInMemoryByteChannel(byte[] data)` Constructor taking a byte array.
    `SeekableInMemoryByteChannel(int size)` Constructor taking a size of storage to be allocated.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `byte[]` | `array()` Obtains the array backing this channel.
    `void` | `close()`
    `boolean` | `isOpen()`
    `long` | `position()`
    `SeekableByteChannel` | `position(long newPosition)`
    `int` | `read(ByteBuffer buf)`
    `long` | `size()`
    `SeekableByteChannel` | `truncate(long newSize)`
    `int` | `write(ByteBuffer b)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### SeekableInMemoryByteChannel

          public SeekableInMemoryByteChannel(byte[] data)

      Constructor taking a byte array.

      This constructor is intended to be used with pre-allocated buffer or when reading from a given byte array.

      Parameters:
        `data` - input data or pre-allocated array.

    - #### SeekableInMemoryByteChannel

          public SeekableInMemoryByteChannel()

      Parameterless constructor - allocates internal buffer by itself.

    - #### SeekableInMemoryByteChannel

          public SeekableInMemoryByteChannel(int size)

      Constructor taking a size of storage to be allocated.

      Creates a channel and allocates internal storage of a given size.

      Parameters:
        `size` - size of internal buffer to allocate, in bytes.

  - ### Method Detail

    - #### position

          public long position()

      Specified by:
        `position` in interface `SeekableByteChannel`

    - #### position

          public SeekableByteChannel position(long newPosition)
                                       throws IOException

      Specified by:
        `position` in interface `SeekableByteChannel`
      Throws:
        `IOException`

    - #### size

          public long size()

      Specified by:
        `size` in interface `SeekableByteChannel`

    - #### truncate

          public SeekableByteChannel truncate(long newSize)

      Specified by:
        `truncate` in interface `SeekableByteChannel`

    - #### read

          public int read(ByteBuffer buf)
                   throws IOException

      Specified by:
        `read` in interface `ReadableByteChannel`
      Specified by:
        `read` in interface `SeekableByteChannel`
      Throws:
        `IOException`

    - #### close

          public void close()

      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Specified by:
        `close` in interface `Channel`

    - #### isOpen

          public boolean isOpen()

      Specified by:
        `isOpen` in interface `Channel`

    - #### write

          public int write(ByteBuffer b)
                    throws IOException

      Specified by:
        `write` in interface `SeekableByteChannel`
      Specified by:
        `write` in interface `WritableByteChannel`
      Throws:
        `IOException`

    - #### array

          public byte[] array()

      Obtains the array backing this channel.

      NOTE: The returned buffer is not aligned with containing data, use `size()` to obtain the size of data stored in the buffer.

      Returns:
        internal byte array.

Copyright © 2016 The Apache Software Foundation. All rights reserved.