Java ByteBuffer Write write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length)

Here you can find the source of write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length)

Description

A gathering write utility wrapper.

License

Apache License

Parameter

Parameter Description
out The GatheringByteChannel to write to
buffers The buffers to write
offset The offset into the buffers array
length The length in buffers to write

Exception

Parameter Description
IOException if unable write to the GatheringByteChannel

Return

The total bytes written

Declaration

public static long write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length)
        throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.GatheringByteChannel;

public class Main {
    /**/* w w  w .j a v  a2 s  . com*/
     * A gathering write utility wrapper.
     * <p>
     * This method wraps a gather write with a loop that handles the limitations of some operating systems that have a
     * limit on the number of buffers written. The method loops on the write until either all the content is written or
     * no progress is made.
     *
     * @param out
     *            The GatheringByteChannel to write to
     * @param buffers
     *            The buffers to write
     * @param offset
     *            The offset into the buffers array
     * @param length
     *            The length in buffers to write
     * @return The total bytes written
     * @throws IOException
     *             if unable write to the GatheringByteChannel
     */
    public static long write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length)
            throws IOException {
        long total = 0;
        write: while (length > 0) {
            // Write as much as we can
            long wrote = out.write(buffers, offset, length);

            // If we can't write any more, give up
            if (wrote == 0)
                break;

            // count the total
            total += wrote;

            // Look for unwritten content
            for (int i = offset; i < buffers.length; i++) {
                if (buffers[i].hasRemaining()) {
                    // loop with new offset and length;
                    length = length - (i - offset);
                    offset = i;
                    continue write;
                }
            }
            length = 0;
        }

        return total;
    }
}

Related

  1. write(ByteBuffer bb, int elementWidth, long value)
  2. write(ByteBuffer data, String filename, boolean append)
  3. write(final byte[] array, final int offset, final int end, final ByteBuffer outBB)
  4. write(MappedByteBuffer buffer, int pos, String asciString)
  5. write(SocketChannel channel, ByteBuffer b, PrimitiveIterator.OfInt iterator)
  6. write(SocketChannel p_channel, SSLEngine p_sslEngine, ByteBuffer p_outAppBuf, ByteBuffer p_outNetBuf)
  7. write(WritableByteChannel channel, ByteBuffer buffer, byte[] data)