Java ByteBuffer Write writeStringData(ByteBuffer buff, String s, int len)

Here you can find the source of writeStringData(ByteBuffer buff, String s, int len)

Description

Write characters from a string (without the length).

License

Mozilla Public License

Parameter

Parameter Description
buff the target buffer (must be large enough)
s the string
len the number of characters

Declaration

public static void writeStringData(ByteBuffer buff, String s, int len) 

Method Source Code

//package com.java2s;
/*/*from   w w  w  .j  ava  2  s  .  c  om*/
 * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
 * and the EPL 1.0 (http://h2database.com/html/license.html).
 * Initial Developer: H2 Group
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Write characters from a string (without the length).
     *
     * @param buff the target buffer (must be large enough)
     * @param s the string
     * @param len the number of characters
     */
    public static void writeStringData(ByteBuffer buff, String s, int len) {
        for (int i = 0; i < len; i++) {
            int c = s.charAt(i);
            if (c < 0x80) {
                buff.put((byte) c);
            } else if (c >= 0x800) {
                buff.put((byte) (0xe0 | (c >> 12)));
                buff.put((byte) (((c >> 6) & 0x3f)));
                buff.put((byte) (c & 0x3f));
            } else {
                buff.put((byte) (0xc0 | (c >> 6)));
                buff.put((byte) (c & 0x3f));
            }
        }
    }
}

Related

  1. writeString(ByteBuffer byteBuffer, String str)
  2. writeString(final String text, final ByteBuffer out)
  3. writeString(String s, ByteBuffer buff)
  4. writeString(String string, ByteBuffer bb)
  5. writeStringArray(ByteBuffer buf, String[] array)
  6. writeStringToBuffer(ByteBuffer buffer, String message)
  7. writeTo(ByteBuffer buffer, File file)
  8. writeToChannel(final WritableByteChannel destChannel, final ByteBuffer buffer)
  9. writeToChannel(WritableByteChannel chan, ByteBuffer buffer)