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.impl.nio;
29
30 import java.io.IOException;
31 import java.nio.ByteBuffer;
32 import java.nio.channels.ReadableByteChannel;
33 import java.nio.channels.WritableByteChannel;
34
35 import org.apache.hc.core5.util.Args;
36
37
38
39
40
41
42
43
44
45 public class BufferedData extends ExpandableBuffer {
46
47 public static BufferedData allocate(final int bufferSize) {
48 return new BufferedData(bufferSize);
49 }
50
51 protected BufferedData(final int bufferSize) {
52 super(bufferSize);
53 }
54
55 @Override
56 public final boolean hasData() {
57 return super.hasData();
58 }
59
60 @Override
61 public final int length() {
62 return super.length();
63 }
64
65 @Override
66 public final int capacity() {
67 return super.capacity();
68 }
69
70 @Override
71 public final void clear() {
72 super.clear();
73 }
74
75 public final void put(final ByteBuffer src) {
76 Args.notNull(src, "Data source");
77 setInputMode();
78 final int requiredCapacity = buffer().position() + src.remaining();
79 ensureAdjustedCapacity(requiredCapacity);
80 buffer().put(src);
81 }
82
83 public final int readFrom(final ReadableByteChannel channel) throws IOException {
84 Args.notNull(channel, "Channel");
85 setInputMode();
86 if (!buffer().hasRemaining()) {
87 expand();
88 }
89 return channel.read(buffer());
90 }
91
92 public final int writeTo(final WritableByteChannel dst) throws IOException {
93 if (dst == null) {
94 return 0;
95 }
96 setOutputMode();
97 return dst.write(buffer());
98 }
99
100 public final ByteBuffer data() {
101 setOutputMode();
102 return buffer();
103 }
104
105 }