Java ByteBuffer Put writeFully(ByteBuffer buffer, OutputStream os)

Here you can find the source of writeFully(ByteBuffer buffer, OutputStream os)

Description

Writes all the remaining bytes of the ByteBuffer to the given OutputStream .

License

Apache License

Parameter

Parameter Description
buffer The ByteBuffer that will be read completely.
os The OutputStream that will be written to.

Exception

Parameter Description
IOException If any error occurs while writing to the OutputStream.
IllegalArgumentException When the ByteBuffer is not backed by an array.

Declaration

public static void writeFully(ByteBuffer buffer, OutputStream os) throws IOException 

Method Source Code

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

import java.io.IOException;

import java.io.OutputStream;
import java.nio.ByteBuffer;

public class Main {
    /**//from   ww w.j  a v  a 2 s . c o  m
     * Writes all the remaining bytes of the {@link ByteBuffer} to the given {@link OutputStream}. When you have written
     * to the buffer, don't forget to call {@link ByteBuffer#flip()} to make those readable again.
     * 
     * @param buffer
     *            The {@link ByteBuffer} that will be read completely.
     * @param os
     *            The {@link OutputStream} that will be written to.
     * @throws IOException
     *             If any error occurs while writing to the {@link OutputStream}.
     * @throws IllegalArgumentException
     *             When the {@link ByteBuffer} is not backed by an array.
     */
    public static void writeFully(ByteBuffer buffer, OutputStream os) throws IOException {
        if (!buffer.hasArray()) {
            throw new IllegalArgumentException("This only works with buffer that have a backing array");
        }
        os.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
        buffer.position(buffer.limit());
    }
}

Related

  1. wrap(ByteBuffer byteBuffer, ByteBuffer outputBB, SSLEngine sslEngine)
  2. write(ByteBuffer buffer, DataOutput out)
  3. write(ByteBuffer byteBuffer, OutputStream outputStream)
  4. writeByteBufferList(List list, DataOutputStream os)
  5. writeBytes(OutputStream os, ByteBuffer buffer)
  6. writeFully(OutputStream out, ByteBuffer buf)
  7. writeOutputStream(OutputStream output, ByteBuffer data)
  8. writeOutputStreamWriter(String str, int offset, int count, OutputStream out, ByteBuffer bytes, CharsetEncoder encoder, Object lock)
  9. writePackedLong(ByteBuffer output, long value)