Skip to content

Commit

Permalink
Use cache streams to copy over byes, not the byte array
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed May 23, 2011
1 parent e630605 commit 0de0df9
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.elasticsearch.common.io;

import org.elasticsearch.common.Preconditions;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.CachedStreamOutput;

import java.io.*;

Expand Down Expand Up @@ -160,9 +162,14 @@ public static void copy(byte[] in, OutputStream out) throws IOException {
* @throws IOException in case of I/O errors
*/
public static byte[] copyToByteArray(InputStream in) throws IOException {
FastByteArrayOutputStream out = FastByteArrayOutputStream.Cached.cached();
copy(in, out);
return out.copiedByteArray();
CachedStreamOutput.Entry cachedEntry = CachedStreamOutput.popEntry();
try {
BytesStreamOutput out = cachedEntry.cachedBytes();
copy(in, out);
return out.copiedByteArray();
} finally {
CachedStreamOutput.pushEntry(cachedEntry);
}
}


Expand Down

0 comments on commit 0de0df9

Please sign in to comment.