Skip to content

Commit

Permalink
Small refactoring before working on AHC-99
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarcand committed Jun 8, 2011
1 parent aabe6e7 commit 3ac8e24
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.ArrayList;
Expand Down Expand Up @@ -82,19 +81,10 @@ public String getResponseBody(String charset) throws IOException {
charset = DEFAULT_CHARSET;
}

return contentToString(charset);
}

String contentToString(String charset) throws UnsupportedEncodingException {
StringBuilder b = new StringBuilder();
for (HttpResponseBodyPart bp : bodyParts) {
b.append(new String(bp.getBodyPartBytes(), charset));
}
return b.toString();
return AsyncHttpProviderUtils.contentToString(bodyParts, charset);
}

/* @Override */

public InputStream getResponseBodyAsStream() throws IOException {
if (bodyParts.size() > 0) {
return new ByteArrayInputStream(bodyParts.toArray(new HttpResponseBodyPart[bodyParts.size()])[0].getBodyPartBytes());
Expand All @@ -121,7 +111,7 @@ public String getResponseBodyExcerpt(int maxLength, String charset) throws IOExc
charset = DEFAULT_CHARSET;
}

String response = contentToString(charset);
String response = AsyncHttpProviderUtils.contentToString(bodyParts, charset);
return response.length() <= maxLength ? response : response.substring(0, maxLength);
}

Expand Down
15 changes: 2 additions & 13 deletions src/main/java/com/ning/http/client/providers/jdk/JDKResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.ArrayList;
Expand Down Expand Up @@ -86,22 +85,12 @@ public String getResponseBody(String charset) throws IOException {
}

if (!contentComputed.get()) {
contentToString(charset);
content = AsyncHttpProviderUtils.contentToString(bodyParts, charset);
}
return content;
}

String contentToString(String charset) throws UnsupportedEncodingException {
StringBuilder b = new StringBuilder();
for (HttpResponseBodyPart bp : bodyParts) {
b.append(new String(bp.getBodyPartBytes(), charset));
}
content = b.toString();
return content;
}

/* @Override */

public InputStream getResponseBodyAsStream() throws IOException {
if (contentComputed.get()) {
return new ByteArrayInputStream(content.getBytes(DEFAULT_CHARSET));
Expand Down Expand Up @@ -175,7 +164,7 @@ public String getResponseBodyExcerpt(int maxLength, String charset) throws IOExc
}

if (!contentComputed.get()) {
contentToString(charset == null ? DEFAULT_CHARSET : charset);
content = AsyncHttpProviderUtils.contentToString(bodyParts, charset == null ? DEFAULT_CHARSET : charset);
}

return content.length() <= maxLength ? content : content.substring(0, maxLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.ArrayList;
Expand Down Expand Up @@ -88,20 +87,10 @@ public String getResponseBody(String charset) throws IOException {
charset = DEFAULT_CHARSET;
}

return contentToString(charset);
}


String contentToString(String charset) throws UnsupportedEncodingException {
StringBuilder b = new StringBuilder();
for (HttpResponseBodyPart bp : bodyParts) {
b.append(new String(bp.getBodyPartBytes(), charset));
}
return b.toString();
return AsyncHttpProviderUtils.contentToString(bodyParts, charset);
}

/* @Override */

public InputStream getResponseBodyAsStream() throws IOException {
ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
for (HttpResponseBodyPart bp : bodyParts) {
Expand Down Expand Up @@ -132,7 +121,7 @@ public String getResponseBodyExcerpt(int maxLength, String charset) throws IOExc
charset = DEFAULT_CHARSET;
}

String response = contentToString(charset);
String response = AsyncHttpProviderUtils.contentToString(bodyParts, charset);
return response.length() <= maxLength ? response : response.substring(0, maxLength);
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -175,6 +176,13 @@ public final static String getAuthority(URI uri) {
return url;
}

public final static String contentToString(Collection<HttpResponseBodyPart> bodyParts, String charset) throws UnsupportedEncodingException {
StringBuilder b = new StringBuilder();
for (HttpResponseBodyPart bp : bodyParts) {
b.append(new String(bp.getBodyPartBytes(), charset));
}
return b.toString();
}

public final static URI getRedirectUri(URI uri, String location) {
URI newUri = uri.resolve(location);
Expand Down

0 comments on commit 3ac8e24

Please sign in to comment.