Class ByteBufNIO
- java.lang.Object
-
- org.bson.ByteBufNIO
-
-
Constructor Summary
Constructors Constructor Description ByteBufNIO(ByteBuffer buf)Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]array()Returns the byte array that backs this buffer (optional operation).ByteBufferasNIO()Gets the underlying NIOByteBuffer.ByteBufasReadOnly()Creates a new, read-only byte buffer that shares this buffer's content.intcapacity()Returns this buffer's capacity.ByteBufclear()Clears this buffer.ByteBufduplicate()Creates a new byte buffer that shares this buffer's content.ByteBufflip()Flips this buffer.byteget()Relative get method.ByteBufget(byte[] bytes)Relative bulkgetmethod.ByteBufget(byte[] bytes, int offset, int length)Relative bulk get method.byteget(int index)Absolute get method.ByteBufget(int index, byte[] bytes)Absolute bulkgetmethod.ByteBufget(int index, byte[] bytes, int offset, int length)Absolute bulk get method.doublegetDouble()Relative get method for reading a double value.doublegetDouble(int index)Absolute get method for reading a double value.intgetInt()Relative get method for reading an int value.intgetInt(int index)Absolute get method for reading an int value.longgetLong()Relative get method for reading a long value.longgetLong(int index)Absolute get method for reading a long value.intgetReferenceCount()Gets the current reference count, which starts at 0.booleanhasRemaining()States whether there are any elements between the current position and the limit.intlimit()Returns this buffer's limit.ByteBuflimit(int newLimit)Sets this buffer's limit.ByteBuforder(ByteOrder byteOrder)Modifies this buffer's byte order.intposition()Returns this buffer's position.ByteBufposition(int newPosition)Sets this buffer's position.ByteBufput(byte b)Relative put method (optional operation).ByteBufput(byte[] src, int offset, int length)Relative bulk put method (optional operation).ByteBufput(int index, byte b)Absolute put method (optional operation).voidrelease()Release a reference to this object.intremaining()Returns the number of elements between the current position and the limit.ByteBufNIOretain()Retain an additional reference to this object.
-
-
-
Constructor Detail
-
ByteBufNIO
public ByteBufNIO(ByteBuffer buf)
Creates a new instance.- Parameters:
buf- theByteBufferto wrap.
-
-
Method Detail
-
getReferenceCount
public int getReferenceCount()
Description copied from interface:ByteBufGets the current reference count, which starts at 0.- Specified by:
getReferenceCountin interfaceByteBuf- Returns:
- the current count, which must be greater than or equal to 0
-
retain
public ByteBufNIO retain()
Description copied from interface:ByteBufRetain an additional reference to this object. All retained references must be released, or there will be a leak.
-
release
public void release()
Description copied from interface:ByteBufRelease a reference to this object.
-
capacity
public int capacity()
Description copied from interface:ByteBufReturns this buffer's capacity.
-
put
public ByteBuf put(int index, byte b)
Description copied from interface:ByteBufAbsolute put method (optional operation).
Writes the given byte into this buffer at the given index.
-
remaining
public int remaining()
Description copied from interface:ByteBufReturns the number of elements between the current position and the limit.
-
put
public ByteBuf put(byte[] src, int offset, int length)
Description copied from interface:ByteBufRelative bulk put method (optional operation).
This method transfers bytes into this buffer from the given source array. If there are more bytes to be copied from the array than remain in this buffer, that is, if
length > remaining(), then no bytes are transferred and aBufferOverflowExceptionis thrown.Otherwise, this method copies
lengthbytes from the given array into this buffer, starting at the given offset in the array and at the current position of this buffer. The position of this buffer is then incremented bylength.In other words, an invocation of this method of the form
dst.put(src, off, len)has exactly the same effect as the loopfor (int i = off; i < off + len; i++) { dst.put(a[i]); }except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.
- Specified by:
putin interfaceByteBuf- Parameters:
src- The array from which bytes are to be readoffset- The offset within the array of the first byte to be read; must be non-negative and no larger thanarray.lengthlength- The number of bytes to be read from the given array; must be non-negative and no larger thanarray.length - offset- Returns:
- This buffer
-
hasRemaining
public boolean hasRemaining()
Description copied from interface:ByteBufStates whether there are any elements between the current position and the limit.- Specified by:
hasRemainingin interfaceByteBuf- Returns:
trueif, and only if, there is at least one element remaining in this buffer
-
put
public ByteBuf put(byte b)
Description copied from interface:ByteBufRelative put method (optional operation).
Writes the given byte into this buffer at the current position, and then increments the position.
-
flip
public ByteBuf flip()
Description copied from interface:ByteBufFlips this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded.
After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of channel-write or relative get operations. For example:
buf.put(magic); // Prepend header in.read(buf); // Read data into rest of buffer buf.flip(); // Flip buffer out.write(buf); // Write header + data to channel
This method is often used in conjunction with the
compactmethod when transferring data from one place to another.
-
array
public byte[] array()
Description copied from interface:ByteBufReturns the byte array that backs this buffer (optional operation).
Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa.
-
limit
public int limit()
Description copied from interface:ByteBufReturns this buffer's limit.
-
position
public ByteBuf position(int newPosition)
Description copied from interface:ByteBufSets this buffer's position. If the mark is defined and larger than the new position then it is discarded.
-
clear
public ByteBuf clear()
Description copied from interface:ByteBufClears this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded.
Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example:
buf.clear(); // Prepare buffer for reading in.read(buf); // Read data
This method does not actually erase the data in the buffer, but it is named as if it did because it will most often be used in situations in which that might as well be the case.
-
order
public ByteBuf order(ByteOrder byteOrder)
Description copied from interface:ByteBufModifies this buffer's byte order.- Specified by:
orderin interfaceByteBuf- Parameters:
byteOrder- The new byte order, eitherBIG_ENDIANorLITTLE_ENDIAN- Returns:
- This buffer
-
get
public byte get()
Description copied from interface:ByteBufRelative get method. Reads the byte at this buffer's current position, and then increments the position.
-
get
public byte get(int index)
Description copied from interface:ByteBufAbsolute get method. Reads the byte at the given index.
-
get
public ByteBuf get(byte[] bytes)
Description copied from interface:ByteBufRelative bulk
getmethod.This method transfers bytes from this buffer into the given destination array. An invocation of this method of the form
src.get(a)behaves in exactly the same way as the invocation:src.get(a, 0, a.length)
-
get
public ByteBuf get(int index, byte[] bytes)
Description copied from interface:ByteBufAbsolute bulk
getmethod.This method transfers bytes from this buffer into the given destination array. An invocation of this method of the form
src.get(a)behaves in exactly the same way as the invocation:src.get(index, a, 0, a.length)
-
get
public ByteBuf get(byte[] bytes, int offset, int length)
Description copied from interface:ByteBufRelative bulk get method.This method transfers bytes from this buffer into the given destination array. If there are fewer bytes remaining in the buffer than are required to satisfy the request, that is, if
length > remaining(), then no bytes are transferred and aBufferUnderflowExceptionis thrown.Otherwise, this method copies
lengthbytes from this buffer into the given array, starting at the current position of this buffer and at the given offset in the array. The position of this buffer is then incremented bylength.In other words, an invocation of this method of the form
src.get(dst, off, len)has exactly the same effect as the loop
except that it first checks that there are sufficient bytes in this buffer and it is potentially much more efficient.for (int i = off; i < off + len; i++) dst[i] = src.get();- Specified by:
getin interfaceByteBuf- Parameters:
bytes- The array into which bytes are to be writtenoffset- The offset within the array of the first byte to be written; must be non-negative and no larger thandst.lengthlength- The maximum number of bytes to be written to the given array; must be non-negative and no larger thandst.length - offset- Returns:
- This buffer
-
get
public ByteBuf get(int index, byte[] bytes, int offset, int length)
Description copied from interface:ByteBufAbsolute bulk get method.This method transfers bytes from this buffer into the given destination array. If there are fewer bytes remaining in the buffer than are required to satisfy the request, that is, if
length > remaining, then no bytes are transferred and aBufferUnderflowExceptionis thrown.Otherwise, this method copies
lengthbytes from this buffer into the given array, starting at the given index buffer and at the given offset in the array.In other words, an invocation of this method of the form
src.get(dst, off, len)has exactly the same effect as the loop
except that it first checks that there are sufficient bytes in this buffer and it is potentially much more efficient.for (int i = off; i < off + len; i++) dst[i] = src.get(i);- Specified by:
getin interfaceByteBuf- Parameters:
index- The index from which the bytes will be readbytes- The array into which bytes are to be writtenoffset- The offset within the array of the first byte to be written; must be non-negative and no larger thandst.lengthlength- The maximum number of bytes to be written to the given array; must be non-negative and no larger thandst.length - offset- Returns:
- This buffer
-
getLong
public long getLong()
Description copied from interface:ByteBufRelative get method for reading a long value.
Reads the next eight bytes at this buffer's current position, composing them into a long value according to the current byte order, and then increments the position by eight.
-
getLong
public long getLong(int index)
Description copied from interface:ByteBufAbsolute get method for reading a long value.Reads eight bytes at the given index, composing them into a long value according to the current byte order.
-
getDouble
public double getDouble()
Description copied from interface:ByteBufRelative get method for reading a double value.
Reads the next eight bytes at this buffer's current position, composing them into a double value according to the current byte order, and then increments the position by eight.
-
getDouble
public double getDouble(int index)
Description copied from interface:ByteBufAbsolute get method for reading a double value.Reads eight bytes at the given index, composing them into a double value according to the current byte order.
-
getInt
public int getInt()
Description copied from interface:ByteBufRelative get method for reading an int value.
Reads the next four bytes at this buffer's current position, composing them into an int value according to the current byte order, and then increments the position by four.
-
getInt
public int getInt(int index)
Description copied from interface:ByteBufAbsolute get method for reading an int value.Reads four bytes at the given index, composing them into a int value according to the current byte order.
-
position
public int position()
Description copied from interface:ByteBufReturns this buffer's position.
-
limit
public ByteBuf limit(int newLimit)
Description copied from interface:ByteBufSets this buffer's limit. If the position is larger than the new limit then it is set to the new limit. If the mark is defined and larger than the new limit then it is discarded.
-
asReadOnly
public ByteBuf asReadOnly()
Description copied from interface:ByteBufCreates a new, read-only byte buffer that shares this buffer's content.
The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer; the new buffer itself, however, will be read-only and will not allow the shared content to be modified. The two buffers' position, limit, and mark values will be independent.
The new buffer's capacity, limit, position, and mark values will be identical to those of this buffer.
- Specified by:
asReadOnlyin interfaceByteBuf- Returns:
- The new, read-only byte buffer
-
duplicate
public ByteBuf duplicate()
Description copied from interface:ByteBufCreates a new byte buffer that shares this buffer's content.
The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.
The new buffer's capacity, limit, position, and mark values will be identical to those of this buffer. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.
-
asNIO
public ByteBuffer asNIO()
Description copied from interface:ByteBufGets the underlying NIOByteBuffer. Changes made directly to the returned buffer will be reflected in this instance, and vice versa, so be careful. This method should really only be used so that the underlying buffer can be passed directly to a socket channel.
-
-