将InputStream转换为byte []的最有效方法?

时间:2013-04-22 21:22:40

标签: java inputstream

ChannelBufferInputStream responseStream = (ChannelBufferInputStream) response.getBodyAsStream();
ArrayList<Byte> arrayList = new ArrayList<Byte>();
try {
    while (responseStream.available() > 0) {
        arrayList.add(responseStream.readByte());
    }
} catch (IOException e) {
    e.printStackTrace();
    return internalServerError();
}
Iterator<Byte> iterator = arrayList.iterator();
byte[] bytes = new byte[arrayList.size()];
int i = 0;
while (iterator.hasNext()) {
    bytes[i++] = iterator.next();
}

在我的网络应用的每个页面加载时调用此代码。它似乎运行得非常快,但有什么能让它运行得更快吗?

编辑 - 使用字节数组输出流更新

ChannelBufferInputStream responseStream = (ChannelBufferInputStream) response.getBodyAsStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
    int read = responseStream.read();
    while (read != -1) {
        byteArrayOutputStream.write(read);
        read = responseStream.read();
    }
} catch (IOException e) {
    e.printStackTrace();
    return internalServerError();
}
byte[] bytes = byteArrayOutputStream.toByteArray();
return ok(bytes).as(response.getHeader("Content-type"));

编辑 - 基准测试代码

ChannelBufferInputStream responseStream = (ChannelBufferInputStream) response.getBodyAsStream();
long t1 = System.nanoTime();

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
    int read = responseStream.read();
    while (read != -1) {
        byteArrayOutputStream.write(read);
        read = responseStream.read();
    }
} catch (IOException e) {
    e.printStackTrace();
    return internalServerError();
}
byte[] bytes = byteArrayOutputStream.toByteArray();

long t2 = System.nanoTime();
System.out.println(t2-t1);
return ok(bytes).as(response.getHeader("Content-type"));

100+请求后的平均时间 - 46873

ChannelBufferInputStream responseStream = (ChannelBufferInputStream) response.getBodyAsStream();
long t1 = System.nanoTime();

ArrayList<Byte> arrayList = new ArrayList<Byte>();
try {
    while (responseStream.available() > 0) {
        arrayList.add(responseStream.readByte());
    }
} catch (IOException e) {
    e.printStackTrace();
    return internalServerError();
}
Iterator<Byte> iterator = arrayList.iterator();
byte[] bytes = new byte[arrayList.size()];
int i = 0;
while (iterator.hasNext()) {
    bytes[i++] = iterator.next();
}

long t2 = System.nanoTime();
System.out.println(t2-t1);
return ok(bytes).as(response.getHeader("Content-type"));

100+请求后的平均时间 - 522848

long t1 = System.nanoTime();
byte[] bytes;
try {
    bytes = org.apache.commons.io.IOUtils.toByteArray(responseStream);
} catch (Exception e) {
    return internalServerError();
}

long t2 = System.nanoTime();
System.out.println(t2-t1);

100+请求后的平均时间 - 45088

long t1 = System.nanoTime();
byte[] bytes;
try {
    bytes = sun.misc.IOUtils.readFully(responseStream, -1, true);
} catch (Exception e) {
    return internalServerError();
}

long t2 = System.nanoTime();
System.out.println(t2 - t1);

100+请求后的平均时间 - 20180

3 个答案:

答案 0 :(得分:13)

是。使用ByteArrayOutputStream而不是ArrayList。然后从InputStream中读取块的块(不使用available(),这几乎总是从不使用)并将这些块写入ByteArrayOutputStream,直到read()方法返回-1。然后在ByteArrayOutputStream上调用toByteArray()。

您可以使用Guava的ByteStreams.toByteArray()方法,它可以为您完成所有这些,或者您可以阅读其源代码,以便更好地了解它是如何做到的。阅读IO tutorial也可能有所帮助。

答案 1 :(得分:4)

Apache Commons IO IOUtils.toByteArray方法出了什么问题?为此目的,这已经过多年优化。

答案 2 :(得分:1)

为什么呢?此代码完全等同于read(byte[]),只是它对整个数据执行了两个额外的复制步骤。你不需要这些。一个简单的read(byte[])会快几倍。

使用available()也是无效的。您需要整个响应,而不仅仅是可以无阻塞地读取的部分。你需要循环。