Java ByteBuffer Write write(WritableByteChannel channel, ByteBuffer buffer, byte[] data)

Here you can find the source of write(WritableByteChannel channel, ByteBuffer buffer, byte[] data)

Description

write

License

Open Source License

Declaration

static void write(WritableByteChannel channel, ByteBuffer buffer,
            byte[] data) throws IOException 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2009 Remy Chi Jian Suen and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   w  w w.j  a v a2 s.c om*/
 *     Remy Chi Jian Suen - initial API and implementation
 ******************************************************************************/

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.WritableByteChannel;

public class Main {
    static void write(WritableByteChannel channel, ByteBuffer buffer,
            byte[] data) throws IOException {
        buffer.clear();

        int remaining = data.length;
        int limit = buffer.limit();
        if (remaining < limit) {
            buffer.put(data);
            buffer.flip();
            channel.write(buffer);
            buffer.clear();
            return;
        }

        int offset = 0;
        while (remaining > limit) {
            buffer.put(data, offset, limit);
            buffer.flip();
            channel.write(buffer);
            buffer.clear();

            offset += limit;
            remaining -= limit;
        }

        buffer.put(data, offset, remaining);
        buffer.flip();
        channel.write(buffer);
        buffer.clear();
    }
}

Related

  1. write(final byte[] array, final int offset, final int end, final ByteBuffer outBB)
  2. write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length)
  3. write(MappedByteBuffer buffer, int pos, String asciString)
  4. write(SocketChannel channel, ByteBuffer b, PrimitiveIterator.OfInt iterator)
  5. write(SocketChannel p_channel, SSLEngine p_sslEngine, ByteBuffer p_outAppBuf, ByteBuffer p_outNetBuf)
  6. write(WritableByteChannel channel, ByteBuffer[] srcs)
  7. write(WritableByteChannel channel, ByteBuffer[] srcs, int offset, int length)
  8. write(WritableByteChannel out, ByteBuffer buffer)
  9. write(WritableByteChannel socketChannel, SSLEngine engine, ByteBuffer plainOut, ByteBuffer cypherOut)