Java ByteBuffer to String getString(ByteBuffer buf)

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

Description

get String

License

Open Source License

Declaration

private static String getString(ByteBuffer buf)
            throws BufferUnderflowException 

Method Source Code

//package com.java2s;
/*//from w  w  w  .  ja v a  2 s  .c om
 This file is part of the Greenfoot program. 
 Copyright (C) 2005-2009,2010,2011,2012  Poul Henriksen and Michael Kolling 
    
 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. 
    
 This program 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 General Public License for more details. 
    
 You should have received a copy of the GNU General Public License 
 along with this program; if not, write to the Free Software 
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
    
 This file is subject to the Classpath exception as provided in the  
 LICENSE.txt file that accompanied this code.
 */

import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

public class Main {
    private static String getString(ByteBuffer buf)
            throws BufferUnderflowException {
        int len = buf.getShort(); //2-bytes for length
        if (len == -1)
            return null;

        char[] cs = new char[len];
        for (int i = 0; i < len; i++) {
            cs[i] = buf.getChar();
        }
        return new String(cs);
    }
}

Related

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