Package org.jenkinsci.remoting.util
Class ByteBufferUtils
- java.lang.Object
-
- org.jenkinsci.remoting.util.ByteBufferUtils
-
public final class ByteBufferUtils extends Object
Utility methods to help working withByteBuffer
s.- Since:
- 3.0
-
-
Field Summary
Fields Modifier and Type Field Description static ByteBuffer
EMPTY_BUFFER
A handy constant to use for no-op send/receive calls.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ByteBuffer
accumulate(ByteBuffer src, ByteBuffer dst)
Accumulate the given buffer into the current context.static ByteBuffer
duplicate(ByteBuffer buffer)
Duplicate a byte buffer for storing for future use.static String
getUTF8(ByteBuffer src)
Read from the source buffer a string that has been encoded in UTF-8 and prefixed by a two byte length.static void
put(ByteBuffer src, ByteBuffer dst)
Transfer bytes from src to dst.static void
putUTF8(String src, ByteBuffer dst)
Transfer a string into the destination buffer as a UTF-8 encoded string prefixed by a two byte length.static ByteBuffer
wrapUTF8(String string)
Wrap a string encoded as a UTF-8 string prefixed by a two byte length.
-
-
-
Field Detail
-
EMPTY_BUFFER
public static final ByteBuffer EMPTY_BUFFER
A handy constant to use for no-op send/receive calls.
-
-
Method Detail
-
put
public static void put(ByteBuffer src, ByteBuffer dst)
Transfer bytes from src to dst. If the source has more bytes than the destination then only as many bytes as the destination has capacity to take will be transferred.- Parameters:
src
- the source.dst
- the destination.
-
putUTF8
public static void putUTF8(String src, ByteBuffer dst)
Transfer a string into the destination buffer as a UTF-8 encoded string prefixed by a two byte length.- Parameters:
src
- the string to encode and copy.dst
- the destination.
-
getUTF8
public static String getUTF8(ByteBuffer src)
Read from the source buffer a string that has been encoded in UTF-8 and prefixed by a two byte length.- Parameters:
src
- the source buffer.- Returns:
- the string.
- Throws:
BufferUnderflowException
- if the source buffer has not got the full string.
-
wrapUTF8
public static ByteBuffer wrapUTF8(String string)
Wrap a string encoded as a UTF-8 string prefixed by a two byte length.- Parameters:
string
- the string.- Returns:
- a
ByteBuffer
containing the two byte length followed by the string encoded as UTF-8.
-
accumulate
public static ByteBuffer accumulate(ByteBuffer src, ByteBuffer dst)
Accumulate the given buffer into the current context. Allocation is performed only if needed.- Parameters:
src
- the buffer to accumulatedst
- the buffer to accumulate into- Returns:
- the accumulated buffer (may be
dst
or a new buffer ifdst
did not have sufficient capacity)
-
duplicate
public static ByteBuffer duplicate(ByteBuffer buffer)
Duplicate a byte buffer for storing for future use.- Parameters:
buffer
- the buffer to duplicate- Returns:
- the newly allocated buffer
-
-