一、使用ByteArrayOutputStream的方式
在Java语言中,InputStream是读取数据的基本方式。但是,有时候我们需要将InputStream转化为Byte数组。而使用ByteArrayOutputStream是最常见的方法之一。下面是代码示例:
public byte[] readBytesFromInputStream(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}首先,我们创建了ByteArrayOutputStream对象output,并定义了一个byte类型的buffer数组。接下来,我们使用input.read(buffer)读取input中的数据到buffer中,并通过output.write(buffer, 0, n)将buffer中的数据写入到output中。最后,调用output.toByteArray()将output转换成byte数组。该方法的缺点是需要在内存中保持所有数据。
二、使用ByteArrayInputStream的方式
除了使用ByteArrayOutputStream实现InputStream转换成byte数组,还可以使用ByteArrayInputStream。下面是代码示例:
public byte[] readBytesFromInputStream(InputStream input) {
try {
return org.apache.commons.io.IOUtils.toByteArray(input);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}在这个方法中,我们使用了Apache Commons IO库,调用org.apache.commons.io.IOUtils的toByteArray方法将input转换为byte数组。这种方法不需要担心内存限制,因为只有需要数据时才会从input对象中读取。
三、使用BufferedInputStream的方式
Java标准库中,还提供了BufferedInputStream,可以实现输入流的缓存。下面是代码示例:
public byte[] readBytesFromInputStream(InputStream input) {
try {
BufferedInputStream bufferedInput = new BufferedInputStream(input);
byte[] result = new byte[bufferedInput.available()];
bufferedInput.read(result, 0, result.length);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}在这个方法中,我们首先创建了BufferedInputStream对象bufferedInput,然后调用available()获取输入流的字节数,创建相应大小的byte数组result。最后,调用bufferedInput.read(result, 0, result.length)从输入流中读取数据。
四、InputStream转ByteArray的性能比较
下面的代码演示了三个方法的性能比较:
public static void main(String[] args) {
int count = 100;
long start1 = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
InputStream input = new ByteArrayInputStream("Hello World!".getBytes());
readBytesFromInputStream(input);
}
long end1 = System.currentTimeMillis();
long start2 = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
InputStream input = new ByteArrayInputStream("Hello World!".getBytes());
readBytesFromInputStream2(input);
}
long end2 = System.currentTimeMillis();
long start3 = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
InputStream input = new ByteArrayInputStream("Hello World!".getBytes());
readBytesFromInputStream3(input);
}
long end3 = System.currentTimeMillis();
System.out.println("ByteArrayOutputStream 方法耗时:" + (end1 - start1) + " ms");
System.out.println("IOUtils.toByteArray 方法耗时:" + (end2 - start2) + " ms");
System.out.println("BufferedInputStream 方法耗时:" + (end3 - start3) + " ms");
}
private static byte[] readBytesFromInputStream(InputStream input) {
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static byte[] readBytesFromInputStream2(InputStream input) {
try {
return org.apache.commons.io.IOUtils.toByteArray(input);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static byte[] readBytesFromInputStream3(InputStream input) {
try {
BufferedInputStream bufferedInput = new BufferedInputStream(input);
byte[] result = new byte[bufferedInput.available()];
bufferedInput.read(result, 0, result.length);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
可以看到,使用ByteArrayOutputStream的方式是最慢的,使用BufferedInputStream的方式较快,而使用IOUtils.toByteArray方法的方式最快。在实际情况中,需要根据自己的需求选择合适的方法。
五、总结
本文介绍了三种将InputStream转化为Byte数组的方式,分别是使用ByteArrayOutputStream、ByteArrayInputStream以及BufferedInputStream。此外,我们还对三种方法进行了性能比较。在实际使用中需要根据自己的需求选择最适合的方法。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/186283.html
微信扫一扫
支付宝扫一扫