Here you can find the source of writeFully(ByteBuffer buffer, OutputStream os)
Parameter | Description |
---|---|
buffer | The ByteBuffer that will be read completely. |
os | The OutputStream that will be written to. |
Parameter | Description |
---|---|
IOException | If any error occurs while writing to the OutputStream. |
IllegalArgumentException | When the ByteBuffer is not backed by an array. |
public static void writeFully(ByteBuffer buffer, OutputStream os) throws IOException
//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()); } }