Android ByteBuffer to String Convert fromBytes(ByteBuffer buf)

Here you can find the source of fromBytes(ByteBuffer buf)

Description

Return a new String with chars corresponding to buf.

License

Open Source License

Parameter

Parameter Description
buf a ByteBuffer of bytes

Return

a new String corresponding to the bytes in buf

Declaration

public static String fromBytes(ByteBuffer buf) 

Method Source Code

//package com.java2s;

import java.nio.ByteBuffer;

public class Main {
    /**/*from w w  w. j a v a 2  s.c o m*/
     * Return a new String with chars corresponding to buf from off to
     * off + len.
     *
     * @param buf an array of bytes
     * @param off the initial offset
     * @param len the length
     * @return a new String corresponding to the bytes in buf
     */
    @SuppressWarnings("deprecation")
    public static String fromBytes(byte[] buf, int off, int len) {
        // Yes, I known the method is deprecated, but it is the fastest
        // way of converting between between byte[] and String
        return new String(buf, 0, off, len);
    }

    /**
     * Return a new String with chars corresponding to buf.
     *
     * @param buf an array of bytes
     * @return a new String corresponding to the bytes in buf
     */
    public static String fromBytes(byte[] buf) {
        return fromBytes(buf, 0, buf.length);
    }

    /**
     * Return a new String with chars corresponding to buf.
     *
     * @param buf a ByteBuffer of bytes
     * @return a new String corresponding to the bytes in buf
     */
    public static String fromBytes(ByteBuffer buf) {
        return fromBytes(buf.array(), buf.arrayOffset() + buf.position(),
                buf.arrayOffset() + buf.limit());
    }
}