package com.mindprod.example;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import static java.lang.System.*;
/**
* Example use of java.nio for writing.
* <p/>
* It needs a directory E:\temp to write a test file to.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2009-01-01 initial version
* @since 2009-01-01
*/
public class TestNioWrite
{
/**
* Display state of channel/buffer.
*
* @param where description of where we are in the program to label the state snapzhot
* @param fc FileChannel reading/writing.
* @param b Buffer to display state of:
*
* @throws java.io.IOException if I/O problems.
*/
private static void showStats( String where, FileChannel fc, Buffer b ) throws IOException
{
out.println( where +
" channelPosition: " +
fc.position() +
" bufferPosition: " +
b.position() +
" limit: " +
b.limit() +
" remaining: " +
b.remaining() +
" capacity: " +
b.capacity() );
}
/**
* write some raw bytes to file
*
* @throws java.io.IOException if problems writing bytes.
*/
private static void writeRawBytes() throws IOException
{
final FileOutputStream fos = new FileOutputStream( "E:/temp/tempout.txt" );
FileChannel fc = fos.getChannel();
ByteBuffer buffer = ByteBuffer.allocate( 1024 * 15 );
showStats( "newly allocated write", fc, buffer );
for ( int i = 0; i < 10; i++ )
{
buffer.clear();
showStats( "after clear", fc, buffer );
for ( int j = 0; j < 100; j++ )
{
buffer.put( "hello".getBytes( "8859_1") );
showStats( "after put", fc, buffer );
}
showStats( "before flip", fc, buffer );
buffer.flip();
showStats( "after flip", fc, buffer );
fc.write( buffer );
showStats( "after write", fc, buffer );
}
fc.close();
}
/**
* test harness
*
* @param args not used
*
* @throws java.io.IOException if trouble writing bytes.
*/
public static void main( String[] args ) throws IOException
{
writeRawBytes();
}
}