View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.core5.testing.nio;
28  
29  import java.io.IOException;
30  import java.nio.ByteBuffer;
31  
32  import org.apache.hc.core5.http.ContentType;
33  import org.apache.hc.core5.http.nio.StreamChannel;
34  import org.apache.hc.core5.http.nio.entity.AbstractBinAsyncEntityProducer;
35  
36  public class MultiBinEntityProducer extends AbstractBinAsyncEntityProducer {
37  
38      private final byte[] bin;
39      private final int total;
40      private final ByteBuffer bytebuf;
41  
42      private int count;
43  
44      public MultiBinEntityProducer(final byte[] bin, final int total, final ContentType contentType) {
45          super(-1, contentType);
46          this.bin = bin;
47          this.total = total;
48          this.bytebuf = ByteBuffer.allocate(4096);
49          this.count = 0;
50      }
51  
52      @Override
53      public boolean isRepeatable() {
54          return true;
55      }
56  
57      @Override
58      protected int availableData() {
59          return Integer.MAX_VALUE;
60      }
61  
62      @Override
63      protected void produceData(final StreamChannel<ByteBuffer> channel) throws IOException {
64          while (bytebuf.remaining() > bin.length + 2 && count < total) {
65              bytebuf.put(bin);
66              count++;
67          }
68          if (bytebuf.position() > 0) {
69              bytebuf.flip();
70              channel.write(bytebuf);
71              bytebuf.compact();
72          }
73          if (count >= total && bytebuf.position() == 0) {
74              channel.endStream();
75          }
76      }
77  
78      @Override
79      public boolean isChunked() {
80          return false;
81      }
82  
83      @Override
84      public long getContentLength() {
85          return bin.length * total;
86      }
87  
88      @Override
89      public void failed(final Exception cause) {
90      }
91  
92      @Override
93      public void releaseResources() {
94          count = 0;
95          bytebuf.clear();
96          super.releaseResources();
97      }
98  
99  }