Skip to content

Commit 55ece6d

Browse files
committed
Summary of changes:
- Slight modification of grizzly response refactoring. - return netty tests to running status. That said, build still fails due to clirr.
1 parent cda8924 commit 55ece6d

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

src/main/java/com/ning/http/client/providers/ResponseBase.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.io.InputStream;
55
import java.net.URI;
6+
import java.nio.charset.Charset;
67
import java.util.List;
78

89
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
@@ -102,4 +103,17 @@ public InputStream getResponseBodyAsStream() throws IOException {
102103
return AsyncHttpProviderUtils.contentAsStream(bodyParts);
103104
}
104105

106+
protected String calculateCharset() {
107+
String charset = null;
108+
String contentType = getContentType();
109+
if (contentType != null) {
110+
charset = AsyncHttpProviderUtils.parseCharset(contentType);
111+
}
112+
113+
if (charset == null) {
114+
charset = DEFAULT_CHARSET;
115+
}
116+
return charset;
117+
}
118+
105119
}

src/main/java/com/ning/http/client/providers/grizzly/GrizzlyResponse.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ public InputStream getResponseBodyAsStream() throws IOException {
9292
* {@inheritDoc}
9393
*/
9494
public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
95-
95+
if (charset == null) {
96+
charset = calculateCharset();
97+
}
9698
final int len = Math.min(responseBody.remaining(), maxLength);
9799
final int pos = responseBody.position();
98100
return responseBody.toStringContent(getCharset(charset), pos, len + pos);
@@ -110,12 +112,24 @@ public String getResponseBody(String charset) throws IOException {
110112
}
111113

112114

115+
/**
116+
* {@inheritDoc}
117+
*/
118+
@Override
119+
public byte[] getResponseBodyAsBytes() throws IOException {
120+
final byte[] responseBodyBytes = new byte[responseBody.remaining()];
121+
final int origPos = responseBody.position();
122+
responseBody.get(responseBodyBytes);
123+
responseBody.position(origPos);
124+
return responseBodyBytes;
125+
}
126+
127+
113128
/**
114129
* {@inheritDoc}
115130
*/
116131
public String getResponseBodyExcerpt(int maxLength) throws IOException {
117132

118-
// TODO FIX NULL
119133
return getResponseBodyExcerpt(maxLength, null);
120134

121135
}

src/main/java/com/ning/http/client/providers/grizzly/GrizzlyResponseBodyPart.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.glassfish.grizzly.Buffer;
2020
import org.glassfish.grizzly.Connection;
2121
import org.glassfish.grizzly.http.HttpContent;
22+
import org.glassfish.grizzly.utils.BufferInputStream;
2223

2324
import java.io.ByteArrayInputStream;
2425
import java.io.IOException;
@@ -75,7 +76,6 @@ public byte[] getBodyPartBytes() {
7576
final int origPos = b.position();
7677
bytes = new byte[b.remaining()];
7778
b.get(bytes);
78-
b.flip();
7979
b.position(origPos);
8080
contentBytes.compareAndSet(null, bytes);
8181
return bytes;
@@ -84,13 +84,12 @@ public byte[] getBodyPartBytes() {
8484

8585
@Override
8686
public InputStream readBodyPartBytes() {
87-
return new ByteArrayInputStream(getBodyPartBytes());
87+
return new BufferInputStream(content.getContent());
8888
}
8989

9090
@Override
9191
public int length() {
92-
// this is ugly but...
93-
return getBodyPartBytes().length;
92+
return content.getContent().remaining();
9493
}
9594

9695
/**
@@ -100,7 +99,7 @@ public int length() {
10099
public int writeTo(OutputStream outputStream) throws IOException {
101100

102101
final byte[] bytes = getBodyPartBytes();
103-
outputStream.write(getBodyPartBytes());
102+
outputStream.write(bytes);
104103
return bytes.length;
105104

106105
}
@@ -112,7 +111,7 @@ public int writeTo(OutputStream outputStream) throws IOException {
112111
@Override
113112
public ByteBuffer getBodyByteBuffer() {
114113

115-
return ByteBuffer.wrap(getBodyPartBytes());
114+
return content.getContent().toByteBuffer();
116115

117116
}
118117

src/main/java/com/ning/http/client/providers/netty/NettyResponse.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public String getResponseBodyExcerpt(int maxLength) throws IOException {
4747

4848
public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
4949
// should be fine; except that it may split multi-byte chars (last char may become '?')
50+
if (charset == null) {
51+
charset = calculateCharset();
52+
}
5053
byte[] b = AsyncHttpProviderUtils.contentToBytes(bodyParts, maxLength);
5154
return new String(b, charset);
5255
}

0 commit comments

Comments
 (0)