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

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

Description

Create String offset from position by offset upto length using encoding, and position of buffer is moved to after position + offset + length

License

Open Source License

Parameter

Parameter Description
buffer a parameter
offset a parameter
length a parameter
encoding a parameter

Declaration

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

Method Source Code


//package com.java2s;
/*//from   www . jav a  2s .c o m
 * 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.io.*;
import java.nio.ByteBuffer;

public class Main {
    /**
     * Create String starting from offset upto length using encoding
     *
     * @param b
     * @param offset
     * @param length
     * @param encoding
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getString(byte[] b, int offset, int length, String encoding) {
        try {
            return new String(b, offset, length, encoding);
        } catch (UnsupportedEncodingException ue) {
            //Shouldnt have to worry about this exception as should only be calling with well defined charsets
            throw new RuntimeException(ue);
        }
    }

    /**
     * Create String offset from position by offset upto length using encoding, and position of buffer
     * is moved to after position + offset + length
     *
     * @param buffer
     * @param offset
     * @param length
     * @param encoding
     * @return
     */
    public static String getString(ByteBuffer buffer, int offset, int length, String encoding) {
        byte[] b = new byte[length];
        buffer.position(buffer.position() + offset);
        buffer.get(b);
        try {
            return new String(b, 0, length, encoding);
        } catch (UnsupportedEncodingException uee) {
            //TODO, will we ever use unsupported encodings
            throw new RuntimeException(uee);
        }
    }
}

Related

  1. getString(ByteBuffer buf, Charset encoding)
  2. getString(ByteBuffer buf, int length, String charsetName)
  3. getString(ByteBuffer buffer)
  4. getString(ByteBuffer buffer)
  5. getString(ByteBuffer buffer, int length)
  6. getString(ByteBuffer buffer, int size)
  7. getString(ByteBuffer buffer, int size)
  8. getString(ByteBuffer buffer, String charFormat)
  9. getString(ByteBuffer byteBuffer, int size)