Java ByteBuffer to String string(ByteBuffer buffer)

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

Description

Decode a String representation.

License

Apache License

Parameter

Parameter Description
buffer a byte buffer holding the string representation

Return

the decoded string

Declaration

public static String string(ByteBuffer buffer) throws CharacterCodingException 

Method Source Code

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

import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;

public class Main {
    public static final Charset UTF_8 = Charset.forName("UTF-8");

    /**//from   www. j  a v  a 2  s.  com
    * Decode a String representation.
    * This method assumes that the encoding charset is UTF_8.
    *
    * @param buffer a byte buffer holding the string representation
    * @return the decoded string
    */
    public static String string(ByteBuffer buffer) throws CharacterCodingException {
        return string(buffer, UTF_8);
    }

    /**
    * Decode a String representation.
    * This method assumes that the encoding charset is UTF_8.
    *
    * @param buffer a byte buffer holding the string representation
    * @param position the starting position in {@code buffer} to start decoding from
    * @param length the number of bytes from {@code buffer} to use
    * @return the decoded string
    */
    public static String string(ByteBuffer buffer, int position, int length) throws CharacterCodingException {
        return string(buffer, position, length, UTF_8);
    }

    /**
    * Decode a String representation.
    *
    * @param buffer a byte buffer holding the string representation
    * @param position the starting position in {@code buffer} to start decoding from
    * @param length the number of bytes from {@code buffer} to use
    * @param charset the String encoding charset
    * @return the decoded string
    */
    public static String string(ByteBuffer buffer, int position, int length, Charset charset)
            throws CharacterCodingException {
        ByteBuffer copy = buffer.duplicate();
        copy.position(position);
        copy.limit(copy.position() + length);
        return string(copy, charset);
    }

    /**
    * Decode a String representation.
    *
    * @param buffer a byte buffer holding the string representation
    * @param charset the String encoding charset
    * @return the decoded string
    */
    public static String string(ByteBuffer buffer, Charset charset) throws CharacterCodingException {
        return charset.newDecoder().decode(buffer.duplicate()).toString();
    }
}

Related

  1. readString(ByteBuffer byteBuffer)
  2. readString(ByteBuffer logBuf)
  3. readString(final ByteBuffer buffer, final String encoding)
  4. string(ByteBuffer b, Charset charset)
  5. string(ByteBuffer buffer)
  6. string(ByteBuffer buffer, int position, int length, Charset charset)
  7. string(ByteBuffer bytes)
  8. toString(@Nonnull ByteBuffer buf, int len)
  9. toString(ByteBuffer b, String separator)