Java ByteBuffer to String readString(ByteBuffer buff, int len)

Here you can find the source of readString(ByteBuffer buff, int len)

Description

Read a string.

License

Mozilla Public License

Parameter

Parameter Description
buff the source buffer
len the number of characters

Return

the value

Declaration

public static String readString(ByteBuffer buff, int len) 

Method Source Code

//package com.java2s;
/*//from  ww w  . jav  a2 s  .c o m
 * 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 {
    /**
     * Read a string.
     *
     * @param buff the source buffer
     * @param len the number of characters
     * @return the value
     */
    public static String readString(ByteBuffer buff, int len) {
        char[] chars = new char[len];
        for (int i = 0; i < len; i++) {
            int x = buff.get() & 0xff;
            if (x < 0x80) {
                chars[i] = (char) x;
            } else if (x >= 0xe0) {
                chars[i] = (char) (((x & 0xf) << 12) + ((buff.get() & 0x3f) << 6) + (buff.get() & 0x3f));
            } else {
                chars[i] = (char) (((x & 0x1f) << 6) + (buff.get() & 0x3f));
            }
        }
        return new String(chars);
    }
}

Related

  1. readString(ByteBuffer bb)
  2. readString(ByteBuffer bb, int limit)
  3. readString(ByteBuffer buf)
  4. readString(ByteBuffer buf, int length)
  5. readString(ByteBuffer buff)
  6. readString(ByteBuffer buffer)
  7. readString(ByteBuffer buffer)
  8. readString(ByteBuffer byteBuffer)
  9. readString(ByteBuffer byteBuffer)