Java ByteBuffer Write writeString(ByteBuffer buffer, String string)

Here you can find the source of writeString(ByteBuffer buffer, String string)

Description

write String

License

Open Source License

Declaration

public static void writeString(ByteBuffer buffer, String string) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void writeString(ByteBuffer buffer, String string) throws IOException {
        try {//  ww  w .j a  v a 2  s.  c  o m
            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);
        }

    }

    public static void writeVarInt(ByteBuffer buffer, int input) {
        while ((input & -128) != 0) {
            buffer.put((byte) (input & 127 | 128));
            input >>>= 7;
        }

        buffer.put((byte) input);
    }
}

Related

  1. writeSize(final int s, ByteBuffer buffer)
  2. writeSlice(ByteBuffer src, int number, ByteBuffer dst)
  3. writeString(ByteBuffer buf, String s)
  4. writeString(ByteBuffer buf, String str)
  5. writeString(ByteBuffer buf, String value)
  6. writeString(ByteBuffer byteBuffer, String str)
  7. writeString(final String text, final ByteBuffer out)
  8. writeString(String s, ByteBuffer buff)
  9. writeString(String string, ByteBuffer bb)