|
16 | 16 | import org.jboss.netty.buffer.ChannelBuffers;
|
17 | 17 | import org.jboss.netty.handler.stream.ChunkedInput;
|
18 | 18 |
|
19 |
| -import java.io.IOException; |
20 | 19 | import java.nio.ByteBuffer;
|
21 | 20 |
|
22 | 21 | /**
|
23 | 22 | * Adapts a {@link Body} to Netty's {@link ChunkedInput}.
|
24 | 23 | */
|
25 |
| -class BodyChunkedInput |
26 |
| - implements ChunkedInput { |
| 24 | +class BodyChunkedInput implements ChunkedInput { |
27 | 25 |
|
28 |
| - private final Body body; |
29 |
| - |
30 |
| - private final int chunkSize = 1024 * 8; |
| 26 | + private static final int DEFAULT_CHUNK_SIZE = 8 * 1024; |
31 | 27 |
|
32 |
| - private ByteBuffer nextChunk; |
| 28 | + private final Body body; |
| 29 | + private final int contentLength; |
| 30 | + private final int chunkSize; |
33 | 31 |
|
34 |
| - private static final ByteBuffer EOF = ByteBuffer.allocate(0); |
| 32 | + private boolean endOfInput; |
35 | 33 |
|
36 | 34 | public BodyChunkedInput(Body body) {
|
37 | 35 | if (body == null) {
|
38 | 36 | throw new IllegalArgumentException("no body specified");
|
39 | 37 | }
|
40 | 38 | this.body = body;
|
| 39 | + contentLength = (int) body.getContentLength(); |
| 40 | + if (contentLength <= 0) |
| 41 | + chunkSize = DEFAULT_CHUNK_SIZE; |
| 42 | + else |
| 43 | + chunkSize = Math.min(contentLength, DEFAULT_CHUNK_SIZE); |
41 | 44 | }
|
42 | 45 |
|
43 |
| - private ByteBuffer peekNextChuck() |
44 |
| - throws IOException { |
| 46 | + public boolean hasNextChunk() throws Exception { |
| 47 | + // unused |
| 48 | + throw new UnsupportedOperationException(); |
| 49 | + } |
45 | 50 |
|
46 |
| - if (nextChunk == null) { |
| 51 | + public Object nextChunk() throws Exception { |
| 52 | + if (endOfInput) { |
| 53 | + return null; |
| 54 | + } else { |
47 | 55 | ByteBuffer buffer = ByteBuffer.allocate(chunkSize);
|
48 |
| - if (body.read(buffer) < 0) { |
49 |
| - nextChunk = EOF; |
| 56 | + long r = body.read(buffer); |
| 57 | + if (r < 0L) { |
| 58 | + endOfInput = true; |
| 59 | + return null; |
50 | 60 | } else {
|
| 61 | + endOfInput = r == contentLength || r < chunkSize; |
51 | 62 | buffer.flip();
|
52 |
| - nextChunk = buffer; |
| 63 | + return ChannelBuffers.wrappedBuffer(buffer); |
53 | 64 | }
|
54 | 65 | }
|
55 |
| - return nextChunk; |
56 |
| - } |
57 |
| - |
58 |
| - public boolean hasNextChunk() |
59 |
| - throws Exception { |
60 |
| - return !isEndOfInput(); |
61 |
| - } |
62 |
| - |
63 |
| - public Object nextChunk() |
64 |
| - throws Exception { |
65 |
| - ByteBuffer buffer = peekNextChuck(); |
66 |
| - if (buffer == EOF) { |
67 |
| - return null; |
68 |
| - } |
69 |
| - nextChunk = null; |
70 |
| - return ChannelBuffers.wrappedBuffer(buffer); |
71 | 66 | }
|
72 | 67 |
|
73 |
| - public boolean isEndOfInput() |
74 |
| - throws Exception { |
75 |
| - return peekNextChuck() == EOF; |
| 68 | + public boolean isEndOfInput() throws Exception { |
| 69 | + // called by ChunkedWriteHandler AFTER nextChunk |
| 70 | + return endOfInput; |
76 | 71 | }
|
77 | 72 |
|
78 |
| - public void close() |
79 |
| - throws Exception { |
| 73 | + public void close() throws Exception { |
80 | 74 | body.close();
|
81 | 75 | }
|
82 |
| - |
83 | 76 | }
|
0 commit comments