Code example for ByteBuffer

Methods: array, limit, position, remaining

0
 
	private static final Logger logger = LoggerFactory.getLogger(Util.class);
	 
	public static void barf(final ByteBuffer bb, final OutputStream out) throws IOException {
		final DataOutputStream dout = new DataOutputStream(out);
		final int remaining = bb.remaining();
		dout.writeInt(remaining);
		dout.write(bb.array(), bb.position(), bb.limit());
	} 
	 
	public static byte[] slurp(final InputStream in) throws IOException {
		final DataInputStream din = new DataInputStream(new BufferedInputStream(in));
		final int n = din.readInt();
		logger.info("reading {} bytes", n);
		final byte[] data = new byte[n];
		for (int i = 0; i < n; i++) {
			data[i] = din.readByte();
		} 
		logger.info("finished reading");
		return data;
	}