/** * Decode a node - it is better to use fetchDecode which may avoid an additional copy * in getting the node from the ObjectFile. */ public static Node decode(ByteBuffer bb) { bb.position(0); Node n = nodec.decode(bb, null); return n; }
/** * Encode a node - it is better to use encodeStore which may avoid an additional copy * in getting the node into the ObjectFile and may avoid short-term byte buffer * allocation. */ public static ByteBuffer encode(Node node) { int maxSize = nodec.maxSize(node); ByteBuffer bb = ByteBuffer.allocate(maxSize); int len = nodec.encode(node, bb, null); bb.limit(len); bb.position(0); return bb; }
private void test(Node n) { int maxSize = nodec.maxSize(n) ; ByteBuffer bb = ByteBuffer.allocate(maxSize) ; int x = nodec.encode(n, bb, null) ; int bbLen = bb.limit()-bb.position(); assertEquals(bbLen, x) ; assertEquals(0, bb.position()) ; ByteBuffer bb2 = ByteBufferLib.duplicate(bb) ; Node n2 = nodec.decode(bb2, null) ; assertEquals(n, n2) ; } }
/** Encode and write, using the space provided which is assumed to be large enough. */ private static long encodeWrite(Node node, ObjectFile file, ByteBuffer bb) { int len = nodec.encode(node, bb, null); long x = file.write(bb); return x; }
/** * Encode and write a {@link Node} to the {@link ObjectFile}. * Uses the given {@link ByteBuffer} for encoding space if possible. * Returns the location, suitable for use with {@link #fetchDecode}. */ public static long encodeStore(Node node, ObjectFile file, ByteBuffer bb) { int maxSize = nodec.maxSize(node); if ( bb == null ) return allocEncodeWrite(node, file, maxSize); if ( bb.capacity() < maxSize ) // Buffer may not be big enough. return allocEncodeWrite(node, file, maxSize); // Use buffer provided. bb.clear(); return encodeWrite(node, file, bb); }
private void test(Node n) { int maxSize = nodec.maxSize(n) ; ByteBuffer bb = ByteBuffer.allocate(maxSize) ; int x = nodec.encode(n, bb, null) ; int bbLen = bb.limit()-bb.position(); assertEquals(bbLen, x) ; assertEquals(0, bb.position()) ; ByteBuffer bb2 = ByteBufferLib.duplicate(bb) ; Node n2 = nodec.decode(bb2, null) ; assertEquals(n, n2) ; } }