7 import java.nio.channels.*;
10 public static ByteBuffer readns(ReadableByteChannel sk) throws IOException {
13 int c = Utils.read(sk);
16 else if((c >= '0') && (c <= '9'))
17 hln = (hln * 10) + (c - '0');
19 throw(new IOException("unexpected eof in netstring header"));
21 throw(new IOException("invalid netstring length byte: " + (c & 0xff)));
23 ByteBuffer data = Utils.readall(sk, ByteBuffer.allocate(hln));
24 if(Utils.read(sk) != ',')
25 throw(new IOException("non-terminated netstring"));
29 public static Map<ByteBuffer, ByteBuffer> splithead(ByteBuffer ns) {
30 Map<ByteBuffer, ByteBuffer> ret = new HashMap<>();
32 for(int i = 0, p = 0; i < ns.limit(); i++) {
34 ByteBuffer s = ns.duplicate();
35 s.position(p).limit(i);
48 public static Map<ByteBuffer, ByteBuffer> readhead(ReadableByteChannel sk) throws IOException {
49 return(splithead(readns(sk)));