Java File Write via ByteBuffer writeByte(WritableByteChannel channel, byte value)

Here you can find the source of writeByte(WritableByteChannel channel, byte value)

Description

write Byte

License

BSD License

Declaration

public static void writeByte(WritableByteChannel channel, byte value) throws IOException 

Method Source Code

//package com.java2s;
/**// w  ww.  j a v a  2  s  .  c  o  m
 * This class is part of JCodec ( www.jcodec.org ) This software is distributed
 * under FreeBSD License
 * 
 * @author The JCodec project
 * 
 */

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.WritableByteChannel;

public class Main {
    public static void writeByte(WritableByteChannel channel, byte value) throws IOException {
        channel.write((ByteBuffer) ByteBuffer.allocate(1).put(value).flip());
    }

    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 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. write(File file, String content, String encoding)
  2. write(InputStream source, File target)
  3. write(Path file, String content)
  4. write(SeekableByteChannel channel, long start, byte[] bytes)
  5. write24BitInteger(int integer, ByteOrder order)
  6. writeBytes(Path file, byte[] bytes)
  7. writeComment(File zipFile, String comment)
  8. writeDataLengthsToHeader(FileOutputStream fpo)
  9. writeDelimitedToOutputStream(byte[] bytes, OutputStream outputStream)