Java ByteBuffer to UTF toUTF8String(ByteBuffer buffer)

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

Description

Convert the buffer to an UTF-8 String

License

Open Source License

Parameter

Parameter Description
buffer The buffer to convert in flush mode. The buffer is unchanged

Return

The buffer as a string.

Declaration

public static String toUTF8String(ByteBuffer buffer) 

Method Source Code

//package com.java2s;
//  are made available under the terms of the Eclipse Public License v1.0

import java.nio.ByteBuffer;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class Main {
    /** Convert the buffer to an UTF-8 String
     * @param buffer The buffer to convert in flush mode. The buffer is unchanged
     * @return The buffer as a string.// ww w  .j av  a  2 s  .  co m
     */
    public static String toUTF8String(ByteBuffer buffer) {
        return toString(buffer, StandardCharsets.UTF_8);
    }

    /** Convert the buffer to an ISO-8859-1 String
     * @param buffer The buffer to convert in flush mode. The buffer is unchanged
     * @return The buffer as a string.
     */
    public static String toString(ByteBuffer buffer) {
        return toString(buffer, StandardCharsets.ISO_8859_1);
    }

    /** Convert the buffer to an ISO-8859-1 String
     * @param buffer  The buffer to convert in flush mode. The buffer is unchanged
     * @param charset The {@link Charset} to use to convert the bytes
     * @return The buffer as a string.
     */
    public static String toString(ByteBuffer buffer, Charset charset) {
        if (buffer == null)
            return null;
        byte[] array = buffer.hasArray() ? buffer.array() : null;
        if (array == null) {
            byte[] to = new byte[buffer.remaining()];
            buffer.slice().get(to);
            return new String(to, 0, to.length, charset);
        }
        return new String(array, buffer.arrayOffset() + buffer.position(), buffer.remaining(), charset);
    }

    /** Convert a partial buffer to a String.
     * 
     * @param buffer the buffer to convert 
     * @param position The position in the buffer to start the string from
     * @param length The length of the buffer
     * @param charset The {@link Charset} to use to convert the bytes
     * @return  The buffer as a string.
     */
    public static String toString(ByteBuffer buffer, int position, int length, Charset charset) {
        if (buffer == null)
            return null;
        byte[] array = buffer.hasArray() ? buffer.array() : null;
        if (array == null) {
            ByteBuffer ro = buffer.asReadOnlyBuffer();
            ro.position(position);
            ro.limit(position + length);
            byte[] to = new byte[length];
            ro.get(to);
            return new String(to, 0, to.length, charset);
        }
        return new String(array, buffer.arrayOffset() + position, length, charset);
    }
}

Related

  1. readUTF(ByteBuffer bb)
  2. readUTF8(ByteBuffer buf)
  3. readUTFString(MappedByteBuffer mmap)
  4. toUtf8CharBuffer(ByteBuffer buffer)
  5. toUtf8String(ByteBuffer buffer)
  6. toUtf8String(ByteBuffer byteBuffer)