Here you can find the source of writeTo(ByteBuffer buffer, OutputStream out)
public static void writeTo(ByteBuffer buffer, OutputStream out) throws IOException
//package com.java2s; // are made available under the terms of the Eclipse Public License v1.0 import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; public class Main { static final int TEMP_BUFFER_SIZE = 4096; public static void writeTo(ByteBuffer buffer, OutputStream out) throws IOException { if (buffer.hasArray()) out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); else {//from w w w.j av a 2 s .co m byte[] bytes = new byte[TEMP_BUFFER_SIZE]; while (buffer.hasRemaining()) { int byteCountToWrite = Math.min(buffer.remaining(), TEMP_BUFFER_SIZE); buffer.get(bytes, 0, byteCountToWrite); out.write(bytes, 0, byteCountToWrite); } } } }