Java ByteBuffer to String readString(final ByteBuffer buffer, final String encoding)

Here you can find the source of readString(final ByteBuffer buffer, final String encoding)

Description

Translate the given buffer into a string

License

Apache License

Parameter

Parameter Description
buffer The buffer to translate
encoding The encoding to use in translating bytes to characters

Declaration

public static String readString(final ByteBuffer buffer, final String encoding)
        throws UnsupportedEncodingException 

Method Source Code

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

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

import java.nio.charset.Charset;

public class Main {
    /**/*from w  ww  .  j  a  va  2 s  .  com*/
     * Translate the given buffer into a string
     *
     * @param buffer   The buffer to translate
     * @param encoding The encoding to use in translating bytes to characters
     */
    public static String readString(final ByteBuffer buffer, final String encoding)
            throws UnsupportedEncodingException {
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        return new String(bytes, encoding);
    }

    public static String readString(final ByteBuffer buffer) throws UnsupportedEncodingException {
        return readString(buffer, Charset.defaultCharset().toString());
    }
}

Related

  1. readString(ByteBuffer buffer)
  2. readString(ByteBuffer buffer)
  3. readString(ByteBuffer byteBuffer)
  4. readString(ByteBuffer byteBuffer)
  5. readString(ByteBuffer logBuf)
  6. string(ByteBuffer b, Charset charset)
  7. string(ByteBuffer buffer)
  8. string(ByteBuffer buffer)
  9. string(ByteBuffer buffer, int position, int length, Charset charset)