Here you can find the source of writeBytes(OutputStream os, ByteBuffer buffer)
Parameter | Description |
---|---|
os | The output stream. |
buffer | The ByteBuffer. |
public static void writeBytes(OutputStream os, ByteBuffer buffer) throws IOException
//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); } } }