Java ByteBuffer Write writeString(ByteBuffer buf, String value)

Here you can find the source of writeString(ByteBuffer buf, String value)

Description

write String

License

LGPL

Declaration

public static void writeString(ByteBuffer buf, String value) 

Method Source Code


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

import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class Main {
    public static void writeString(ByteBuffer buf, String value) {
        if (value.contains("\0")) {
            throw new IllegalArgumentException("Null characters are not allowed in null-terminated strings.");
        }/*from  w w  w. j ava2  s. c  o  m*/
        int len = value.length();
        for (int i = 0; i < len; i++) {
            buf.put((byte) value.charAt(i));
        }
        buf.put((byte) 0);
    }

    public static void writeString(DataOutputStream buf, String value) throws IOException {
        if (value.contains("\0")) {
            throw new IllegalArgumentException("Null characters are not allowed in null-terminated strings.");
        }
        buf.writeBytes(value);
        buf.writeByte((byte) 0);
    }
}

Related

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