Java Utililty Methods ByteBuffer Write

List of utility methods to do ByteBuffer Write

Description

The list of methods to do ByteBuffer Write are organized into topic(s).

Method

voidwriteSize(final int s, ByteBuffer buffer)
write Size
if (s == -1) 
    buffer.put((byte) -1);
else if (s < 254)
    buffer.put((byte) s);
else
    buffer.put((byte) -2).putInt(s); 
voidwriteSlice(ByteBuffer src, int number, ByteBuffer dst)
write Slice
ByteBuffer slice = src.slice();
slice.limit(number);
dst.put(slice);
voidwriteString(ByteBuffer buf, String s)
write String
for (int i = 0; i < s.length(); i++)
    buf.put((byte) s.charAt(i));
voidwriteString(ByteBuffer buf, String str)
write String
int len = str.length();
buf.putShort((short) len);
for (int i = 0; i < len; i++) {
    buf.putChar(str.charAt(i));
voidwriteString(ByteBuffer buf, String value)
write String
if (value.contains("\0")) {
    throw new IllegalArgumentException("Null characters are not allowed in null-terminated strings.");
int len = value.length();
for (int i = 0; i < len; i++) {
    buf.put((byte) value.charAt(i));
buf.put((byte) 0);
...
voidwriteString(ByteBuffer buffer, String string)
write String
try {
    byte[] abyte = string.getBytes(StandardCharsets.UTF_8);
    if (abyte.length > 32767) {
        throw new IOException("String too big (was " + abyte.length + " bytes encoded, max " + 32767 + ")");
    } else {
        writeVarInt(buffer, abyte.length);
        buffer.put(abyte, 0, abyte.length);
} catch (UnsupportedEncodingException e) {
    throw new IOException(e);
voidwriteString(ByteBuffer byteBuffer, String str)
write String
if (str == null) {
    byteBuffer.putInt(0);
} else {
    byte[] b = str.getBytes();
    byteBuffer.putInt(b.length + 1);
    byteBuffer.put(b);
    byteBuffer.put((byte) 0);
voidwriteString(final String text, final ByteBuffer out)
write String
Objects.requireNonNull(text, "text can not be null!");
Objects.requireNonNull(out, "out can not be null!");
out.putShort((short) text.length());
out.put(text.getBytes(StandardCharsets.UTF_8));
voidwriteString(String s, ByteBuffer buff)
write String
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
writeVarInt(bytes.length, buff);
buff.put(bytes);
voidwriteString(String string, ByteBuffer bb)
write String
bb.putShort((short) string.length());
bb.put(string.getBytes());