@Immutable public final class ImmutableBuffer extends PowerBuffer<ImmutableBuffer>
ByteBuffer
, so you can use
an ImmutableBuffer
wherever you would otherwise use a
ByteBuffer
.
This class protects the identity and state of its adapted byte buffer,
but it does not protect its contents.
Any attempt to obtain the adapted byte buffer()
or change its state
results in an UnsupportedOperationException
.
However, you may be able to change the adapted byte buffer's content with
absolute put
operations unless the buffer is also read-only.
This class is designed to be provided as a method parameter where the caller does not want the callee to change the state of the buffer. Consider the following method signature:
void parse(ImmutableBuffer input) throws InvalidDataException;
Given this signature, it's clear to a caller that the callee cannot change
the buffer's state, so it need not worry about that.
While a similar effect could get achieved by creating a
ByteBuffer.duplicate()
of the buffer before calling this method,
using an ImmutableBuffer
avoids the need to document what the callee
is allowed or expected to do with the state of the given buffer because
its immutable by design.
Furthermore, an ImmutableBuffer
can be passed around to any methods
without the need to make a protective copy before each call, thereby
preventing the pollution of the heap's Eden space and saving some CPU cycles.
Mind again that in order to protect the buffer's content, a caller still
needs to call asReadOnlyBuffer()
.
asMutableBuffer()
Modifier and Type | Method and Description |
---|---|
byte[] |
array()
Deprecated.
|
ImmutableBuffer |
asImmutableBuffer()
Returns an immutable buffer view of the adapted byte buffer.
|
MutableBuffer |
asMutableBuffer()
Returns a mutable buffer view of the adapted byte buffer.
|
ImmutableBuffer |
asReadOnlyBuffer() |
ImmutableBuffer |
bigEndian()
Deprecated.
|
ByteBuffer |
buffer()
Deprecated.
|
ImmutableBuffer |
clear()
Deprecated.
|
ImmutableBuffer |
compact()
Deprecated.
|
ImmutableBuffer |
duplicate() |
ImmutableBuffer |
flip()
Deprecated.
|
byte |
get()
Deprecated.
|
ImmutableBuffer |
get(byte[] dst)
Deprecated.
|
ImmutableBuffer |
get(byte[] dst,
int offset,
int length)
Deprecated.
|
char |
getChar()
Deprecated.
|
double |
getDouble()
Deprecated.
|
float |
getFloat()
Deprecated.
|
int |
getInt()
Deprecated.
|
long |
getLong()
Deprecated.
|
short |
getShort()
Deprecated.
|
int |
getUByte()
Deprecated.
|
long |
getUInt()
Deprecated.
|
int |
getUShort()
Deprecated.
|
boolean |
isMutable()
Returns
true if and only if this power buffer is mutable. |
ImmutableBuffer |
limit(int newLimit)
Deprecated.
|
ImmutableBuffer |
littleEndian()
Deprecated.
|
ImmutableBuffer |
load(ReadableByteChannel channel)
Deprecated.
|
ImmutableBuffer |
mark()
Deprecated.
|
ImmutableBuffer |
order(ByteOrder order)
Deprecated.
|
ImmutableBuffer |
position(int newPosition)
Deprecated.
|
ImmutableBuffer |
put(byte b)
Deprecated.
|
ImmutableBuffer |
put(byte[] src)
Deprecated.
|
ImmutableBuffer |
put(byte[] src,
int offset,
int length)
Deprecated.
|
ImmutableBuffer |
put(ByteBuffer src)
Deprecated.
|
ImmutableBuffer |
putChar(char value)
Deprecated.
|
ImmutableBuffer |
putDouble(double value)
Deprecated.
|
ImmutableBuffer |
putFloat(float value)
Deprecated.
|
ImmutableBuffer |
putInt(int value)
Deprecated.
|
ImmutableBuffer |
putLong(long value)
Deprecated.
|
ImmutableBuffer |
putShort(short value)
Deprecated.
|
ImmutableBuffer |
reset()
Deprecated.
|
ImmutableBuffer |
rewind()
Deprecated.
|
ImmutableBuffer |
save(WritableByteChannel channel)
Deprecated.
|
ImmutableBuffer |
skip(int skip)
Deprecated.
|
ImmutableBuffer |
slice() |
static ImmutableBuffer |
wrap(byte[] array) |
static ImmutableBuffer |
wrap(byte[] array,
int offset,
int length) |
static ImmutableBuffer |
wrap(ByteBuffer buf)
Constructs a new immutable buffer which adapts the given byte
buffer . |
allocate, allocateDirect, arrayOffset, asCharBuffer, asDoubleBuffer, asFloatBuffer, asIntBuffer, asLongBuffer, asShortBuffer, capacity, clone, compareTo, equals, get, getChar, getDouble, getFloat, getInt, getLong, getShort, getUByte, getUInt, getUShort, hasArray, hashCode, hasRemaining, isDirect, isReadOnly, limit, order, position, put, put, putChar, putDouble, putFloat, putInt, putLong, putShort, remaining, toString
@Deprecated public byte[] array()
array
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.array()
public ImmutableBuffer asImmutableBuffer()
PowerBuffer
PowerBuffer.order()
.asImmutableBuffer
in class PowerBuffer<ImmutableBuffer>
public MutableBuffer asMutableBuffer()
PowerBuffer
PowerBuffer.order()
.asMutableBuffer
in class PowerBuffer<ImmutableBuffer>
public ImmutableBuffer asReadOnlyBuffer()
asReadOnlyBuffer
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.asReadOnlyBuffer()
@Deprecated public ImmutableBuffer bigEndian()
PowerBuffer
bigEndian
in class PowerBuffer<ImmutableBuffer>
this
.@Deprecated public ByteBuffer buffer()
PowerBuffer
buffer
in class PowerBuffer<ImmutableBuffer>
@Deprecated public ImmutableBuffer clear()
clear
in class PowerBuffer<ImmutableBuffer>
Buffer.clear()
@Deprecated public ImmutableBuffer compact()
compact
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.compact()
public ImmutableBuffer duplicate()
duplicate
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.duplicate()
@Deprecated public ImmutableBuffer flip()
flip
in class PowerBuffer<ImmutableBuffer>
Buffer.flip()
@Deprecated public byte get()
get
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.get()
@Deprecated public ImmutableBuffer get(byte[] dst)
get
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.get(byte[])
@Deprecated public ImmutableBuffer get(byte[] dst, int offset, int length)
get
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.get(byte[], int, int)
@Deprecated public char getChar()
getChar
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.getChar()
@Deprecated public double getDouble()
getDouble
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.getDouble()
@Deprecated public float getFloat()
getFloat
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.getFloat()
@Deprecated public int getInt()
getInt
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.getInt()
@Deprecated public long getLong()
getLong
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.getLong()
@Deprecated public short getShort()
getShort
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.getShort()
@Deprecated public int getUByte()
PowerBuffer
getUByte
in class PowerBuffer<ImmutableBuffer>
@Deprecated public long getUInt()
PowerBuffer
getUInt
in class PowerBuffer<ImmutableBuffer>
@Deprecated public int getUShort()
PowerBuffer
getUShort
in class PowerBuffer<ImmutableBuffer>
public boolean isMutable()
PowerBuffer
true
if and only if this power buffer is mutable.isMutable
in class PowerBuffer<ImmutableBuffer>
@Deprecated public ImmutableBuffer limit(int newLimit)
limit
in class PowerBuffer<ImmutableBuffer>
Buffer.limit(int)
@Deprecated public ImmutableBuffer littleEndian()
PowerBuffer
littleEndian
in class PowerBuffer<ImmutableBuffer>
this
.@Deprecated public ImmutableBuffer load(ReadableByteChannel channel) throws IOException
PowerBuffer
IOException
occurs or the end-of-file is reached before
this buffer has been entirely filled, then it does not get reset and the
IOException
or an EOFException
gets thrown respectively.load
in class PowerBuffer<ImmutableBuffer>
channel
- the channel.this
.EOFException
- on unexpected end-of-file.IOException
- on any I/O error.@Deprecated public ImmutableBuffer mark()
mark
in class PowerBuffer<ImmutableBuffer>
Buffer.mark()
@Deprecated public ImmutableBuffer order(ByteOrder order)
order
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.order(ByteOrder)
@Deprecated public ImmutableBuffer position(int newPosition)
position
in class PowerBuffer<ImmutableBuffer>
Buffer.position(int)
@Deprecated public ImmutableBuffer put(byte b)
put
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.put(byte)
@Deprecated public ImmutableBuffer put(byte[] src)
put
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.put(byte[])
@Deprecated public ImmutableBuffer put(byte[] src, int offset, int length)
put
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.put(byte[], int, int)
@Deprecated public ImmutableBuffer put(ByteBuffer src)
put
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.put(ByteBuffer)
@Deprecated public ImmutableBuffer putChar(char value)
putChar
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.putChar(char)
@Deprecated public ImmutableBuffer putDouble(double value)
putDouble
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.putDouble(double)
@Deprecated public ImmutableBuffer putFloat(float value)
putFloat
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.putFloat(float)
@Deprecated public ImmutableBuffer putInt(int value)
putInt
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.putInt(int)
@Deprecated public ImmutableBuffer putLong(long value)
putLong
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.putLong(long)
@Deprecated public ImmutableBuffer putShort(short value)
putShort
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.putShort(short)
@Deprecated public ImmutableBuffer reset()
reset
in class PowerBuffer<ImmutableBuffer>
Buffer.reset()
@Deprecated public ImmutableBuffer rewind()
rewind
in class PowerBuffer<ImmutableBuffer>
Buffer.rewind()
@Deprecated public ImmutableBuffer save(WritableByteChannel channel) throws IOException
PowerBuffer
IOException
occurs, then this buffer does not get reset
and the IOException
gets thrown.save
in class PowerBuffer<ImmutableBuffer>
channel
- the channel.this
.IOException
- on any I/O error.@Deprecated public ImmutableBuffer skip(int skip)
PowerBuffer
skip
in class PowerBuffer<ImmutableBuffer>
skip
- the number of bytes to move forwards.
May be negative to move backwards, too.this
.public ImmutableBuffer slice()
slice
in class PowerBuffer<ImmutableBuffer>
ByteBuffer.slice()
public static ImmutableBuffer wrap(byte[] array)
ByteBuffer.wrap(byte[])
public static ImmutableBuffer wrap(byte[] array, int offset, int length)
ByteBuffer.wrap(byte[], int, int)
public static ImmutableBuffer wrap(ByteBuffer buf)
buffer
.buf
- the byte buffer to adapt.Copyright © 2012–2014 Schlichtherle IT Services. All rights reserved.