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              }
85              return 0;
86          }
87          this.buf.put(src);
88          return len;
89      }
90  
91      @Override
92      public boolean isOpen() {
93          return !this.closed;
94      }
95  
96      @Override
97      public void close() throws IOException {
98          this.closed = true;
99      }
100 
101     public void flush() {
102         this.capacityUsed = 0;
103     }
104 
105     public void reset() {
106         this.capacityUsed = 0;
107         this.buf.clear();
108     }
109 
110     public byte[] toByteArray() {
111         final ByteBuffer dup = this.buf.duplicate();
112         dup.flip();
113         final byte[] bytes = new byte[dup.remaining()];
114         dup.get(bytes);
115         return bytes;
116     }
117 
118     public String dump(final Charset charset) throws CharacterCodingException {
119         this.buf.flip();
120         final CharBuffer charBuffer = charset.newDecoder().decode(this.buf);
121         this.buf.compact();
122         return charBuffer.toString();
123     }
124 
125 }