Java ByteBuffer Write writeTo(ByteBuffer buffer, File file)

Here you can find the source of writeTo(ByteBuffer buffer, File file)

Description

write To

License

BSD License

Declaration

public static void writeTo(ByteBuffer buffer, File file) throws IOException 

Method Source Code

//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;
    }
}

Related

  1. writeString(String s, ByteBuffer buff)
  2. writeString(String string, ByteBuffer bb)
  3. writeStringArray(ByteBuffer buf, String[] array)
  4. writeStringData(ByteBuffer buff, String s, int len)
  5. writeStringToBuffer(ByteBuffer buffer, String message)
  6. writeToChannel(final WritableByteChannel destChannel, final ByteBuffer buffer)
  7. writeToChannel(WritableByteChannel chan, ByteBuffer buffer)
  8. writeToChannel(WritableByteChannel dst, ByteBuffer src)
  9. writeToFile(String name, ByteBuffer bb)