fast Channel Copy - Java java.nio.channels

Java examples for java.nio.channels:ReadableByteChannel

Description

fast Channel Copy

Demo Code

// Copyright 2011 Google Inc. All Rights Reserved.
//package com.java2s;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class Main {
    public static void copy(InputStream inputStream,
            OutputStream outputStream) throws IOException {
        fastChannelCopy(Channels.newChannel(inputStream),
                Channels.newChannel(outputStream));
    }/*from www.j av  a2s  .  co m*/

    public static void fastChannelCopy(final ReadableByteChannel src,
            final WritableByteChannel dest) throws IOException {
        final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
        while (src.read(buffer) != -1) {
            // prepare the buffer to be drained
            buffer.flip();
            // write to the channel, may block
            dest.write(buffer);
            // If partial transfer, shift remainder down
            // If buffer is empty, same as doing clear()
            buffer.compact();
        }
        // EOF will leave buffer in fill state
        buffer.flip();
        // make sure the buffer is fully drained.
        while (buffer.hasRemaining()) {
            dest.write(buffer);
        }
    }
}

Related Tutorials