Java InputStream to Byte Array getBytes(InputStream inputStream)

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

Description

get Bytes

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/**//from w  ww  .  ja  v a 2 s . c om
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

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

public class Main {
    public static byte[] getBytes(InputStream inputStream) throws IOException {
        try {
            byte[] buffer = new byte[4096];
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            for (int count = inputStream.read(buffer); count >= 0; count = inputStream.read(buffer)) {
                out.write(buffer, 0, count);
            }
            byte[] bytes = out.toByteArray();
            return bytes;
        } finally {
            close(inputStream);
        }
    }

    public static void close(Closeable thing) {
        if (thing != null) {
            try {
                thing.close();
            } catch (Exception ignored) {
            }
        }
    }
}

Related

  1. getBytes(InputStream input)
  2. getBytes(InputStream input)
  3. getBytes(InputStream inputStream)
  4. getBytes(InputStream inputStream)
  5. getBytes(InputStream inputStream)
  6. getBytes(InputStream is)
  7. getBytes(InputStream is)
  8. getBytes(InputStream is)
  9. getBytes(InputStream is)