Java ByteBuffer Decode decode(byte firstByte, ByteBuffer buffer, String charset)

Here you can find the source of decode(byte firstByte, ByteBuffer buffer, String charset)

Description

decode

License

Open Source License

Declaration

public static final String decode(byte firstByte, ByteBuffer buffer,
            String charset) 

Method Source Code

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

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class Main {
    private static final long OB_MAX_1B_STR_LEN = 55;
    private static final byte OB_VARCHAR_LEN_MASK = 0x3f;

    public static final String decode(byte firstByte, ByteBuffer buffer,
            String charset) {/*from  w w  w. ja  va 2 s  .c o m*/
        int lenOrValue = firstByte & OB_VARCHAR_LEN_MASK;
        int strLen = 0;
        if (lenOrValue <= OB_MAX_1B_STR_LEN) {
            strLen = lenOrValue;
        } else {
            for (int n = 0; n < lenOrValue - OB_MAX_1B_STR_LEN; n++) {
                strLen |= ((buffer.get() & 0xffl) << (n << 3));
            }
        }
        byte[] ret = new byte[strLen];
        buffer.get(ret);
        try {
            return new String(ret, charset);
        } catch (UnsupportedEncodingException e) {
            return "";// never happen
        }
    }
}

Related

  1. bytesToChars(CharsetDecoder decoder, ByteBuffer bytes, CharBuffer chars)
  2. decode(ByteBuffer bb)
  3. decode(ByteBuffer bb, CharsetDecoder decoder)
  4. decode(Charset charset, ByteBuffer buffer)
  5. decode(CharsetDecoder decoder, ByteBuffer src, CharBuffer dst)