Here you can find the source of channelCopy2(ReadableByteChannel src, WritableByteChannel dest)
Parameter | Description |
---|---|
src | a parameter |
dest | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void channelCopy2(ReadableByteChannel src, WritableByteChannel dest) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; public class Main { /**// w ww. j a v a 2 s .c om * copy content of src to dest, <br/> * using {@link ByteBuffer#clear()} to maker user byteBuffer was fully drained, while this may * cause more SYSTEM CALLS * @param src * @param dest * @throws IOException */ public static void channelCopy2(ReadableByteChannel src, WritableByteChannel dest) throws IOException { ByteBuffer byteBuffer = ByteBuffer.allocateDirect(16 * 1024);// 16KB while (src.read(byteBuffer) != -1) { byteBuffer.flip(); while (byteBuffer.hasRemaining()) { dest.write(byteBuffer); } // make sure byteBuffer is empty, and ready for filling byteBuffer.clear(); } } }