Java InputStream to Byte Array getBytesFromInputStream(InputStream inputStream)

Here you can find the source of getBytesFromInputStream(InputStream inputStream)

Description

Get an array of bytes from an input stream.

License

Open Source License

Parameter

Parameter Description
inputStream Input stream.

Exception

Parameter Description
IOException Throws if an error occurred.

Return

Returns an array of bytes.

Declaration

public static byte[] getBytesFromInputStream(InputStream inputStream) throws IOException 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) Tim Visee 2016. All rights reserved.                         *
 *                                                                            *
 * @author Tim Visee                                                          *
 * @website http://timvisee.com/                                              *
 *                                                                            *
 * Open Source != No Copyright                                                *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included    *
 * in all copies or substantial portions of the Software.                     *
 *                                                                            *
 * You should have received a copy of The MIT License (MIT) along with this   *
 * program. If not, see <http://opensource.org/licenses/MIT/>.                *
 ******************************************************************************/

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**//from  w w  w .  j  a v a  2  s  .  c o  m
     * Get an array of bytes from an input stream.
     *
     * @param inputStream Input stream.
     *
     * @return Returns an array of bytes.
     *
     * @throws IOException Throws if an error occurred.
     */
    public static byte[] getBytesFromInputStream(InputStream inputStream) throws IOException {
        // Try to get the input bytes
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            // Create a new buffer
            byte[] buffer = new byte[0xFFFF];

            // Load the bytes from the input stream
            for (int len; (len = inputStream.read(buffer)) != -1;)
                os.write(buffer, 0, len);

            // Flush
            os.flush();

            // Convert the byte array output stream to a byte array
            return os.toByteArray();
        }
    }
}

Related

  1. getBytes(InputStream is, int max_len)
  2. getBytes(InputStream stream)
  3. getBytes(InputStream stream)
  4. getBytesFromInputStream(InputStream inputStream)
  5. getBytesFromInputStream(InputStream inputStream)
  6. getBytesFromInputStream(InputStream is)
  7. getBytesFromInputStream(InputStream is)
  8. getBytesFromStream(InputStream input)
  9. getBytesFromStream(InputStream inputStream)