Here you can find the source of writeTo(ByteBuffer buffer, File file)
public static void writeTo(ByteBuffer buffer, File file) throws IOException
//package com.java2s; /**/*from w ww .ja v a2 s. c om*/ * This class is part of JCodec ( www.jcodec.org ) This software is distributed * under FreeBSD License * * @author The JCodec project * */ import java.io.Closeable; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main { public static void writeTo(ByteBuffer buffer, File file) throws IOException { FileChannel out = null; try { out = new FileOutputStream(file).getChannel(); out.write(buffer); } finally { closeQuietly(out); } } public static void write(ByteBuffer to, ByteBuffer from) { if (from.hasArray()) { to.put(from.array(), from.arrayOffset() + from.position(), Math.min(to.remaining(), from.remaining())); } else { to.put(toArrayL(from, to.remaining())); } } public static void closeQuietly(Closeable channel) { if (channel == null) return; try { channel.close(); } catch (IOException e) { } } public static byte[] toArrayL(ByteBuffer buffer, int count) { byte[] result = new byte[Math.min(buffer.remaining(), count)]; buffer.duplicate().get(result); return result; } public static ByteBuffer duplicate(ByteBuffer bb) { ByteBuffer out = ByteBuffer.allocate(bb.remaining()); out.put(bb.duplicate()); out.flip(); return out; } }