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

Transfer the data from the source to the sink using the given through buffer to pass data through.

License

Open Source License

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;
/*// w  w  w  .j  av a  2s . c o m
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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 {
    /**
     * Transfer the data from the source to the sink using the given through buffer to pass data through.
     */
    public static long transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer,
            final WritableByteChannel sink) throws IOException {
        long total = 0L;
        while (total < count) {
            throughBuffer.clear();
            if (count - total < throughBuffer.remaining()) {
                throughBuffer.limit((int) (count - total));
            }

            try {
                long res = source.read(throughBuffer);
                if (res <= 0) {
                    return total == 0L ? res : total;
                }
            } finally {
                throughBuffer.flip();

            }
            while (throughBuffer.hasRemaining()) {
                long res = sink.write(throughBuffer);
                if (res <= 0) {
                    return total;
                }
                total += res;
            }
        }
        return total;
    }
}

Related

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