List of utility methods to do ByteBuffer Put
void | bufToStream(ByteBuffer b, OutputStream out) buf To Stream int i = b.position(); while (i < b.limit()) { out.write(b.get(i++)); |
InputStream | byteBufferToInputStream(final ByteBuffer byteBuffer) Produce an InputStream that is able to read from a ByteBuffer . return new InputStream() { final ByteBuffer buf = byteBuffer; @Override public int read() throws IOException { if (!buf.hasRemaining()) { return -1; return buf.get() & 0xFF; ... |
void | byteBufferToOutputStream(ByteBuffer in, OutputStream out) Reads the full contents of a ByteBuffer and writes them to an OutputStream. final int BUF_SIZE = 8192; if (in.hasArray()) { out.write(in.array(), in.arrayOffset() + in.position(), in.remaining()); } else { final byte[] b = new byte[Math.min(in.remaining(), BUF_SIZE)]; while (in.remaining() > 0) { int bytesToRead = Math.min(in.remaining(), BUF_SIZE); in.get(b, 0, bytesToRead); ... |
void | checkReadSpace(FileInputStream aStream, ByteBuffer aBuf) check Read Space if (aBuf.remaining() < 8) {
aBuf.flip();
aStream.getChannel().read(aBuf);
aBuf.flip();
|
long | computeSize(ByteBuffer[] buffers) compute Size long size = 0; for (ByteBuffer byteBuffer : buffers) { size += byteBuffer.remaining(); return size; |
void | convert(Object lock, CharsetEncoder encoder, ByteBuffer bytes, CharBuffer chars, OutputStream out) convert synchronized (lock) { checkEncoder(encoder); CoderResult result = encoder.encode(chars, bytes, true); while (true) { if (result.isError()) { throw new IOException(result.toString()); } else if (result.isOverflow()) { flushOutputStreamWriter(out, bytes, encoder, lock); ... |
String | deserializeString(ByteBuffer inputBuffer, Charset charset) Deserializes a string from byte buffer int size = inputBuffer.getInt(); byte[] value = new byte[size]; inputBuffer.get(value); return new String(value, charset); |
String | extractString(ByteArrayOutputStream byteBuffer) extract String String string = new String(byteBuffer.toByteArray(), CHARSET); byteBuffer.reset(); return string; |
void | flushOutputStreamWriter(OutputStream out, ByteBuffer bytes, CharsetEncoder encoder, Object lock) flush Output Stream Writer synchronized (lock) { checkEncoder(encoder); int position; if ((position = bytes.position()) > 0) { bytes.flip(); out.write(bytes.array(), 0, position); bytes.clear(); out.flush(); |
byte[][] | getZeroTerminatedStringBytesArray(ByteBuffer inputBuffer) get Zero Terminated String Bytes Array ArrayList<byte[]> array = new ArrayList<>(); while (inputBuffer.remaining() > 0) { array.add(getZeroTerminatedStringBytes(inputBuffer)); return array.toArray(new byte[array.size()][]); |