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

sis*_*onb 12 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();
}
Run Code Online (Sandbox Code Playgroud)

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

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

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"));
Run Code Online (Sandbox Code Playgroud)

编辑 - 基准测试代码

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"));
Run Code Online (Sandbox Code Playgroud)

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"));
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

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

JB *_*zet 13

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

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


bma*_*ies 5

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

  • 嘿,我不想只为一个函数导入一个库。 (3认同)