Here you can find the source of write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length)
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 |
Parameter | Description |
---|---|
IOException | if unable write to the GatheringByteChannel |
public static long write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length) throws IOException
//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; } }