Java ByteBuffer to String getString(ByteBuffer bb)

Here you can find the source of getString(ByteBuffer bb)

Description

Inputs a string from a ByteBuffer.

License

Open Source License

Parameter

Parameter Description
bb the input byte buffer

Exception

Parameter Description
IOException if an I/O error occurs

Return

the string

Declaration

public static String getString(ByteBuffer bb) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   w w  w  . j  a v  a  2s  .com*/
 * Copyright 2001 Sun Microsystems, Inc.
 * 
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 */

import java.io.DataInputStream;

import java.nio.ByteBuffer;

import java.io.IOException;

public class Main {
    /** Inputs a string from a DataInputStream.
     * @param dis the stream
     * @return the string
     * @throws IOException if an I/O error occurs */
    public static String getString(DataInputStream dis) throws IOException {
        int size = dis.readShort();
        char[] charBuffer = new char[size];
        for (int i = 0; i < size; i++) {
            charBuffer[i] = dis.readChar();
        }
        return new String(charBuffer, 0, size);
    }

    /** Inputs a string from a ByteBuffer.
     * @param bb the input byte buffer
     * @return the string
     * @throws IOException if an I/O error occurs */
    public static String getString(ByteBuffer bb) throws IOException {
        int size = bb.getShort();
        char[] charBuffer = new char[size];
        for (int i = 0; i < size; i++) {
            charBuffer[i] = bb.getChar();
        }
        return new String(charBuffer, 0, size);
    }
}

Related

  1. decodeString(ByteBuffer bb)
  2. decodeString(ByteBuffer buffer, String charset)
  3. decodeString(ByteBuffer src)
  4. getStr(ByteBuffer buff)
  5. getString(@Nonnull final ByteBuffer src)
  6. getString(ByteBuffer buf)
  7. getString(ByteBuffer buf)
  8. getString(ByteBuffer buf)
  9. getString(ByteBuffer buf, Charset encoding)