FileChannel
Write to Channel with ByteBuffer
With this example we are going to demonstrate how to write data to a NIO Channel using a ByteBuffer in Java. In particular we are going to read data from a specific file in the file system and write them to a destination file. In short what we do is the following :
- To write data to a channel you should create a WritableByteChannel. To do so you can create a FileOutputStream object to encapsulate the target file. Then use the
getChannel()
API method of the FileOutputStream object to get the file channel to write data to - We can allocate a new direct (memory-mapped) byte buffer by using the ByteBuffer class
allocateDirect(int)
API method - To writes a sequence of bytes to the channel from the given buffer all you have to do is use the
write(ByteBuffer)
API method of the WritableByteChannel class providing the buffer as the input attribute. Do not forget toflip()
the buffer prior writing its contents to the channel since the writing will be done from the buffer’s current position on-wards
as described in the code snippet below.
Do not forget to close the channel after you are done processing the file so as to release operating system resources.
package com.javacodegeeks.snippets.core; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.channels.WritableByteChannel; public class WriteToChannelWithByteBuffer { public static void main(String[] args) { try { // destination file channel // return the unique FileChannel object associated with this file output stream. WritableByteChannel channel = new FileOutputStream("out.xml").getChannel(); // Allocate a new direct (memory-mapped) byte buffer with a 10 byte capacity ByteBuffer buf = ByteBuffer.allocateDirect(10); InputStream is = new FileInputStream("in.xml"); byte[] byteArray = new byte[1024]; int count = 0; int index = 0; while (count >= 0) { if (index == count) { count = is.read(byteArray); index = 0; } while (index < count && buf.hasRemaining()) { // Writes the given byte into this buffer at the current position // and then increments the position. buf.put(byteArray[index++]); } // Flips this buffer. The limit is set to the current position and then // the position is set to zero. If the mark is defined then it is discarded. buf.flip(); // Writes a sequence of bytes to this channel from the given buffer. channel.write(buf); // Check if there are any elements between the current position and the limit. if (buf.hasRemaining()) { // compacts the buffer, i.e. the bytes between the buffer's current // position and its limit, if any, are copied to the beginning of the buffer. buf.compact(); } else { // Clears this buffer. The position is set to zero, the limit // is set to the capacity, and the mark is discarded. buf.clear(); } } // close the channel channel.close(); } catch (IOException e) { System.out.println("I/O Error: " + e.getMessage()); } } }
This was an example of how to write to a Channel using a ByteBuffer in Java.