Java WritableByteChannel tutorial with examples

PreviousNext

A channel that can write bytes.

Introduction

A channel that can write bytes.

Only one write operation upon a writable channel may be in progress at any given time.

If one thread initiates a write operation upon a channel then any other thread that attempts to initiate another write operation will block until the first operation is complete.

Whether or not other kinds of I/O operations may proceed concurrently with a write operation depends upon the type of the channel.

Example

The following code shows how to use WritableByteChannel from java.nio.channels.

Example 1

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.zip.GZIPInputStream;

public class Main {

    public static void main(String[] args) throws IOException {

        FileInputStream fin = new FileInputStream(args[0]);
        GZIPInputStream gzin = new GZIPInputStream(fin);
        ReadableByteChannel in = Channels.newChannel(gzin);

        WritableByteChannel out = Channels.newChannel(System.out);
        ByteBuffer buffer = ByteBuffer.allocate(65536);
        while (in.read(buffer) != -1) {
            buffer.flip();/* w   w w  . d    em    o  2  s.    c  o  m*/
            out.write(buffer);
            buffer.clear();
        }
    }
}

Example 2

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.zip.GZIPInputStream;

public class Main {

    public static void main(String[] args) throws IOException {

        FileInputStream fin = new FileInputStream(args[0]);
        GZIPInputStream gzin = new GZIPInputStream(fin);
        ReadableByteChannel in = Channels.newChannel(gzin);

        WritableByteChannel out = Channels.newChannel(System.out);
        ByteBuffer buffer = ByteBuffer.allocate(65536);
        while (in.read(buffer) != -1) {
            buffer.flip();//   w  w  w   .  d  em  o  2   s  .   co   m 
            out.write(buffer);
            buffer.clear();
        }
    }
}

Example 3

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;

public class Main {

    public static void main(String[] args) throws Exception {
        int port = 19;

        SocketAddress address = new InetSocketAddress("127.0.0.1", port);
        SocketChannel client = SocketChannel.open(address);

        ByteBuffer buffer = ByteBuffer.allocate(74);
        WritableByteChannel out = Channels.newChannel(System.out);

        while (client.read(buffer) != -1) {
            buffer.flip();//    w  w  w.    de    mo  2  s   .   c  om  
            out.write(buffer);
            buffer.clear();
        }
    }
}
PreviousNext

Related