Skip to content

Commit b203f2b

Browse files
committed
Get rid of Response.getResponseBodyExcerpt, close AsyncHttpClient#908
1 parent e556223 commit b203f2b

File tree

7 files changed

+1
-123
lines changed

7 files changed

+1
-123
lines changed

api/src/main/java/org/asynchttpclient/Response.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,6 @@ public interface Response {
6969
*/
7070
InputStream getResponseBodyAsStream() throws IOException;
7171

72-
/**
73-
* Returns the first maxLength bytes of the response body as a string. Note that this does not check whether the content type is actually a textual one, but it will use the
74-
* charset if present in the content type header.
75-
*
76-
* @param maxLength
77-
* The maximum number of bytes to read
78-
* @param charset
79-
* the charset to use when decoding the stream
80-
* @return The response body
81-
* @throws java.io.IOException
82-
*/
83-
String getResponseBodyExcerpt(int maxLength, Charset charset) throws IOException;
84-
8572
/**
8673
* Return the entire response body as a String.
8774
*
@@ -92,17 +79,6 @@ public interface Response {
9279
*/
9380
String getResponseBody(Charset charset) throws IOException;
9481

95-
/**
96-
* Returns the first maxLength bytes of the response body as a string. Note that this does not check whether the content type is actually a textual one, but it will use the
97-
* charset if present in the content type header.
98-
*
99-
* @param maxLength
100-
* The maximum number of bytes to read
101-
* @return The response body
102-
* @throws java.io.IOException
103-
*/
104-
String getResponseBodyExcerpt(int maxLength) throws IOException;
105-
10682
/**
10783
* Return the entire response body as a String.
10884
*

api/src/main/java/org/asynchttpclient/util/AsyncHttpProviderUtils.java

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616
import static org.asynchttpclient.util.MiscUtils.*;
1717

1818
import java.io.IOException;
19-
import java.io.UnsupportedEncodingException;
2019
import java.nio.ByteBuffer;
2120
import java.nio.charset.Charset;
2221
import java.util.List;
2322

2423
import org.asynchttpclient.AsyncHttpClientConfig;
25-
import org.asynchttpclient.HttpResponseBodyPart;
2624
import org.asynchttpclient.Param;
2725
import org.asynchttpclient.Request;
2826
import org.asynchttpclient.uri.Uri;
@@ -35,8 +33,6 @@ public class AsyncHttpProviderUtils {
3533
public static final IOException REMOTELY_CLOSED_EXCEPTION = buildStaticIOException("Remotely closed");
3634
public static final IOException CHANNEL_CLOSED_EXCEPTION = buildStaticIOException("Channel closed");
3735

38-
private final static byte[] NO_BYTES = new byte[0];
39-
4036
public final static Charset DEFAULT_CHARSET = ISO_8859_1;
4137

4238
public static final void validateSupportedScheme(Uri uri) {
@@ -48,45 +44,6 @@ public static final void validateSupportedScheme(Uri uri) {
4844
}
4945
}
5046

51-
/**
52-
* @param bodyParts NON EMPTY body part
53-
* @param maxLen
54-
* @return
55-
* @throws UnsupportedEncodingException
56-
*/
57-
public final static byte[] contentToBytes(List<HttpResponseBodyPart> bodyParts, int maxLen) throws UnsupportedEncodingException {
58-
final int partCount = bodyParts.size();
59-
if (partCount == 0) {
60-
return NO_BYTES;
61-
}
62-
if (partCount == 1) {
63-
byte[] chunk = bodyParts.get(0).getBodyPartBytes();
64-
if (chunk.length <= maxLen) {
65-
return chunk;
66-
}
67-
byte[] result = new byte[maxLen];
68-
System.arraycopy(chunk, 0, result, 0, maxLen);
69-
return result;
70-
}
71-
int size = 0;
72-
byte[] result = new byte[maxLen];
73-
for (HttpResponseBodyPart part : bodyParts) {
74-
byte[] chunk = part.getBodyPartBytes();
75-
int amount = Math.min(maxLen - size, chunk.length);
76-
System.arraycopy(chunk, 0, result, size, amount);
77-
size += amount;
78-
if (size == maxLen) {
79-
return result;
80-
}
81-
}
82-
if (size < maxLen) {
83-
byte[] old = result;
84-
result = new byte[old.length];
85-
System.arraycopy(old, 0, result, 0, old.length);
86-
}
87-
return result;
88-
}
89-
9047
public final static String getBaseUrl(Uri uri) {
9148
return uri.getScheme() + "://" + getAuthority(uri);
9249
}

api/src/main/java/org/asynchttpclient/webdav/WebDavCompletionHandlerBase.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,11 @@ public InputStream getResponseBodyAsStream() throws IOException {
160160
return wrappedResponse.getResponseBodyAsStream();
161161
}
162162

163-
@Override
164-
public String getResponseBodyExcerpt(int maxLength, Charset charset) throws IOException {
165-
return wrappedResponse.getResponseBodyExcerpt(maxLength, charset);
166-
}
167-
168163
@Override
169164
public String getResponseBody(Charset charset) throws IOException {
170165
return wrappedResponse.getResponseBody(charset);
171166
}
172167

173-
@Override
174-
public String getResponseBodyExcerpt(int maxLength) throws IOException {
175-
return wrappedResponse.getResponseBodyExcerpt(maxLength);
176-
}
177-
178168
@Override
179169
public String getResponseBody() throws IOException {
180170
return wrappedResponse.getResponseBody();

api/src/main/java/org/asynchttpclient/webdav/WebDavResponse.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ public InputStream getResponseBodyAsStream() throws IOException {
5959
return response.getResponseBodyAsStream();
6060
}
6161

62-
public String getResponseBodyExcerpt(int maxLength) throws IOException {
63-
return response.getResponseBodyExcerpt(maxLength);
64-
}
65-
66-
public String getResponseBodyExcerpt(int maxLength, Charset charset) throws IOException {
67-
return response.getResponseBodyExcerpt(maxLength, charset);
68-
}
69-
7062
public String getResponseBody() throws IOException {
7163
return response.getResponseBody();
7264
}

api/src/test/java/org/asynchttpclient/AsyncProvidersBasicTest.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ public Response onCompleted(Response response) throws Exception {
605605
String xContentType = response.getHeader("X-Content-Type");
606606
String boundary = xContentType.substring((xContentType.indexOf("boundary") + "boundary".length() + 1));
607607

608-
String s = response.getResponseBodyExcerpt(boundary.length() + "--".length()).substring("--".length());
608+
String s = response.getResponseBody().substring(boundary.length());
609609
assertEquals(boundary, s);
610610
} finally {
611611
l.countDown();
@@ -1008,21 +1008,6 @@ public void onThrowable(Throwable t) {
10081008
}
10091009
}
10101010

1011-
@Test(groups = { "standalone", "default_provider", "async" })
1012-
public void asyncResponseBodyTooLarge() throws Exception {
1013-
try (AsyncHttpClient client = getAsyncHttpClient(null)) {
1014-
Response response = client.preparePost(getTargetUrl()).setBody("0123456789").execute(new AsyncCompletionHandlerAdapter() {
1015-
1016-
@Override
1017-
public void onThrowable(Throwable t) {
1018-
fail("Unexpected exception", t);
1019-
}
1020-
}).get();
1021-
1022-
assertNotNull(response.getResponseBodyExcerpt(Integer.MAX_VALUE));
1023-
}
1024-
}
1025-
10261011
@Test(groups = { "standalone", "default_provider", "async" })
10271012
public void asyncResponseEmptyBody() throws Exception {
10281013
try (AsyncHttpClient client = getAsyncHttpClient(null)) {

providers/netty3/src/main/java/org/asynchttpclient/netty/NettyResponse.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,6 @@ public ChannelBuffer getResponseBodyAsChannelBuffer() throws IOException {
8787
return b;
8888
}
8989

90-
@Override
91-
public String getResponseBodyExcerpt(int maxLength) throws IOException {
92-
return getResponseBodyExcerpt(maxLength, null);
93-
}
94-
95-
public String getResponseBodyExcerpt(int maxLength, Charset charset) throws IOException {
96-
String response = getResponseBody(charset);
97-
return response.length() <= maxLength ? response : response.substring(0, maxLength);
98-
}
99-
10090
@Override
10191
protected List<Cookie> buildCookies() {
10292
List<Cookie> cookies = new ArrayList<>();

providers/netty4/src/main/java/org/asynchttpclient/netty/NettyResponse.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*/
1414
package org.asynchttpclient.netty;
1515

16-
import static org.asynchttpclient.util.AsyncHttpProviderUtils.contentToBytes;
1716
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
1817
import io.netty.handler.codec.http.HttpHeaders;
1918

@@ -44,17 +43,6 @@ public NettyResponse(HttpResponseStatus status,//
4443
super(status, headers, bodyParts);
4544
}
4645

47-
@Override
48-
public String getResponseBodyExcerpt(int maxLength) throws IOException {
49-
return getResponseBodyExcerpt(maxLength, null);
50-
}
51-
52-
public String getResponseBodyExcerpt(int maxLength, Charset charset) throws IOException {
53-
byte[] b = contentToBytes(bodyParts, maxLength);
54-
// should be fine; except that it may split multi-byte chars (last char may become '?')
55-
return new String(b, calculateCharset(charset));
56-
}
57-
5846
protected List<Cookie> buildCookies() {
5947

6048
List<String> setCookieHeaders = headers.getHeaders().get(HttpHeaders.Names.SET_COOKIE2);

0 commit comments

Comments
 (0)