Here you can find the source of getAscii(ByteBuffer bytes)
public static String getAscii(ByteBuffer bytes) throws IOException
//package com.java2s; /*// w w w . jav a 2s .co m Copyright 2011 Semantic Discovery, Inc. (www.semanticdiscovery.com) This file is part of the Semantic Discovery Toolkit. The Semantic Discovery Toolkit 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 3 of the License, or (at your option) any later version. The Semantic Discovery Toolkit 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 The Semantic Discovery Toolkit. If not, see <http://www.gnu.org/licenses/>. */ import java.nio.ByteBuffer; import java.io.IOException; public class Main { public static String getAscii(ByteBuffer bytes) throws IOException { StringBuilder builder = new StringBuilder(); if (bytes.remaining() < 2) throw new IOException("too few bytes specified for string value!"); int numBytes = getUnsignedShort(bytes); if (bytes.remaining() < numBytes) throw new IOException("too few bytes specified for string value!"); for (int i = 0; i < numBytes; i++) builder.append((char) bytes.get()); return builder.toString(); } public static String getAscii(ByteBuffer bytes, int pos) throws IOException { StringBuilder builder = new StringBuilder(); if ((pos + 2) > bytes.limit()) throw new IOException("too few bytes specified for string value!"); int numBytes = getUnsignedShort(bytes, pos); pos += 2; if ((pos + numBytes) > bytes.limit()) throw new IOException("too few bytes specified for string value!"); for (int i = 0; i < numBytes; i++) builder.append((char) bytes.get(pos + i)); return builder.toString(); } public static int getUnsignedShort(ByteBuffer bytes) throws IOException { if (bytes.remaining() < 2) throw new IOException("too few bytes specified for unsigned short value!"); byte b1 = bytes.get(); byte b2 = bytes.get(); int value = (int) ((0xff & b1) << 8 | (0xff & b2)); return value; } public static int getUnsignedShort(ByteBuffer bytes, int pos) throws IOException { if ((pos + 2) > bytes.limit()) throw new IOException("too few bytes specified for unsigned short value!"); byte b1 = bytes.get(pos); byte b2 = bytes.get(pos + 1); int value = (int) ((0xff & b1) << 8 | (0xff & b2)); return value; } }