1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 }