Java ByteBuffer to String getString(final ByteBuffer buffer, final int offset, final int length, final Charset encoding)

Here you can find the source of getString(final ByteBuffer buffer, final int offset, final int length, final Charset encoding)

Description

Reads bytes from a ByteBuffer as if they were encoded in the specified CharSet.

License

Open Source License

Parameter

Parameter Description
buffer a parameter
offset offset from current position
length size of data to process
encoding a parameter

Declaration

public static String getString(final ByteBuffer buffer, final int offset, final int length,
        final Charset encoding) 

Method Source Code

//package com.java2s;
/*// w  w  w.j av a  2  s  .c om
 * Entagged Audio Tag library
 * Copyright (c) 2003-2005 Rapha?l Slinckx <raphael@slinckx.net>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.nio.ByteBuffer;

import java.nio.charset.Charset;

public class Main {
    /**
     * Reads bytes from a ByteBuffer as if they were encoded in the specified CharSet.
     *
     * @param buffer
     * @param offset offset from current position
     * @param length size of data to process
     * @param encoding
     * @return
     */
    public static String getString(final ByteBuffer buffer, final int offset, final int length,
            final Charset encoding) {
        final byte[] b = new byte[length];
        buffer.position(buffer.position() + offset);
        buffer.get(b);
        return new String(b, 0, length, encoding);
    }

    /**
     * Reads bytes from a ByteBuffer as if they were encoded in the specified CharSet.
     *
     * @param buffer
     * @param encoding
     * @return
     */
    public static String getString(final ByteBuffer buffer, final Charset encoding) {
        final byte[] b = new byte[buffer.remaining()];
        buffer.get(b);
        return new String(b, 0, b.length, encoding);
    }
}

Related

  1. getString(ByteBuffer buffer, int size)
  2. getString(ByteBuffer buffer, String charFormat)
  3. getString(ByteBuffer byteBuffer, int size)
  4. getString(ByteBuffer in, int maxLength)
  5. getString(final ByteBuffer buffer)
  6. getString(java.nio.ByteBuffer buffer, int offset, int len)
  7. getStringA(ByteBuffer byteBuffer, int length)
  8. getStringDTrimmed(ByteBuffer byteBuffer, int length)
  9. getStringFromBuffer(ByteBuffer buf, int len)