import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class StreamIUtils {
public static byte[] readStream2Bytes(InputStream inputStream) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
copyStream(inputStream, byteArrayOutputStream, 102400);
return byteArrayOutputStream.toByteArray();
}
public static int copyStream(InputStream inputStream, OutputStream outputStream, long byteCount) throws Exception {
if (inputStream == null || outputStream == null) {
throw new IOException("流为空");
}
byte[] buffer = new byte[512];
int totalSize = 0;
int count;
try (InputStream input = inputStream; OutputStream output = outputStream) {
while ((count = input.read(buffer, 0, buffer.length)) != -1) {
totalSize += count;
if (totalSize > byteCount) {
throw new IOException("超过了最大字节数");
}
output.write(buffer, 0, count);
}
}
return totalSize;
}
}