Java ByteBuffer to String getString(final ByteBuffer buffer)

Here you can find the source of getString(final ByteBuffer buffer)

Description

Reads a String from a ByteBuffer.

License

Open Source License

Parameter

Parameter Description
buffer a parameter

Return

the String at the buffer's current position

Declaration

public static String getString(final ByteBuffer buffer) 

Method Source Code

//package com.java2s;
/* *********************************************************************** *
 * project: org.matsim.*/*from   w  w w.  ja  v a2 s .co  m*/
 * ByteBufferUtils.java
 *                                                                         *
 * *********************************************************************** *
 *                                                                         *
 * copyright       : (C) 2009 by the members listed in the COPYING,        *
 *                   LICENSE and WARRANTY file.                            *
 * email           : info at matsim dot org                                *
 *                                                                         *
 * *********************************************************************** *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *   See also COPYING, LICENSE and WARRANTY file                           *
 *                                                                         *
 * *********************************************************************** */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Reads a String from a ByteBuffer. Reads first an int for the length of the String,
     * and then the corresponding number of characters. Increments the position of the
     * ByteBuffer according to the length of the String. 
     * 
     * @param buffer
     * @return the String at the buffer's current position
     */
    public static String getString(final ByteBuffer buffer) {
        int length = buffer.getInt();
        char[] chBuffer = new char[length];
        for (int i = 0; i < length; i++) {
            chBuffer[i] = buffer.getChar();
        }
        return new String(chBuffer);
    }
}

Related

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