Java ByteBuffer Put writeBytes(OutputStream os, ByteBuffer buffer)

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

Description

Writes the buffer content to the output stream.

License

Open Source License

Parameter

Parameter Description
os The output stream.
buffer The ByteBuffer.

Declaration

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

Method Source Code

//package com.java2s;
// See LICENSE.txt for license information

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

import java.nio.channels.Channels;

import java.nio.channels.WritableByteChannel;

public class Main {
    /**/*from   w ww  .j  ava  2  s . c  o  m*/
     * Writes the buffer content to the output stream.
     * @param os The output stream.
     * @param buffer The buffer to write.
     */
    public static void writeBytes(OutputStream os, byte[] buffer) throws IOException {
        os.write(buffer);
    }

    /**
     * Writes the buffer content to the output stream.
     * @param os The output stream.
     * @param buffer The ByteBuffer.
     */
    public static void writeBytes(OutputStream os, ByteBuffer buffer) throws IOException {
        WritableByteChannel ch = Channels.newChannel(os);
        ch.write(buffer);
    }

    /**
     * Writes all available data from the input stream to the output stream.
     * @param os The output stream.
     * @param is The input stream
     * @return Actual number of bytes written.
     */
    public static long writeBytes(OutputStream os, InputStream is) throws IOException {
        return writeBytes(os, is, -1L);
    }

    /**
     * Writes all available data from the input stream to the output stream.
     * @param os The output stream.
     * @param is The input stream
     * @param length Max. number of bytes to write. Specify {@code -1L} to ignore length.
     * @return Actual number of bytes written.
     */
    public static long writeBytes(OutputStream os, InputStream is, long length) throws IOException {
        long retVal = 0L;
        byte[] buffer = new byte[8192];

        while (length < 0L || retVal < length) {
            int numBytes = (length < 0) ? buffer.length : (int) Math.min(buffer.length, length - retVal);
            int n = is.read(buffer, 0, numBytes);
            if (n < 0) {
                break;
            }
            os.write(buffer, 0, n);
            retVal += n;
        }

        return retVal;
    }

    /**
     * Writes the specified byte value 'count' times to the output stream.
     * @param os The output stream.
     * @param value The value to write.
     * @param count The number of times to write 'value'.
     */
    public static void writeBytes(OutputStream os, byte value, int count) throws IOException {
        while (count-- > 0) {
            os.write(value);
        }
    }
}

Related

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