Class SeekableInMemoryByteChannel
- java.lang.Object
-
- org.apache.commons.compress.utils.SeekableInMemoryByteChannel
-
- 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 SeekableInMemoryByteChannel extends java.lang.Object implements java.nio.channels.SeekableByteChannel
ASeekableByteChannel
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 viaarray()
.- Since:
- 1.13
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.atomic.AtomicBoolean
closed
private byte[]
data
private static int
NAIVE_RESIZE_LIMIT
private int
position
private int
size
-
Constructor Summary
Constructors Constructor 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]
array()
Obtains the array backing this channel.void
close()
private void
ensureOpen()
boolean
isOpen()
long
position()
java.nio.channels.SeekableByteChannel
position(long newPosition)
int
read(java.nio.ByteBuffer buf)
private void
repositionIfNecessary()
private void
resize(int newLength)
long
size()
java.nio.channels.SeekableByteChannel
truncate(long newSize)
int
write(java.nio.ByteBuffer b)
-
-
-
Field Detail
-
NAIVE_RESIZE_LIMIT
private static final int NAIVE_RESIZE_LIMIT
- See Also:
- Constant Field Values
-
data
private byte[] data
-
closed
private final java.util.concurrent.atomic.AtomicBoolean closed
-
position
private int position
-
size
private int size
-
-
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 interfacejava.nio.channels.SeekableByteChannel
-
position
public java.nio.channels.SeekableByteChannel position(long newPosition) throws java.io.IOException
- Specified by:
position
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
-
size
public long size()
- Specified by:
size
in interfacejava.nio.channels.SeekableByteChannel
-
truncate
public java.nio.channels.SeekableByteChannel truncate(long newSize)
- Specified by:
truncate
in interfacejava.nio.channels.SeekableByteChannel
-
read
public int read(java.nio.ByteBuffer buf) throws java.io.IOException
- Specified by:
read
in interfacejava.nio.channels.ReadableByteChannel
- Specified by:
read
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.nio.channels.Channel
- Specified by:
close
in interfacejava.io.Closeable
-
isOpen
public boolean isOpen()
- Specified by:
isOpen
in interfacejava.nio.channels.Channel
-
write
public int write(java.nio.ByteBuffer b) throws java.io.IOException
- Specified by:
write
in interfacejava.nio.channels.SeekableByteChannel
- Specified by:
write
in interfacejava.nio.channels.WritableByteChannel
- Throws:
java.io.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.
-
resize
private void resize(int newLength)
-
ensureOpen
private void ensureOpen() throws java.nio.channels.ClosedChannelException
- Throws:
java.nio.channels.ClosedChannelException
-
repositionIfNecessary
private void repositionIfNecessary()
-
-