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  
28  package org.apache.hc.core5.http;
29  
30  import java.io.IOException;
31  import java.nio.ByteBuffer;
32  import java.nio.CharBuffer;
33  import java.nio.channels.ClosedChannelException;
34  import java.nio.channels.WritableByteChannel;
35  import java.nio.charset.CharacterCodingException;
36  import java.nio.charset.Charset;
37  
38  public class WritableByteChannelMock implements WritableByteChannel {
39  
40      private final int capacityLimit;
41  
42      private int capacityUsed;
43      private ByteBuffer buf;
44      private boolean closed;
45  
46      public WritableByteChannelMock(final int initialSize, final int capacityLimit) {
47          this.buf = ByteBuffer.allocate(initialSize);
48          this.capacityLimit = capacityLimit;
49      }
50  
51      public WritableByteChannelMock(final int initialSize) {
52          this(initialSize, 0);
53      }
54  
55      private void expandCapacity(final int capacity) {
56          final ByteBuffer oldbuffer = this.buf;
57          this.buf = ByteBuffer.allocate(capacity);
58          oldbuffer.flip();
59          this.buf.put(oldbuffer);
60      }
61  
62      private void ensureCapacity(final int requiredCapacity) {
63          if (requiredCapacity > this.buf.capacity()) {
64              expandCapacity(requiredCapacity);
65          }
66      }
67  
68      @Override
69      public int write(final ByteBuffer src) throws IOException {
70          if (this.closed) {
71              throw new ClosedChannelException();
72          }
73          final int len = src.remaining();
74          ensureCapacity(this.buf.position() + len);
75          if (this.capacityLimit > 0) {
76              final int chunk = Math.min(this.capacityLimit - this.capacityUsed, len);
77              if (chunk > 0) {
78                  final int limit = src.limit();
79                  src.limit(src.position() + chunk);
80                  this.buf.put(src);
81                  src.limit(limit);
82                  this.capacityUsed += chunk;
83                  return chunk;
84              } else {
85                  return 0;
86              }
87          } else {
88              this.buf.put(src);
89              return len;
90          }
91      }
92  
93      @Override
94      public boolean isOpen() {
95          return !this.closed;
96      }
97  
98      @Override
99      public void close() throws IOException {
100         this.closed = true;
101     }
102 
103     public void flush() {
104         this.capacityUsed = 0;
105     }
106 
107     public void reset() {
108         this.capacityUsed = 0;
109         this.buf.clear();
110     }
111 
112     public String dump(final Charset charset) throws CharacterCodingException {
113         this.buf.flip();
114         final CharBuffer charBuffer = charset.newDecoder().decode(this.buf);
115         this.buf.compact();
116         return charBuffer.toString();
117     }
118 
119 }