Use cache streams to copy over byes, not the byte array

This commit is contained in:
kimchy 2011-05-24 01:02:43 +03:00
parent 123b21f4ae
commit 7ff07ebaf1
1 changed files with 10 additions and 3 deletions

View File

@ -20,6 +20,8 @@
package org.elasticsearch.common.io; package org.elasticsearch.common.io;
import org.elasticsearch.common.Preconditions; import org.elasticsearch.common.Preconditions;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.CachedStreamOutput;
import java.io.*; import java.io.*;
@ -160,9 +162,14 @@ public abstract class Streams {
* @throws IOException in case of I/O errors * @throws IOException in case of I/O errors
*/ */
public static byte[] copyToByteArray(InputStream in) throws IOException { public static byte[] copyToByteArray(InputStream in) throws IOException {
FastByteArrayOutputStream out = FastByteArrayOutputStream.Cached.cached(); CachedStreamOutput.Entry cachedEntry = CachedStreamOutput.popEntry();
try {
BytesStreamOutput out = cachedEntry.cachedBytes();
copy(in, out); copy(in, out);
return out.copiedByteArray(); return out.copiedByteArray();
} finally {
CachedStreamOutput.pushEntry(cachedEntry);
}
} }