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.SeekableByteChannelASeekableByteChannelimplementation that wraps a byte[].When this channel is used for writing an internal buffer grows to accommodate incoming data. The natural size limit is the value of
Integer.MAX_VALUEand it is not possible toset the positionortruncateto a value bigger than that. Internal buffer can be accessed viaarray().- Since:
- 1.13
-
-
Constructor Summary
Constructors Constructor Description SeekableInMemoryByteChannel()Constructs a new instance using a default empty buffer.SeekableInMemoryByteChannel(byte[] data)Constructs a new instance from a byte array.SeekableInMemoryByteChannel(int size)Constructs a new instance from 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.voidclose()booleanisOpen()longposition()Returns this channel's position.java.nio.channels.SeekableByteChannelposition(long newPosition)intread(java.nio.ByteBuffer buf)longsize()Returns the current size of entity to which this channel is connected.java.nio.channels.SeekableByteChanneltruncate(long newSize)Truncates the entity, to which this channel is connected, to the given size.intwrite(java.nio.ByteBuffer b)
-
-
-
Constructor Detail
-
SeekableInMemoryByteChannel
public SeekableInMemoryByteChannel()
Constructs a new instance using a default empty buffer.
-
SeekableInMemoryByteChannel
public SeekableInMemoryByteChannel(byte[] data)
Constructs a new instance from 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(int size)
Constructs a new instance from 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
-
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.
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.nio.channels.Channel- Specified by:
closein interfacejava.io.Closeable
-
isOpen
public boolean isOpen()
- Specified by:
isOpenin interfacejava.nio.channels.Channel
-
position
public long position()
Returns this channel's position.This method violates the contract of
SeekableByteChannel.position()as it will not throw any exception when invoked on a closed channel. Instead it will return the position the channel had when close has been called.- Specified by:
positionin interfacejava.nio.channels.SeekableByteChannel
-
position
public java.nio.channels.SeekableByteChannel position(long newPosition) throws java.io.IOException- Specified by:
positionin interfacejava.nio.channels.SeekableByteChannel- Throws:
java.io.IOException
-
read
public int read(java.nio.ByteBuffer buf) throws java.io.IOException- Specified by:
readin interfacejava.nio.channels.ReadableByteChannel- Specified by:
readin interfacejava.nio.channels.SeekableByteChannel- Throws:
java.io.IOException
-
size
public long size()
Returns the current size of entity to which this channel is connected.This method violates the contract of
SeekableByteChannel.size()as it will not throw any exception when invoked on a closed channel. Instead it will return the size the channel had when close has been called.- Specified by:
sizein interfacejava.nio.channels.SeekableByteChannel
-
truncate
public java.nio.channels.SeekableByteChannel truncate(long newSize)
Truncates the entity, to which this channel is connected, to the given size.This method violates the contract of
SeekableByteChannel.truncate(long)as it will not throw any exception when invoked on a closed channel.- Specified by:
truncatein interfacejava.nio.channels.SeekableByteChannel- Throws:
java.lang.IllegalArgumentException- if size is negative or bigger than the maximum of a Java integer
-
write
public int write(java.nio.ByteBuffer b) throws java.io.IOException- Specified by:
writein interfacejava.nio.channels.SeekableByteChannel- Specified by:
writein interfacejava.nio.channels.WritableByteChannel- Throws:
java.io.IOException
-
-