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.CharBuffer;
31  import java.nio.charset.Charset;
32  import java.nio.charset.StandardCharsets;
33  
34  import org.apache.hc.core5.http.ContentType;
35  import org.apache.hc.core5.http.nio.StreamChannel;
36  import org.apache.hc.core5.http.nio.entity.AbstractCharAsyncEntityProducer;
37  
38  public class MultiLineEntityProducer extends AbstractCharAsyncEntityProducer {
39  
40      private final String text;
41      private final int total;
42      private final CharBuffer charbuf;
43  
44      private int count;
45  
46      public MultiLineEntityProducer(final String text, final int total, final Charset charset) {
47          super(1024, -1, ContentType.TEXT_PLAIN.withCharset(charset));
48          this.text = text;
49          this.total = total;
50          this.charbuf = CharBuffer.allocate(4096);
51          this.count = 0;
52      }
53  
54      public MultiLineEntityProducer(final String text, final int total) {
55          this(text, total, StandardCharsets.US_ASCII);
56      }
57  
58      @Override
59      public boolean isRepeatable() {
60          return true;
61      }
62  
63      @Override
64      protected int availableData() {
65          return Integer.MAX_VALUE;
66      }
67  
68      @Override
69      protected void produceData(final StreamChannel<CharBuffer> channel) throws IOException {
70          while (charbuf.remaining() > text.length() + 2 && count < total) {
71              charbuf.put(text + "\r\n");
72              count++;
73          }
74          if (charbuf.position() > 0) {
75              charbuf.flip();
76              channel.write(charbuf);
77              charbuf.compact();
78          }
79          if (count >= total && charbuf.position() == 0) {
80              channel.endStream();
81          }
82      }
83  
84      @Override
85      public void failed(final Exception cause) {
86      }
87  
88      @Override
89      public void releaseResources() {
90          count = 0;
91          charbuf.clear();
92          super.releaseResources();
93      }
94  
95  }