Java ReadableByteChannel Read transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer, final WritableByteChannel sink)

Here you can find the source of transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer, final WritableByteChannel sink)

Description

Platform-independent channel-to-channel transfer method.

License

Apache License

Parameter

Parameter Description
source the source channel to read bytes from
count the number of bytes to transfer (must be >= 0L )
throughBuffer the buffer to transfer through (must not be null )
sink the sink channel to write bytes to

Exception

Parameter Description
IOException if an I/O error occurs during the transfer of bytes

Return

the number of bytes actually transferred (possibly 0)

Declaration

public static long transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer,
        final WritableByteChannel sink) throws IOException 

Method Source Code

//package com.java2s;
/*/*from  w w  w  . ja  v a  2  s. com*/
 * JBoss, Home of Professional Open Source
 *
 * Copyright 2008 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.ReadableByteChannel;

import java.nio.channels.WritableByteChannel;

public class Main {
    /**
     * Platform-independent channel-to-channel transfer method.  Uses regular {@code read} and {@code write} operations
     * to move bytes from the {@code source} channel to the {@code sink} channel.  After this call, the {@code throughBuffer}
     * should be checked for remaining bytes; if there are any, they should be written to the {@code sink} channel before
     * proceeding.  This method may be used with NIO channels, XNIO channels, or a combination of the two.
     * <p>
     * If either or both of the given channels are blocking channels, then this method may block.
     *
     * @param source the source channel to read bytes from
     * @param count the number of bytes to transfer (must be >= {@code 0L})
     * @param throughBuffer the buffer to transfer through (must not be {@code null})
     * @param sink the sink channel to write bytes to
     * @return the number of bytes actually transferred (possibly 0)
     * @throws IOException if an I/O error occurs during the transfer of bytes
     */
    public static long transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer,
            final WritableByteChannel sink) throws IOException {
        long res;
        long total = 0L;
        throughBuffer.limit(0);
        while (total < count) {
            throughBuffer.compact();
            try {
                if (count - total < (long) throughBuffer.remaining()) {
                    throughBuffer.limit((int) (count - total));
                }
                res = source.read(throughBuffer);
                if (res <= 0) {
                    return total == 0L ? res : total;
                }
            } finally {
                throughBuffer.flip();
            }
            res = sink.write(throughBuffer);
            if (res == 0) {
                return total;
            }
            total += res;
        }
        return total;
    }
}

Related

  1. readLengthAndString(ReadableByteChannel channel, ByteBuffer buffer)
  2. readMap(ReadableByteChannel channel, ByteBuffer buffer)
  3. readObjFromChannel(ReadableByteChannel chan, ByteBuffer buffer, AtomicBoolean endPointCrashed)
  4. readToBuffer(ReadableByteChannel src, ByteBuffer dst)
  5. retryRead(ReadableByteChannel channel, ByteBuffer buffer)
  6. transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer, final WritableByteChannel sink)
  7. writeEntryToTemp(File tempDir, ByteBuffer buffer, ReadableByteChannel channel)