Skip to content

Commit 1ef6d82

Browse files
committed
More clean up
1 parent bfb9ed2 commit 1ef6d82

File tree

8 files changed

+25
-32
lines changed

8 files changed

+25
-32
lines changed

client/src/main/java/org/asynchttpclient/request/body/generator/BlockingFeedableBodyGenerator.java renamed to client/src/main/java/org/asynchttpclient/request/body/generator/BlockingQueueFeedableBodyGenerator.java

Lines changed: 3 additions & 10 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -13,25 +13,18 @@
13
*/
13
*/
14
package org.asynchttpclient.request.body.generator;
14
package org.asynchttpclient.request.body.generator;
15

15

16-
import java.util.Queue;
17
import java.util.concurrent.ArrayBlockingQueue;
16
import java.util.concurrent.ArrayBlockingQueue;
18
import java.util.concurrent.BlockingQueue;
17
import java.util.concurrent.BlockingQueue;
19

18

20-
public final class BlockingFeedableBodyGenerator extends QueueBasedFeedableBodyGenerator<BlockingQueue<BodyChunk>> {
19+
public final class BlockingQueueFeedableBodyGenerator extends QueueBasedFeedableBodyGenerator<BlockingQueue<BodyChunk>> {
21-
private final ArrayBlockingQueue<BodyChunk> queue;
22

20

23-
public BlockingFeedableBodyGenerator(int capacity) {
21+
public BlockingQueueFeedableBodyGenerator(int capacity) {
24-
queue = new ArrayBlockingQueue<>(capacity, true);
22+
super(new ArrayBlockingQueue<>(capacity, true));
25
}
23
}
26

24

27
@Override
25
@Override
28
protected boolean offer(BodyChunk chunk) throws InterruptedException {
26
protected boolean offer(BodyChunk chunk) throws InterruptedException {
29
queue.put(chunk);
27
queue.put(chunk);
30
return true;
28
return true;
31
}
29
}
32-
33-
@Override
34-
protected Queue<org.asynchttpclient.request.body.generator.BodyChunk> queue() {
35-
return queue;
36-
}
37
}
30
}

client/src/main/java/org/asynchttpclient/request/body/generator/BodyChunk.java

Lines changed: 4 additions & 4 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -16,11 +16,11 @@
16
import java.nio.ByteBuffer;
16
import java.nio.ByteBuffer;
17

17

18
public final class BodyChunk {
18
public final class BodyChunk {
19-
final boolean isLast;
19+
public final boolean last;
20-
final ByteBuffer buffer;
20+
public final ByteBuffer buffer;
21

21

22-
public BodyChunk(final ByteBuffer buffer, final boolean isLast) {
22+
public BodyChunk(final ByteBuffer buffer, final boolean last) {
23
this.buffer = buffer;
23
this.buffer = buffer;
24-
this.isLast = isLast;
24+
this.last = last;
25
}
25
}
26
}
26
}

client/src/main/java/org/asynchttpclient/request/body/generator/PushBody.java

Lines changed: 2 additions & 2 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -54,7 +54,7 @@ private BodyState readNextChunk(ByteBuf target) throws IOException {
54
if (nextChunk == null) {
54
if (nextChunk == null) {
55
// Nothing in the queue. suspend stream if nothing was read. (reads == 0)
55
// Nothing in the queue. suspend stream if nothing was read. (reads == 0)
56
return res;
56
return res;
57-
} else if (!nextChunk.buffer.hasRemaining() && !nextChunk.isLast) {
57+
} else if (!nextChunk.buffer.hasRemaining() && !nextChunk.last) {
58
// skip empty buffers
58
// skip empty buffers
59
queue.remove();
59
queue.remove();
60
} else {
60
} else {
@@ -69,7 +69,7 @@ private void readChunk(ByteBuf target, BodyChunk part) {
69
move(target, part.buffer);
69
move(target, part.buffer);
70

70

71
if (!part.buffer.hasRemaining()) {
71
if (!part.buffer.hasRemaining()) {
72-
if (part.isLast) {
72+
if (part.last) {
73
state = BodyState.STOP;
73
state = BodyState.STOP;
74
}
74
}
75
queue.remove();
75
queue.remove();

client/src/main/java/org/asynchttpclient/request/body/generator/QueueBasedFeedableBodyGenerator.java

Lines changed: 6 additions & 3 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -20,17 +20,20 @@
20

20

21
public abstract class QueueBasedFeedableBodyGenerator<T extends Queue<BodyChunk>> implements FeedableBodyGenerator {
21
public abstract class QueueBasedFeedableBodyGenerator<T extends Queue<BodyChunk>> implements FeedableBodyGenerator {
22

22

23+
protected final T queue;
23
private FeedListener listener;
24
private FeedListener listener;
24

25

26+
public QueueBasedFeedableBodyGenerator(T queue) {
27+
this.queue = queue;
28+
}
29+
25
@Override
30
@Override
26
public Body createBody() {
31
public Body createBody() {
27-
return new PushBody(queue());
32+
return new PushBody(queue);
28
}
33
}
29

34

30
protected abstract boolean offer(BodyChunk chunk) throws Exception;
35
protected abstract boolean offer(BodyChunk chunk) throws Exception;
31

36

32-
protected abstract Queue<BodyChunk> queue();
33-
34
@Override
37
@Override
35
public boolean feed(final ByteBuffer buffer, final boolean isLast) throws Exception {
38
public boolean feed(final ByteBuffer buffer, final boolean isLast) throws Exception {
36
boolean offered = offer(new BodyChunk(buffer, isLast));
39
boolean offered = offer(new BodyChunk(buffer, isLast));

client/src/main/java/org/asynchttpclient/request/body/generator/ReactiveStreamsBodyGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class ReactiveStreamsBodyGenerator implements FeedableBodyGenerator {
35

35

36
public ReactiveStreamsBodyGenerator(Publisher<ByteBuffer> publisher) {
36
public ReactiveStreamsBodyGenerator(Publisher<ByteBuffer> publisher) {
37
this.publisher = publisher;
37
this.publisher = publisher;
38-
this.feedableBodyGenerator = new UnboundedFeedableBodyGenerator();
38+
this.feedableBodyGenerator = new UnboundedQueueFeedableBodyGenerator();
39
}
39
}
40

40

41
public Publisher<ByteBuffer> getPublisher() {
41
public Publisher<ByteBuffer> getPublisher() {

client/src/main/java/org/asynchttpclient/request/body/generator/UnboundedFeedableBodyGenerator.java renamed to client/src/main/java/org/asynchttpclient/request/body/generator/UnboundedQueueFeedableBodyGenerator.java

Lines changed: 5 additions & 8 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -13,19 +13,16 @@
13
*/
13
*/
14
package org.asynchttpclient.request.body.generator;
14
package org.asynchttpclient.request.body.generator;
15

15

16-
import java.util.Queue;
17
import java.util.concurrent.ConcurrentLinkedQueue;
16
import java.util.concurrent.ConcurrentLinkedQueue;
18

17

19-
public final class UnboundedFeedableBodyGenerator extends QueueBasedFeedableBodyGenerator<ConcurrentLinkedQueue<BodyChunk>> {
18+
public final class UnboundedQueueFeedableBodyGenerator extends QueueBasedFeedableBodyGenerator<ConcurrentLinkedQueue<BodyChunk>> {
20-
private final Queue<BodyChunk> queue = new ConcurrentLinkedQueue<>();
21

19

22-
@Override
20+
public UnboundedQueueFeedableBodyGenerator() {
23-
protected boolean offer(BodyChunk chunk) throws Exception {
21+
super(new ConcurrentLinkedQueue<>());
24-
return queue.offer(chunk);
25
}
22
}
26

23

27
@Override
24
@Override
28-
protected Queue<org.asynchttpclient.request.body.generator.BodyChunk> queue() {
25+
protected boolean offer(BodyChunk chunk) throws Exception {
29-
return queue;
26+
return queue.offer(chunk);
30
}
27
}
31
}
28
}

client/src/test/java/org/asynchttpclient/request/body/ChunkingTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -32,7 +32,7 @@
32
import org.asynchttpclient.Response;
32
import org.asynchttpclient.Response;
33
import org.asynchttpclient.request.body.generator.FeedableBodyGenerator;
33
import org.asynchttpclient.request.body.generator.FeedableBodyGenerator;
34
import org.asynchttpclient.request.body.generator.InputStreamBodyGenerator;
34
import org.asynchttpclient.request.body.generator.InputStreamBodyGenerator;
35-
import org.asynchttpclient.request.body.generator.UnboundedFeedableBodyGenerator;
35+
import org.asynchttpclient.request.body.generator.UnboundedQueueFeedableBodyGenerator;
36
import org.testng.annotations.Test;
36
import org.testng.annotations.Test;
37

37

38
public class ChunkingTest extends AbstractBasicTest {
38
public class ChunkingTest extends AbstractBasicTest {
@@ -74,7 +74,7 @@ public void doTestWithInputStreamBodyGenerator(InputStream is) throws Throwable
74
public void doTestWithFeedableBodyGenerator(InputStream is) throws Throwable {
74
public void doTestWithFeedableBodyGenerator(InputStream is) throws Throwable {
75
try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {
75
try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {
76

76

77-
final FeedableBodyGenerator feedableBodyGenerator = new UnboundedFeedableBodyGenerator();
77+
final FeedableBodyGenerator feedableBodyGenerator = new UnboundedQueueFeedableBodyGenerator();
78
Request r = post(getTargetUrl()).setBody(feedableBodyGenerator).build();
78
Request r = post(getTargetUrl()).setBody(feedableBodyGenerator).build();
79

79

80
ListenableFuture<Response> responseFuture = c.executeRequest(r);
80
ListenableFuture<Response> responseFuture = c.executeRequest(r);

client/src/test/java/org/asynchttpclient/request/body/generator/FeedableBodyGeneratorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -28,12 +28,12 @@
28

28

29
public class FeedableBodyGeneratorTest {
29
public class FeedableBodyGeneratorTest {
30

30

31-
private UnboundedFeedableBodyGenerator feedableBodyGenerator;
31+
private UnboundedQueueFeedableBodyGenerator feedableBodyGenerator;
32
private TestFeedListener listener;
32
private TestFeedListener listener;
33

33

34
@BeforeMethod
34
@BeforeMethod
35
public void setUp() throws Exception {
35
public void setUp() throws Exception {
36-
feedableBodyGenerator = new UnboundedFeedableBodyGenerator();
36+
feedableBodyGenerator = new UnboundedQueueFeedableBodyGenerator();
37
listener = new TestFeedListener();
37
listener = new TestFeedListener();
38
feedableBodyGenerator.setListener(listener);
38
feedableBodyGenerator.setListener(listener);
39
}
39
}

0 commit comments

Comments
 (0)