Java ByteArrayOutputStream Write readAll(InputStream in)

Here you can find the source of readAll(InputStream in)

Description

read All

License

Apache License

Declaration

public static byte[] readAll(InputStream in) throws IOException 

Method Source Code

//package com.java2s;
/**/*from w  w w .j  a  v a2s  .c  o m*/
 *   Copyright 2011 Garrick Toubassi
 *
 *   Licensed 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;

public class Main {
    public static int readAll(InputStream in, byte[] buf) throws IOException {
        return readBytes(in, buf, buf.length);
    }

    public static byte[] readAll(InputStream in) throws IOException {
        byte[] buf = new byte[8192];
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        int numRead = 0;
        while ((numRead = in.read(buf)) > -1) {
            bytesOut.write(buf, 0, numRead);
        }
        return bytesOut.toByteArray();
    }

    public static int readBytes(InputStream in, byte[] buf, int length) throws IOException {
        int totalRead = 0;
        int numRead = 0;
        while ((numRead = in.read(buf, totalRead, length - totalRead)) > -1 && totalRead < length) {
            totalRead += numRead;
        }
        return totalRead;
    }
}

Related

  1. readAll(File file)
  2. readAll(InputStream aInputStream)
  3. readAll(InputStream bytes)
  4. readAll(InputStream bytes, int bufferSize)
  5. readAll(InputStream in)
  6. readAll(InputStream in)
  7. readAll(InputStream in)
  8. readAll(InputStream in)
  9. readAll(InputStream in)