Class SeekableInMemoryByteChannel
- java.lang.Object
-
- org.jboss.shrinkwrap.api.nio.file.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
SeekableByteChannel
implementation backed by an auto-resizing byte array; thread-safe. Can hold a maxiumum ofInteger.MAX_VALUE
bytes.
-
-
Field Summary
Fields Modifier and Type Field Description private byte[]
contents
Internal buffer for contents; guarded by "this"private boolean
open
Whether or not thisSeekableByteChannel
is open; volatile instead of sync is acceptable because this field participates in no compound computations or invariants with other instance members.private int
position
Current position; guarded by "this"
-
Constructor Summary
Constructors Constructor Description SeekableInMemoryByteChannel()
Creates a new instance with 0 size and 0 position, and open.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
checkClosed()
Throws aClosedChannelException
if thisSeekableByteChannel
is closed.void
close()
private byte[]
concat(byte[] input1, byte[] input2, int position)
Creates a new array which is the concatenated result of the two inputs, at the designated position (to be filled with 0x00) in the case of a gap).(package private) java.io.InputStream
getContents()
Obtain a copy of the contents of thisChannel
as anInputStream
boolean
isOpen()
long
position()
java.nio.channels.SeekableByteChannel
position(long newPosition)
int
read(java.nio.ByteBuffer destination)
long
size()
java.nio.channels.SeekableByteChannel
truncate(long size)
int
write(java.nio.ByteBuffer source)
-
-
-
Field Detail
-
position
private int position
Current position; guarded by "this"
-
open
private volatile boolean open
Whether or not thisSeekableByteChannel
is open; volatile instead of sync is acceptable because this field participates in no compound computations or invariants with other instance members.
-
contents
private byte[] contents
Internal buffer for contents; guarded by "this"
-
-
Method Detail
-
isOpen
public boolean isOpen()
- Specified by:
isOpen
in interfacejava.nio.channels.Channel
- See Also:
Channel.isOpen()
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.nio.channels.Channel
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
- See Also:
Channel.close()
-
read
public int read(java.nio.ByteBuffer destination) throws java.io.IOException
- Specified by:
read
in interfacejava.nio.channels.ReadableByteChannel
- Specified by:
read
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
- See Also:
SeekableByteChannel.read(java.nio.ByteBuffer)
-
write
public int write(java.nio.ByteBuffer source) throws java.io.IOException
- Specified by:
write
in interfacejava.nio.channels.SeekableByteChannel
- Specified by:
write
in interfacejava.nio.channels.WritableByteChannel
- Throws:
java.io.IOException
- See Also:
SeekableByteChannel.write(java.nio.ByteBuffer)
-
concat
private byte[] concat(byte[] input1, byte[] input2, int position)
Creates a new array which is the concatenated result of the two inputs, at the designated position (to be filled with 0x00) in the case of a gap).- Parameters:
input1
-input2
-position
-- Returns:
-
position
public long position() throws java.io.IOException
- Specified by:
position
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
- See Also:
SeekableByteChannel.position()
-
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
- See Also:
SeekableByteChannel.position(long)
-
size
public long size() throws java.io.IOException
- Specified by:
size
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
- See Also:
SeekableByteChannel.size()
-
truncate
public java.nio.channels.SeekableByteChannel truncate(long size) throws java.io.IOException
- Specified by:
truncate
in interfacejava.nio.channels.SeekableByteChannel
- Throws:
java.io.IOException
- See Also:
SeekableByteChannel.truncate(long)
-
getContents
java.io.InputStream getContents()
Obtain a copy of the contents of thisChannel
as anInputStream
- Returns:
-
checkClosed
private void checkClosed() throws java.nio.channels.ClosedChannelException
Throws aClosedChannelException
if thisSeekableByteChannel
is closed.- Throws:
java.nio.channels.ClosedChannelException
-
-