Java ByteBuffer Write writeString(String s, ByteBuffer buff)

Here you can find the source of writeString(String s, ByteBuffer buff)

Description

write String

License

LGPL

Declaration

public static void writeString(String s, ByteBuffer buff) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void writeString(String s, ByteBuffer buff) {
        byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
        writeVarInt(bytes.length, buff);
        buff.put(bytes);//from   w  ww. jav a  2 s  .com
    }

    /**
     * Writes a signed integer with the "VarInt" format.
     *
     * @param n the int to encode
     * @param buff where to write the bytes
     */
    public static void writeVarInt(int n, ByteBuffer buff) {
        while ((n & 0xFFFF_FF80) != 0) {// While we have more than 7 bits (0b0xxxxxxx)
            byte data = (byte) (n | 0x80);// Discard bit sign and set msb to 1 (VarInt byte prefix).
            buff.put(data);
            n >>>= 7;
        }
        buff.put((byte) n);
    }
}

Related

  1. writeString(ByteBuffer buf, String str)
  2. writeString(ByteBuffer buf, String value)
  3. writeString(ByteBuffer buffer, String string)
  4. writeString(ByteBuffer byteBuffer, String str)
  5. writeString(final String text, final ByteBuffer out)
  6. writeString(String string, ByteBuffer bb)
  7. writeStringArray(ByteBuffer buf, String[] array)
  8. writeStringData(ByteBuffer buff, String s, int len)
  9. writeStringToBuffer(ByteBuffer buffer, String message)