Android InputStream Read readFullyAndClose(InputStream is)

Here you can find the source of readFullyAndClose(InputStream is)

Description

Reads all the bytes from the given input stream.

License

Apache License

Parameter

Parameter Description
is the input stream to be read.

Exception

Parameter Description
IOException if a read error occurs.

Return

the content of the stream as a byte array.

Declaration

public static byte[] readFullyAndClose(InputStream is)
        throws IOException 

Method Source Code

//package com.java2s;
/*/*from  w  w  w  .java  2 s  . c o  m*/
 *  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.IOException;
import java.io.InputStream;

import java.util.Arrays;

public class Main {
    /**
     * Reads all the bytes from the given input stream.
     *
     * Calls read multiple times on the given input stream until it receives an
     * end of file marker. Returns the combined results as a byte array. Note
     * that this method may block if the underlying stream read blocks.
     *
     * @param is
     *            the input stream to be read.
     * @return the content of the stream as a byte array.
     * @throws IOException
     *             if a read error occurs.
     */
    public static byte[] readFullyAndClose(InputStream is)
            throws IOException {

        try {
            // Initial read
            byte[] buffer = new byte[1024];
            int count = is.read(buffer);
            int nextByte = is.read();

            // Did we get it all in one read?
            if (nextByte == -1) {
                return Arrays.copyOf(buffer, count);
            }

            // Requires additional reads
            ByteArrayOutputStream baos = new ByteArrayOutputStream(
                    count * 2);
            baos.write(buffer, 0, count);
            baos.write(nextByte);
            while (true) {
                count = is.read(buffer);
                if (count == -1) {
                    return baos.toByteArray();
                }
                baos.write(buffer, 0, count);
            }
        } finally {
            is.close();
        }
    }
}

Related

  1. readAllBytes(InputStream in)
  2. readAllBytesToOutput(InputStream is, OutputStream output)
  3. readAllNoClose(InputStream is)
  4. readBSInt(InputStream is)
  5. readFully(InputStream inputStream)
  6. readInputStream(InputStream inStream)
  7. readInputStream(InputStream inputStream)
  8. readInputStream(InputStream is)
  9. readInputStreamContent(InputStream inputStream)