Skip to content

Commit 23a1011

Browse files
author
Stephane Landelle
committed
Minor clean up
1 parent 290dd66 commit 23a1011

File tree

7 files changed

+43
-49
lines changed

7 files changed

+43
-49
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
*/
1313
package org.asynchttpclient;
1414

15-
import org.asynchttpclient.Response.ResponseBuilder;
16-
1715
import java.io.FilterInputStream;
1816
import java.io.IOException;
1917
import java.io.InputStream;

api/src/main/java/org/asynchttpclient/providers/jdk/JDKAsyncHttpProvider.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public T call() throws Exception {
322322
InputStream stream = is;
323323
if (bufferResponseInMemory || byteToRead <= 0) {
324324
int[] lengthWrapper = new int[1];
325-
byte[] bytes = AsyncHttpProviderUtils.readFully(is, lengthWrapper);
325+
byte[] bytes = readFully(is, lengthWrapper);
326326
stream = new ByteArrayInputStream(bytes, 0, lengthWrapper[0]);
327327
byteToRead = lengthWrapper[0];
328328
}
@@ -410,6 +410,34 @@ public T call() throws Exception {
410410
}
411411
return null;
412412
}
413+
414+
// FIXME Streams should be streamed, not turned into byte arrays
415+
private final byte[] readFully(InputStream in, int[] lengthWrapper) throws IOException {
416+
// just in case available() returns bogus (or -1), allocate non-trivial chunk
417+
byte[] b = new byte[Math.max(512, in.available())];
418+
int offset = 0;
419+
while (true) {
420+
int left = b.length - offset;
421+
int count = in.read(b, offset, left);
422+
if (count < 0) { // EOF
423+
break;
424+
}
425+
offset += count;
426+
if (count == left) { // full buffer, need to expand
427+
b = doubleUp(b);
428+
}
429+
}
430+
// wish Java had Tuple return type...
431+
lengthWrapper[0] = offset;
432+
return b;
433+
}
434+
435+
private final byte[] doubleUp(byte[] b) {
436+
int len = b.length;
437+
byte[] b2 = new byte[len + len];
438+
System.arraycopy(b, 0, b2, 0, len);
439+
return b2;
440+
}
413441

414442
private FilterContext handleIoException(FilterContext fc) throws FilterException {
415443
for (IOExceptionFilter asyncFilter : config.getIOExceptionFilters()) {
@@ -571,7 +599,7 @@ private void configure(URI uri, HttpURLConnection urlConnection, Request request
571599
urlConnection.getOutputStream().write(b);
572600
} else if (request.getStreamData() != null) {
573601
int[] lengthWrapper = new int[1];
574-
cachedBytes = AsyncHttpProviderUtils.readFully(request.getStreamData(), lengthWrapper);
602+
cachedBytes = readFully(request.getStreamData(), lengthWrapper);
575603
cachedBytesLenght = lengthWrapper[0];
576604
urlConnection.setRequestProperty("Content-Length", String.valueOf(cachedBytesLenght));
577605
urlConnection.setFixedLengthStreamingMode(cachedBytesLenght);

api/src/main/java/org/asynchttpclient/resumable/PropertiesBasedResumableProcessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ private static String append(Map.Entry<String, Long> e) {
9696
/**
9797
* {@inheritDoc}
9898
*/
99-
/* @Override */
99+
@Override
100100
public Map<String, Long> load() {
101+
Scanner scan = null;
101102
try {
102-
Scanner scan = new Scanner(new File(TMP, storeName), "UTF-8");
103+
scan = new Scanner(new File(TMP, storeName), "UTF-8");
103104
scan.useDelimiter("[=\n]");
104105

105106
String key;
@@ -115,6 +116,9 @@ public Map<String, Long> load() {
115116
} catch (Throwable ex) {
116117
// Survive any exceptions
117118
log.warn(ex.getMessage(), ex);
119+
} finally {
120+
if (scan != null)
121+
scan.close();
118122
}
119123
return properties;
120124
}

api/src/main/java/org/asynchttpclient/resumable/ResumableIOExceptionFilter.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@
1818
import org.asynchttpclient.filter.IOExceptionFilter;
1919

2020
/**
21-
* Simple {@link org.asynchttpclient.filter.IOExceptionFilter} that replay the current {@link org.asynchttpclient.Request} using
22-
* a {@link ResumableAsyncHandler}
21+
* Simple {@link org.asynchttpclient.filter.IOExceptionFilter} that replay the current {@link org.asynchttpclient.Request} using a {@link ResumableAsyncHandler}
2322
*/
2423
public class ResumableIOExceptionFilter implements IOExceptionFilter {
25-
public FilterContext filter(FilterContext ctx) throws FilterException {
24+
public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException {
2625
if (ctx.getIOException() != null && ctx.getAsyncHandler() instanceof ResumableAsyncHandler) {
2726

2827
Request request = ResumableAsyncHandler.class.cast(ctx.getAsyncHandler()).adjustRequestRange(ctx.getRequest());
2928

30-
return new FilterContext.FilterContextBuilder(ctx).request(request).replayRequest(true).build();
29+
return new FilterContext.FilterContextBuilder<T>(ctx).request(request).replayRequest(true).build();
3130
}
3231
return ctx;
3332
}
3433
}
35-

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

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import java.io.ByteArrayInputStream;
1616
import java.io.FileNotFoundException;
17-
import java.io.IOException;
1817
import java.io.InputStream;
1918
import java.io.SequenceInputStream;
2019
import java.io.UnsupportedEncodingException;
@@ -379,35 +378,6 @@ public final static MultipartRequestEntity createMultipartRequestEntity(List<Par
379378
return new MultipartRequestEntity(parts, requestHeaders);
380379
}
381380

382-
// FIXME remove this: tests should use IOUtils and main shouldn't read Stream but... stream them!
383-
@Deprecated
384-
public final static byte[] readFully(InputStream in, int[] lengthWrapper) throws IOException {
385-
// just in case available() returns bogus (or -1), allocate non-trivial chunk
386-
byte[] b = new byte[Math.max(512, in.available())];
387-
int offset = 0;
388-
while (true) {
389-
int left = b.length - offset;
390-
int count = in.read(b, offset, left);
391-
if (count < 0) { // EOF
392-
break;
393-
}
394-
offset += count;
395-
if (count == left) { // full buffer, need to expand
396-
b = doubleUp(b);
397-
}
398-
}
399-
// wish Java had Tuple return type...
400-
lengthWrapper[0] = offset;
401-
return b;
402-
}
403-
404-
private static byte[] doubleUp(byte[] b) {
405-
int len = b.length;
406-
byte[] b2 = new byte[len + len];
407-
System.arraycopy(b, 0, b2, 0, len);
408-
return b2;
409-
}
410-
411381
public static String encodeCookies(Collection<Cookie> cookies) {
412382
StringBuilder sb = new StringBuilder();
413383

api/src/test/java/org/asynchttpclient/async/RemoteSiteTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.concurrent.CountDownLatch;
2424
import java.util.concurrent.TimeUnit;
2525

26+
import org.apache.commons.io.IOUtils;
2627
import org.asynchttpclient.AsyncHandler;
2728
import org.asynchttpclient.AsyncHttpClient;
2829
import org.asynchttpclient.AsyncHttpClientConfig;
@@ -33,7 +34,6 @@
3334
import org.asynchttpclient.Request;
3435
import org.asynchttpclient.RequestBuilder;
3536
import org.asynchttpclient.Response;
36-
import org.asynchttpclient.util.AsyncHttpProviderUtils;
3737
import org.testng.annotations.Test;
3838

3939
/**
@@ -173,13 +173,9 @@ public void asyncFullBodyProperlyRead() throws Exception {
173173
Response r = client.prepareGet("http://www.cyberpresse.ca/").execute().get();
174174

175175
InputStream stream = r.getResponseBodyAsStream();
176-
// FIXME available is an ESTIMATE!!!
177-
int available = stream.available();
178-
int[] lengthWrapper = new int[1];
179-
AsyncHttpProviderUtils.readFully(stream, lengthWrapper);
180-
int byteToRead = lengthWrapper[0];
176+
int contentLength = Integer.valueOf(r.getHeader("Content-Length"));
181177

182-
assertEquals(available, byteToRead);
178+
assertEquals(contentLength, IOUtils.toByteArray(stream).length);
183179
} finally {
184180
client.close();
185181
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/handler/WebSocketProtocol.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public void onError(ChannelHandlerContext ctx, Throwable e) {
184184
return;
185185
}
186186

187-
NettyResponseFuture<?> nettyResponse = (NettyResponseFuture) attribute;
187+
NettyResponseFuture<?> nettyResponse = (NettyResponseFuture<?>) attribute;
188188
WebSocketUpgradeHandler h = WebSocketUpgradeHandler.class.cast(nettyResponse.getAsyncHandler());
189189

190190
NettyWebSocket webSocket = NettyWebSocket.class.cast(h.onCompleted());

0 commit comments

Comments
 (0)