Java ByteBuffer Write writeAll(GatheringByteChannel ch, ByteBuffer... bbs)

Here you can find the source of writeAll(GatheringByteChannel ch, ByteBuffer... bbs)

Description

write All

License

Open Source License

Declaration

public static void writeAll(GatheringByteChannel ch, ByteBuffer... bbs) throws IOException 

Method Source Code

//package com.java2s;
/*//from  w w  w.  ja v a2  s  . com
 * Copyright (C) 2015 Timo Vesalainen <timo.vesalainen@iki.fi>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.GatheringByteChannel;

import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;

public class Main {
    public static void writeAll(GatheringByteChannel ch, ByteBuffer... bbs) throws IOException {
        writeAll(ch, bbs, 0, bbs.length);
    }

    /**
     * Attempts to write all remaining data in bbs.
     * @param ch Target
     * @param bbs 
     * @param offset
     * @param length
     * @throws IOException If couldn't write all.
     */
    public static void writeAll(GatheringByteChannel ch, ByteBuffer[] bbs, int offset, int length)
            throws IOException {
        long all = 0;
        for (int ii = 0; ii < length; ii++) {
            all += bbs[offset + ii].remaining();
        }
        int count = 0;
        while (all > 0) {
            long rc = ch.write(bbs, offset, length);
            if (rc == 0) {
                count++;
            } else {
                all -= rc;
            }
            if (count > 100) {
                throw new IOException("Couldn't write all.");
            }
        }
    }

    /**
     * Writes to ch until no remaining left or throws IOException
     * @param ch
     * @param bb
     * @throws IOException 
     */
    public static void writeAll(WritableByteChannel ch, ByteBuffer bb) throws IOException {
        int count = 0;
        while (bb.hasRemaining()) {
            int rc = ch.write(bb);
            if (rc == 0) {
                count++;
            }
            if (count > 100) {
                throw new IOException("Couldn't write all.");
            }
        }
    }

    /**
     * Writes string bytes to channel using charset.
     * @param ch
     * @param text
     * @param charset
     * @throws IOException 
     */
    public static final void write(WritableByteChannel ch, String text, Charset charset) throws IOException {
        write(ch, text.getBytes(charset));
    }

    /**
     * Writes bytes to channel
     * @param ch
     * @param bytes
     * @throws IOException 
     */
    public static final void write(WritableByteChannel ch, byte[] bytes) throws IOException {
        write(ch, bytes, 0, bytes.length);
    }

    /**
     * Writes length bytes to channel starting at offset
     * @param ch
     * @param bytes
     * @param offset
     * @param length
     * @throws IOException 
     */
    public static final void write(WritableByteChannel ch, byte[] bytes, int offset, int length)
            throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(length);
        bb.put(bytes, offset, length);
        bb.flip();
        writeAll(ch, bb);
    }

    /**
     * GatheringChannel support.
     * @param channel
     * @param srcs
     * @param offset
     * @param length
     * @return
     * @throws IOException 
     */
    public static final long write(WritableByteChannel channel, ByteBuffer[] srcs, int offset, int length)
            throws IOException {
        long res = 0;
        for (int ii = 0; ii < length; ii++) {
            ByteBuffer bb = srcs[ii + offset];
            if (bb.hasRemaining()) {
                res += channel.write(bb);
                if (bb.hasRemaining()) {
                    break;
                }
            }
        }
        return res;
    }

    /**
     * GatheringChannel support.
     * @param channel
     * @param srcs
     * @return
     * @throws IOException 
     */
    public static final long write(WritableByteChannel channel, ByteBuffer[] srcs) throws IOException {
        return write(channel, srcs, 0, srcs.length);
    }
}

Related

  1. write(WritableByteChannel channel, ByteBuffer[] srcs, int offset, int length)
  2. write(WritableByteChannel out, ByteBuffer buffer)
  3. write(WritableByteChannel socketChannel, SSLEngine engine, ByteBuffer plainOut, ByteBuffer cypherOut)
  4. writeAll(ByteBuffer buf, WritableByteChannel channel)
  5. writeAll(ByteChannel channel, ByteBuffer buffer)
  6. writeAllToChannel(List buffers, WritableByteChannel channel)
  7. writeBE(ByteBuffer bb, int elementWidth, long value)
  8. writeBER32(ByteBuffer buffer, int value)
  9. writeBigInteger(ByteBuffer bb, BigInteger bigInteger, int length)