Package io.micronaut.core.io.buffer
Interface ByteBuffer<T>
-
- Type Parameters:
T- buffer type
public interface ByteBuffer<T>Interface to allow interfacing with different byte buffer implementations, primarily as an abstraction over Netty.- Since:
- 1.0
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description TasNativeBuffer()java.nio.ByteBufferasNioBuffer()Exposes this buffer's readable bytes as an NIOByteBuffer.java.nio.ByteBufferasNioBuffer(int index, int length)Exposes this buffer's sub-region as an NIOByteBuffer.ByteBuffercapacity(int capacity)Adjusts the capacity of this buffer.bytegetByte(int index)Get the byte at the specified index.intindexOf(byte b)Find the index of the first occurrence of the given byte.intmaxCapacity()Returns the maximum allowed capacity of this buffer.byteread()Gets a byte at the currentreaderIndexand increases thereaderIndexby1in this buffer.ByteBufferread(byte[] destination)Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=dst.length).ByteBufferread(byte[] destination, int offset, int length)Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).intreadableBytes()Returns the number of readable bytes which is equal to(this.writerIndex - this.readerIndex).java.lang.CharSequencereadCharSequence(int length, java.nio.charset.Charset charset)Gets aCharSequencewith the given length at the currentreaderIndexand increases thereaderIndexby the given length.intreaderIndex()Returns thereaderIndexof this buffer.ByteBufferreaderIndex(int readPosition)Sets thereaderIndexof this buffer.ByteBufferslice(int index, int length)Create a newByteBufferwhose contents is a shared subsequence of this data buffer's content.byte[]toByteArray()Create a copy of the underlying storage frombufinto a byte array.java.io.InputStreamtoInputStream()Convert theByteBufferinto an input stream.java.io.OutputStreamtoOutputStream()Convert theByteBufferinto an output stream.java.lang.StringtoString(java.nio.charset.Charset charset)To string.intwritableBytes()Returns the number of writable bytes which is equal to(this.capacity - this.writerIndex).ByteBufferwrite(byte b)Sets the specified byte at the currentwriterIndexand increases thewriterIndexby1in this buffer.ByteBufferwrite(byte[] source)Transfers the specified source array's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=src.length).ByteBufferwrite(byte[] source, int offset, int length)Transfers the specified source array's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length).ByteBufferwrite(ByteBuffer... buffers)Write the givenByteBufferinstances to this buffer.ByteBufferwrite(java.lang.CharSequence source, java.nio.charset.Charset charset)Transfers the specified source CharSequence's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=src.length).ByteBufferwrite(java.nio.ByteBuffer... buffers)Write the givenByteBufferinstances to this buffer.intwriterIndex()Returns thewriterIndexof this buffer.ByteBufferwriterIndex(int position)Sets thewriterIndexof this buffer.
-
-
-
Method Detail
-
asNativeBuffer
T asNativeBuffer()
- Returns:
- The native buffer type
-
readableBytes
int readableBytes()
Returns the number of readable bytes which is equal to(this.writerIndex - this.readerIndex).- Returns:
- bytes
-
writableBytes
int writableBytes()
Returns the number of writable bytes which is equal to(this.capacity - this.writerIndex).- Returns:
- The bytes
-
maxCapacity
int maxCapacity()
Returns the maximum allowed capacity of this buffer. If a user attempts to increase the capacity of this buffer beyond the maximum capacity usingcapacity(int)orIllegalArgumentException.- Returns:
- The max capacity
-
capacity
ByteBuffer capacity(int capacity)
Adjusts the capacity of this buffer. If thenewCapacityis less than the current capacity, the content of this buffer is truncated. If thenewCapacityis greater than the current capacity, the buffer is appended with unspecified data whose length is(newCapacity - currentCapacity).- Parameters:
capacity- capacity- Returns:
- The bytebuffer
-
readerIndex
int readerIndex()
Returns thereaderIndexof this buffer.- Returns:
- The index
-
readerIndex
ByteBuffer readerIndex(int readPosition)
Sets thereaderIndexof this buffer.- Parameters:
readPosition- readPosition- Returns:
- The buffer
- Throws:
java.lang.IndexOutOfBoundsException- if the specifiedreaderIndexis less than0or greater thanthis.writerIndex
-
writerIndex
int writerIndex()
Returns thewriterIndexof this buffer.- Returns:
- The index
-
writerIndex
ByteBuffer writerIndex(int position)
Sets thewriterIndexof this buffer.- Parameters:
position- The position- Returns:
- The index as buffer
- Throws:
java.lang.IndexOutOfBoundsException- if the specifiedwriterIndexis less thanthis.readerIndexor greater thanthis.capacity
-
read
byte read()
Gets a byte at the currentreaderIndexand increases thereaderIndexby1in this buffer.- Returns:
- bytes
- Throws:
java.lang.IndexOutOfBoundsException- ifthis.readableBytesis less than1
-
readCharSequence
java.lang.CharSequence readCharSequence(int length, java.nio.charset.Charset charset)Gets aCharSequencewith the given length at the currentreaderIndexand increases thereaderIndexby the given length.- Parameters:
length- the length to readcharset- that should be used- Returns:
- the sequence
- Throws:
java.lang.IndexOutOfBoundsException- iflengthis greater thanthis.readableBytes
-
read
ByteBuffer read(byte[] destination)
Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=dst.length).- Parameters:
destination- destination- Returns:
- bytesBuffer
- Throws:
java.lang.IndexOutOfBoundsException- ifdst.lengthis greater thanthis.readableBytes
-
read
ByteBuffer read(byte[] destination, int offset, int length)
Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).- Parameters:
destination- The destination byte arrayoffset- the first index of the destinationlength- the number of bytes to transfer- Returns:
- bytesBuffer
- Throws:
java.lang.IndexOutOfBoundsException- if the specifieddstIndexis less than0, iflengthis greater thanthis.readableBytes, or ifdstIndex + lengthis greater thandst.length
-
write
ByteBuffer write(byte b)
Sets the specified byte at the currentwriterIndexand increases thewriterIndexby1in this buffer. The 24 high-order bits of the specified value are ignored.- Parameters:
b- The byte to write- Returns:
- bytesBuffer
- Throws:
java.lang.IndexOutOfBoundsException- ifthis.writableBytesis less than1
-
write
ByteBuffer write(byte[] source)
Transfers the specified source array's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=src.length).- Parameters:
source- The source bytes- Returns:
- bytesBuffer
- Throws:
java.lang.IndexOutOfBoundsException- ifsrc.lengthis greater thanthis.writableBytes
-
write
ByteBuffer write(java.lang.CharSequence source, java.nio.charset.Charset charset)
Transfers the specified source CharSequence's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=src.length).- Parameters:
source- The char sequencecharset- The charset- Returns:
- This buffer
-
write
ByteBuffer write(byte[] source, int offset, int length)
Transfers the specified source array's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length).- Parameters:
source- The source byte arrayoffset- the first index of the sourcelength- the number of bytes to transfer- Returns:
- bytesBuffer
- Throws:
java.lang.IndexOutOfBoundsException- if the specifiedsrcIndexis less than0, ifsrcIndex + lengthis greater thansrc.length, or iflengthis greater thanthis.writableBytes
-
write
ByteBuffer write(ByteBuffer... buffers)
Write the givenByteBufferinstances to this buffer.- Parameters:
buffers- The buffers to write- Returns:
- this buffer
-
write
ByteBuffer write(java.nio.ByteBuffer... buffers)
Write the givenByteBufferinstances to this buffer.- Parameters:
buffers- The buffers to write- Returns:
- this buffer
-
slice
ByteBuffer slice(int index, int length)
Create a newByteBufferwhose contents is a shared subsequence of this data buffer's content. Data between this byte buffer and the returned buffer is shared; though changes in the returned buffer's position will not be reflected in the reading nor writing position of this data buffer.- Parameters:
index- the index at which to start the slicelength- the length of the slice- Returns:
- the specified slice of this data buffer
-
asNioBuffer
java.nio.ByteBuffer asNioBuffer()
Exposes this buffer's readable bytes as an NIOByteBuffer. The returned buffer shares the content with this buffer, while changing the position and limit of the returned NIO buffer does not affect the indexes and marks of this buffer. This method is identical tobuf.nioBuffer(buf.readerIndex(), buf.readableBytes()). This method does not modifyreaderIndexorwriterIndexof this buffer. Please note that the returned NIO buffer will not see the changes of this buffer if this buffer is a dynamic buffer and it adjusted its capacity.- Returns:
- byteBuffer
- Throws:
java.lang.UnsupportedOperationException- if this buffer cannot create aByteBufferthat shares the content with itself
-
asNioBuffer
java.nio.ByteBuffer asNioBuffer(int index, int length)Exposes this buffer's sub-region as an NIOByteBuffer. The returned buffer shares the content with this buffer, while changing the position and limit of the returned NIO buffer does not affect the indexes and marks of this buffer. This method does not modifyreaderIndexorwriterIndexof this buffer. Please note that the returned NIO buffer will not see the changes of this buffer if this buffer is a dynamic buffer and it adjusted its capacity.- Parameters:
index- The indexlength- The length- Returns:
- byteBuffer
- Throws:
java.lang.UnsupportedOperationException- if this buffer cannot create aByteBufferthat shares the content with itself
-
toInputStream
java.io.InputStream toInputStream()
Convert theByteBufferinto an input stream.- Returns:
- this buffer as an input stream
-
toOutputStream
java.io.OutputStream toOutputStream()
Convert theByteBufferinto an output stream.- Returns:
- this buffer as an input stream
-
toByteArray
byte[] toByteArray()
Create a copy of the underlying storage frombufinto a byte array. The copy will start atreaderIndex()and copyreadableBytes()bytes.- Returns:
- byte array
-
toString
java.lang.String toString(java.nio.charset.Charset charset)
To string.- Parameters:
charset- converted charset- Returns:
- string
-
indexOf
int indexOf(byte b)
Find the index of the first occurrence of the given byte.- Parameters:
b- The byte to find- Returns:
- The index of the byte
-
getByte
byte getByte(int index)
Get the byte at the specified index.- Parameters:
index- The index- Returns:
- The byte
-
-