private static String readStringWithLength(SeekableByteChannel chan) throws IOException {
  ByteBuffer bb=ByteBuffer.allocate(4);
  bb.order(ByteOrder.nativeOrder());
  chan.read(bb);
  bb.flip();
  int len=bb.getInt();
  if (len > 1024) {
    throw new IOException("Invalid string length");
  }
  bb=ByteBuffer.allocate(len);
  if (chan.read(bb) != len) {
    throw new IOException("Not enough string");
  }
  byte[] data=bb.array();
  return (data[len - 1] == 0) ? new String(data,0,len - 1,StandardCharsets.UTF_8) : new String(data,StandardCharsets.UTF_8);
}

private static String readStringWithLength ( SeekableByteChannel chan ) throws IOException { ByteBuffer bb = ByteBuffer . allocate ( 4 ) ; bb . order ( ByteOrder . nativeOrder ( ) ) ; chan . read ( bb ) ; bb . flip ( ) ; int len = bb . getInt ( ) ; if ( len > 1024 ) { throw new IOException ( "Invalid string length" ) ; } bb = ByteBuffer . allocate ( len ) ; if ( chan . read ( bb ) != len ) { throw new IOException ( "Not enough string" ) ; } byte [ ] data = bb . array ( ) ; return ( data [ len - 1 ] == 0 ) ? new String ( data , 0 , len - 1 , StandardCharsets . UTF_8 ) : new String ( data , StandardCharsets . UTF_8 ) ; }
reads an lc style string from a channel , which is a int32 length plus a utf - 8 encoded string possibly ends with \ 0 .
