Java ByteBuffer Write writeFully(WritableByteChannel bc, ByteBuffer buf)

Here you can find the source of writeFully(WritableByteChannel bc, ByteBuffer buf)

Description

Write a ByteBuffer to a WritableByteChannel, handling short writes.

License

Apache License

Parameter

Parameter Description
bc The WritableByteChannel to write to
buf The input buffer

Exception

Parameter Description
IOException On I/O error

Declaration

public static void writeFully(WritableByteChannel bc, ByteBuffer buf) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

public class Main {
    /**//w w w  .j  a va  2  s.  c o m
     * Write a ByteBuffer to a WritableByteChannel, handling short writes.
     * 
     * @param bc               The WritableByteChannel to write to
     * @param buf              The input buffer
     * @throws IOException     On I/O error
     */
    public static void writeFully(WritableByteChannel bc, ByteBuffer buf) throws IOException {
        do {
            bc.write(buf);
        } while (buf.remaining() > 0);
    }

    /**
     * Write a ByteBuffer to a FileChannel at a given offset, 
     * handling short writes.
     * 
     * @param fc               The FileChannel to write to
     * @param buf              The input buffer
     * @param offset           The offset in the file to start writing at
     * @throws IOException     On I/O error
     */
    public static void writeFully(FileChannel fc, ByteBuffer buf, long offset) throws IOException {
        do {
            offset += fc.write(buf, offset);
        } while (buf.remaining() > 0);
    }
}

Related

  1. writeFully(FileChannel channel, long offset, ByteBuffer buf)
  2. writeFully(FileChannel fc, ByteBuffer buf, long offset)
  3. writeFully(FileChannel fileChannel, long filePosition, ByteBuffer buffer)
  4. writeFully(final FileChannel channel, final ByteBuffer dst, final long position)
  5. writeFully(final WritableByteChannel channel, final ByteBuffer buf)
  6. writeFully(WritableByteChannel channel, ByteBuffer[] srcs)
  7. writeHalf(final ByteBuffer buf, final int value)
  8. writeHeader(int type, ByteBuffer lengthBuffer, ByteBuffer buffer)
  9. writeHeaderAndLsd(ByteBuffer out, int width, int height, boolean hasGct, int gctSize)