Java Utililty Methods File Read via ByteBuffer

List of utility methods to do File Read via ByteBuffer

Description

The list of methods to do File Read via ByteBuffer are organized into topic(s).

Method

ByteBufferblockingRead(SocketChannel so, long timeout, int bytes)
blocking Read
return blockingRead(so, timeout, new byte[bytes]);
longchannelCopy(@Nonnull @WillNotClose final ReadableByteChannel aSrc, @Nonnull @WillNotClose final WritableByteChannel aDest)
Copy all content from the source channel to the destination channel.
if (aSrc == null)
    throw new NullPointerException("sourceChannel");
if (!aSrc.isOpen())
    throw new IllegalArgumentException("sourceChannel is not open!");
if (aDest == null)
    throw new NullPointerException("desitnationChannel");
if (!aDest.isOpen())
    throw new IllegalArgumentException("desitnationChannel is not open!");
...
voidchannelCopy2(ReadableByteChannel src, WritableByteChannel dest)
copy content of src to dest,
using ByteBuffer#clear() to maker user byteBuffer was fully drained, while this may cause more SYSTEM CALLS
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(16 * 1024);
while (src.read(byteBuffer) != -1) {
    byteBuffer.flip();
    while (byteBuffer.hasRemaining()) {
        dest.write(byteBuffer);
    byteBuffer.clear();
ByteBufferClientReadWithBlock(SocketChannel sc)
Client Read With Block
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.clear();
try {
    while (sc.read(buffer) < 1)
        ;
} catch (IOException e) {
    e.printStackTrace();
return buffer;
StringClientReadWithWait(SocketChannel sc)
Client Read With Wait
StringBuilder sb = new StringBuilder();
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.clear();
long timeout = 5000;
long now = System.currentTimeMillis();
while (!sb.toString().trim().endsWith("#!")) {
    try {
        sc.read(buffer);
...
voidcopy(final ReadableByteChannel src, final WritableByteChannel dest)
copy
final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
while (src.read(buffer) != -1) {
    buffer.flip();
    dest.write(buffer);
    buffer.compact();
buffer.flip();
while (buffer.hasRemaining()) {
...
voidcopy(final ReadableByteChannel srcChannel, final WritableByteChannel destChannel)
Copies the content from one channel to another.
final ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
while (srcChannel.read(buffer) != -1) {
    buffer.flip();
    destChannel.write(buffer);
    buffer.compact();
buffer.flip();
while (buffer.hasRemaining()) {
...
voidcopyChannel(int bufferSize, ReadableByteChannel source, WritableByteChannel destination)
Copies the content of the source channel onto the destination channel.
inputNotNull(source, destination);
if (!source.isOpen() || !destination.isOpen())
    throw new IllegalStateException("Source and destination channels must be open.");
final java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocateDirect(bufferSize);
while (source.read(buffer) != -1) {
    buffer.flip();
    while (buffer.hasRemaining())
        destination.write(buffer);
...
voidcopyStream(BufferedReader reader, BufferedWriter writer)
copy Stream
String line = null;
while ((line = reader.readLine()) != null) {
    writer.write(line);
writer.flush();
longcount(final ReadableByteChannel src)
Reads data from an input stream counting the number of bytes.
final ByteBuffer buf = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
long count = 0;
int read = 0;
while ((read = src.read(buf)) != -1) {
    count += read;
    buf.clear();
return count;
...