From 70af02d9d9cab246f03a699ca36732b79fcf9795 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sat, 19 Jan 2013 22:21:57 -0800 Subject: [PATCH] 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. --- core/src/main/java/org/jclouds/util/Strings2.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/jclouds/util/Strings2.java b/core/src/main/java/org/jclouds/util/Strings2.java index d6f83afdf2..e3352f53aa 100644 --- a/core/src/main/java/org/jclouds/util/Strings2.java +++ b/core/src/main/java/org/jclouds/util/Strings2.java @@ -19,13 +19,13 @@ package org.jclouds.util; 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 org.jclouds.util.Patterns.TOKEN_TO_PATTERN; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; @@ -149,7 +149,7 @@ public class Strings2 { public static String toStringAndClose(InputStream input) throws IOException { checkNotNull(input, "input"); try { - return new String(toByteArray(input), Charsets.UTF_8); + return CharStreams.toString(new InputStreamReader(input, Charsets.UTF_8)); } finally { closeQuietly(input); }