Space optimize Strings2.toStringAndClose

Avoid creating a byte array before creating a String.  This reduces
peak memory usage when parsing large JSON responses such as listing a
container.
This commit is contained in:
Andrew Gaul 2013-01-19 22:21:57 -08:00
parent 78d1bad411
commit 70af02d9d9
1 changed files with 2 additions and 2 deletions

View File

@ -19,13 +19,13 @@
package org.jclouds.util; package org.jclouds.util;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.io.Closeables.closeQuietly; import static com.google.common.io.Closeables.closeQuietly;
import static org.jclouds.util.Patterns.TOKEN_TO_PATTERN; import static org.jclouds.util.Patterns.TOKEN_TO_PATTERN;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -149,7 +149,7 @@ public class Strings2 {
public static String toStringAndClose(InputStream input) throws IOException { public static String toStringAndClose(InputStream input) throws IOException {
checkNotNull(input, "input"); checkNotNull(input, "input");
try { try {
return new String(toByteArray(input), Charsets.UTF_8); return CharStreams.toString(new InputStreamReader(input, Charsets.UTF_8));
} finally { } finally {
closeQuietly(input); closeQuietly(input);
} }