Java ByteBuffer Put writeTo(ByteBuffer buffer, OutputStream out)

Here you can find the source of writeTo(ByteBuffer buffer, OutputStream out)

Description

write To

License

Open Source License

Declaration

public static void writeTo(ByteBuffer buffer, OutputStream out) throws IOException 

Method Source Code

//package com.java2s;
//  are made available under the terms of the Eclipse Public License v1.0

import java.io.IOException;

import java.io.OutputStream;

import java.nio.ByteBuffer;

public class Main {
    static final int TEMP_BUFFER_SIZE = 4096;

    public static void writeTo(ByteBuffer buffer, OutputStream out) throws IOException {
        if (buffer.hasArray())
            out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
        else {//from w  w  w.j  av a  2 s .co  m
            byte[] bytes = new byte[TEMP_BUFFER_SIZE];
            while (buffer.hasRemaining()) {
                int byteCountToWrite = Math.min(buffer.remaining(), TEMP_BUFFER_SIZE);
                buffer.get(bytes, 0, byteCountToWrite);
                out.write(bytes, 0, byteCountToWrite);
            }
        }
    }
}

Related

  1. writeFully(OutputStream out, ByteBuffer buf)
  2. writeOutputStream(OutputStream output, ByteBuffer data)
  3. writeOutputStreamWriter(String str, int offset, int count, OutputStream out, ByteBuffer bytes, CharsetEncoder encoder, Object lock)
  4. writePackedLong(ByteBuffer output, long value)
  5. writeShortByteArray(ByteBuffer name, DataOutput out)
  6. writeVarInt(ByteBuffer buffer, int input)