Here you can find the source of readAll(InputStream in)
Parameter | Description |
---|---|
in | the stream to read from. |
Parameter | Description |
---|---|
IOException | if there was an error reading from the stream. |
public static byte[] readAll(InputStream in) throws IOException
//package com.java2s; /*//from w w w . ja v a2 s.c om * $Id: Util.java 169 2011-03-03 18:45:00Z ahto.truu $ * * * * Copyright 2008-2011 GuardTime AS * * This file is part of the GuardTime client SDK. * * 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.IOException; import java.io.InputStream; public class Main { /** * The default buffer size for the data read/copy operations in this class. */ public static final int DEFAULT_BUFFER_SIZE = 8192; /** * Reads all data from the given input stream. * * @param in * the stream to read from. * @return the data. * @throws IOException * if there was an error reading from the stream. */ public static byte[] readAll(InputStream in) throws IOException { return readAll(in, DEFAULT_BUFFER_SIZE); } /** * Reads all data from the given input stream using a buffer of the given size. * * @param in * the stream to read from. * @param bufSize * size of the buffer to use. * @return the data. * @throws IOException * if there was an error reading from the stream. */ public static byte[] readAll(InputStream in, int bufSize) throws IOException { if (bufSize < 1) { throw new IllegalArgumentException("Invalid buffer size: " + bufSize); } byte[] res = new byte[0]; byte[] buf = new byte[bufSize]; int bytesRead; while ((bytesRead = in.read(buf)) != -1) { byte[] tmp = new byte[res.length + bytesRead]; System.arraycopy(res, 0, tmp, 0, res.length); System.arraycopy(buf, 0, tmp, res.length, bytesRead); res = tmp; } in.close(); return res; } }