Java examples for java.nio:ByteBuffer Read
read String from ByteBuffer
//package com.java2s; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; public class Main { public static String readString(ByteBuffer b) { int len = b.getInt(); if (b.position() + len > b.capacity() - 1 || len < 0) { throw new IllegalStateException( "Tried to read string of length " + len + " at " + b.position()); }/*from ww w.j a va 2 s . co m*/ if (len > 0) { byte[] buf = new byte[len]; b.get(buf); b.get(); try { return new String(buf, "ISO-8859-15"); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e.getMessage()); } } else { return ""; } } }