Here you can find the source of write(WritableByteChannel channel, ByteBuffer buffer, byte[] data)
static void write(WritableByteChannel channel, ByteBuffer buffer, byte[] data) throws IOException
//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(); } }