|
| 1 | +/* |
| 2 | + * Copyright (c) 2012 Sonatype, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This program is licensed to you under the Apache License Version 2.0, |
| 5 | + * and you may not use this file except in compliance with the Apache License Version 2.0. |
| 6 | + * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, |
| 9 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 10 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 12 | + */ |
| 13 | +package com.ning.http.client.providers.netty; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.nio.ByteBuffer; |
| 17 | +import java.util.Queue; |
| 18 | +import java.util.concurrent.ConcurrentLinkedQueue; |
| 19 | +import java.util.concurrent.atomic.AtomicInteger; |
| 20 | + |
| 21 | +import com.ning.http.client.Body; |
| 22 | +import com.ning.http.client.BodyGenerator; |
| 23 | + |
| 24 | +/** |
| 25 | + * {@link BodyGenerator} which may return just part of the payload at the time |
| 26 | + * handler is requesting it. If it happens - PartialBodyGenerator becomes responsible |
| 27 | + * for finishing payload transferring asynchronously. |
| 28 | + */ |
| 29 | +public class FeedableBodyGenerator implements BodyGenerator { |
| 30 | + private final static byte[] END_PADDING = "\r\n".getBytes(); |
| 31 | + private final static byte[] ZERO = "0".getBytes(); |
| 32 | + private final Queue<BodyPart> queue = new ConcurrentLinkedQueue<BodyPart>(); |
| 33 | + private final AtomicInteger queueSize = new AtomicInteger(); |
| 34 | + private FeedListener listener; |
| 35 | + |
| 36 | + @Override |
| 37 | + public Body createBody() throws IOException { |
| 38 | + return new PushBody(); |
| 39 | + } |
| 40 | + |
| 41 | + public void feed(final ByteBuffer buffer, final boolean isLast) throws IOException { |
| 42 | + queue.offer(new BodyPart(buffer, isLast)); |
| 43 | + queueSize.incrementAndGet(); |
| 44 | + if (listener != null) { |
| 45 | + listener.onContentAdded(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public static interface FeedListener { |
| 50 | + public void onContentAdded(); |
| 51 | + } |
| 52 | + |
| 53 | + public void setListener(FeedListener listener) { |
| 54 | + this.listener = listener; |
| 55 | + } |
| 56 | + |
| 57 | + private final class PushBody implements Body { |
| 58 | + private final int ONGOING = 0; |
| 59 | + private final int CLOSING = 1; |
| 60 | + private final int FINISHED = 2; |
| 61 | + |
| 62 | + private int finishState = 0; |
| 63 | + |
| 64 | + @Override |
| 65 | + public long getContentLength() { |
| 66 | + return -1; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public long read(final ByteBuffer buffer) throws IOException { |
| 71 | + BodyPart nextPart = queue.peek(); |
| 72 | + if (nextPart == null) { |
| 73 | + // Nothing in the queue |
| 74 | + switch (finishState) { |
| 75 | + case ONGOING: |
| 76 | + return 0; |
| 77 | + case CLOSING: |
| 78 | + buffer.put(ZERO); |
| 79 | + buffer.put(END_PADDING); |
| 80 | + finishState = FINISHED; |
| 81 | + return buffer.position(); |
| 82 | + case FINISHED: |
| 83 | + buffer.put(END_PADDING); |
| 84 | + return -1; |
| 85 | + } |
| 86 | + } |
| 87 | + int capacity = buffer.remaining() - 10; // be safe (we'll have to add size, ending, etc.) |
| 88 | + int size = Math.min(nextPart.buffer.remaining(), capacity); |
| 89 | + buffer.put(Integer.toHexString(size).getBytes()); |
| 90 | + buffer.put(END_PADDING); |
| 91 | + for (int i=0; i < size; i++) { |
| 92 | + buffer.put(nextPart.buffer.get()); |
| 93 | + } |
| 94 | + buffer.put(END_PADDING); |
| 95 | + if (!nextPart.buffer.hasRemaining()) { |
| 96 | + if (nextPart.isLast) { |
| 97 | + finishState = CLOSING; |
| 98 | + } |
| 99 | + queue.remove(); |
| 100 | + } |
| 101 | + return size; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void close() throws IOException { |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + private final static class BodyPart { |
| 111 | + private final boolean isLast; |
| 112 | + private final ByteBuffer buffer; |
| 113 | + |
| 114 | + public BodyPart(final ByteBuffer buffer, final boolean isLast) { |
| 115 | + this.buffer = buffer; |
| 116 | + this.isLast = isLast; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments