Description
Read all data from input stream
License
Open Source License
Parameter
Parameter | Description |
---|
is | InputStream where to read |
Exception
Parameter | Description |
---|
IOException | if reading fails with IOException |
Return
Read data as a byte array
Declaration
public static byte[] readAll(final InputStream is) throws IOException
Method Source Code
//package com.java2s;
/**// w w w. j a va2 s .c o m
*
* Open Realm of Stars Game Project
* Copyright (C) 2016 Tuomo Untinen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/licenses/
*
*
* Generic IO Utilities
*
*/
import java.io.IOException;
import java.io.InputStream;
public class Main {
/**
* Read all data from input stream
* @param is InputStream where to read
* @return Read data as a byte array
* @throws IOException if reading fails with IOException
*/
public static byte[] readAll(final InputStream is) throws IOException {
int dataLength = 0;
int byteOffset = 0;
int blockSize = 8000;
byte[] buffer = null;
do {
byte[] tmpBuf = new byte[blockSize];
dataLength = is.read(tmpBuf, 0, tmpBuf.length);
if (dataLength != -1) {
byte[] targetBuf;
if (buffer == null) {
targetBuf = new byte[dataLength];
byteOffset = 0;
} else {
targetBuf = new byte[buffer.length + dataLength];
System.arraycopy(buffer, 0, targetBuf, 0, buffer.length);
byteOffset = buffer.length;
}
System.arraycopy(tmpBuf, 0, targetBuf, byteOffset, dataLength);
buffer = targetBuf;
}
} while (dataLength != -1);
return buffer;
}
}
Related
- readAll(InputStream i, byte b[])
- readAll(InputStream in)
- readall(InputStream in)
- readAll(InputStream in)