Java OutputStream Write Byte Array writeBytesToStream(byte[] bytes, OutputStream os, boolean printStackTraceOnError)

Here you can find the source of writeBytesToStream(byte[] bytes, OutputStream os, boolean printStackTraceOnError)

Description

Write the contents of a byte array to an OutputStream without needing to worry about exceptions.

License

Apache License

Parameter

Parameter Description
bytes the byte array to be written to the stream.
os the OutputStream to write the byte array to.
printStackTraceOnError specifies whether or not a stack trace is to be printed if an i/o error occurs.

Return

true if it worked, false otherwise.

Declaration


@SuppressWarnings({ "BooleanMethodNameMustStartWithQuestion" })
public static boolean writeBytesToStream(byte[] bytes, OutputStream os,
        boolean printStackTraceOnError) 

Method Source Code

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

import java.io.*;

public class Main {
    /**/* w w  w . j  a  va2s. c  om*/
     * Write the contents of a byte array to an {@link OutputStream} without needing to worry about exceptions.
     *
     * @param bytes                  the byte array to be written to the stream.
     * @param os                     the {@link OutputStream} to write the byte array to.
     * @param printStackTraceOnError specifies whether or not a stack trace is to be printed if an i/o error occurs.
     * @return true if it worked, false otherwise.
     */

    @SuppressWarnings({ "BooleanMethodNameMustStartWithQuestion" })
    public static boolean writeBytesToStream(byte[] bytes, OutputStream os,
            boolean printStackTraceOnError) {

        try {

            os.write(bytes);

            return true;

        } catch (IOException e) {

            if (printStackTraceOnError) {

                //noinspection CallToPrintStackTrace
                e.printStackTrace();

            }

            return false;

        }

    }
}

Related

  1. writeBytes(OutputStream os, byte[] b)
  2. writeBytes(OutputStream os, byte[] b)
  3. writeBytes(OutputStream out, byte[] data)
  4. writeBytes(OutputStream output, Object value)
  5. writeBytes(OutputStream outputStream, byte[] data)
  6. writeBytesToStream(byte[] bytes, OutputStream outputStream)
  7. writeBytesToStream(byte[] data, OutputStream os)