Here you can find the source of copyChannel(int bufferSize, ReadableByteChannel source, WritableByteChannel destination)
Parameter | Description |
---|---|
bufferSize | size of the temp buffer to use for this copy. |
source | the source ReadableByteChannel . |
destination | the destination WritableByteChannel ;. |
Parameter | Description |
---|---|
IOException | in case something bad happens. |
public static void copyChannel(int bufferSize, ReadableByteChannel source, WritableByteChannel destination) throws IOException
//package com.java2s; /* (c) 2015 Open Source Geospatial Foundation - all rights reserved * (c) 2001 - 2015 OpenPlans//from w ww . j av a2 s . c om * This code is licensed under the GPL 2.0 license, available at the root * application directory. */ import java.io.IOException; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; public class Main { /** * Copies the content of the source channel onto the destination channel. * * @param bufferSize size of the temp buffer to use for this copy. * @param source the source {@link ReadableByteChannel}. * @param destination the destination {@link WritableByteChannel};. * @throws IOException in case something bad happens. */ public static void copyChannel(int bufferSize, ReadableByteChannel source, WritableByteChannel destination) throws IOException { inputNotNull(source, destination); if (!source.isOpen() || !destination.isOpen()) throw new IllegalStateException( "Source and destination channels must be open."); final java.nio.ByteBuffer buffer = java.nio.ByteBuffer .allocateDirect(bufferSize); while (source.read(buffer) != -1) { //prepare the buffer for draining buffer.flip(); //write to destination while (buffer.hasRemaining()) destination.write(buffer); //clear buffer.clear(); } } /** * Checks if the input is not null. * @param oList list of elements to check for null. */ private static void inputNotNull(Object... oList) { for (Object o : oList) if (o == null) throw new NullPointerException( "Input objects cannot be null"); } }