ByteBuffer sizeb = ByteBuffer.allocate(4);
try {
while (sizeb.hasRemaining()) in.read(sizeb);
sizeb.flip();
int howMany = sizeb.getInt();
sizeb.clear();
for (int i = 0; i < howMany; i++) {
while (sizeb.hasRemaining()) in.read(sizeb);
sizeb.flip();
int length = sizeb.getInt();
sizeb.clear();
ByteBuffer data = ByteBuffer.allocate(length);
while (data.hasRemaining()) in.read(data);
BigInteger result = new BigInteger(data.array());
System.out.println(result);
}
}
catch (IOException ex) {
System.err.println(ex);
}
finally {
try {
in.close();
}
catch (Exception ex) {
// We tried
}
}