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.http.nio.util;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.nio.ByteBuffer;
33 import java.nio.channels.Channels;
34 import java.nio.channels.ReadableByteChannel;
35 import java.nio.channels.WritableByteChannel;
36
37 import org.apache.http.Consts;
38 import org.apache.http.ReadableByteChannelMock;
39 import org.apache.http.impl.io.HttpTransportMetricsImpl;
40 import org.apache.http.impl.nio.reactor.SessionOutputBufferImpl;
41 import org.apache.http.nio.ContentDecoder;
42 import org.apache.http.nio.ContentEncoder;
43 import org.apache.http.nio.reactor.SessionOutputBuffer;
44 import org.apache.http.util.EncodingUtils;
45 import org.junit.Assert;
46 import org.junit.Test;
47
48
49
50
51 public class TestBuffers {
52
53 @Test
54 public void testInputBufferOperations() throws IOException {
55 final ReadableByteChannel channel = new ReadableByteChannelMock(
56 new String[] {"stuff;", "more stuff"}, Consts.ASCII);
57
58 final ContentDecoder decoder = new ContentDecoderMock(channel);
59
60 final SimpleInputBuffer buffer = new SimpleInputBuffer(4, DirectByteBufferAllocator.INSTANCE);
61 final int count = buffer.consumeContent(decoder);
62 Assert.assertEquals(16, count);
63 Assert.assertTrue(decoder.isCompleted());
64
65 final byte[] b1 = new byte[5];
66
67 int len = buffer.read(b1);
68 Assert.assertEquals("stuff", EncodingUtils.getAsciiString(b1, 0, len));
69
70 final int c = buffer.read();
71 Assert.assertEquals(';', c);
72
73 final byte[] b2 = new byte[1024];
74
75 len = buffer.read(b2);
76 Assert.assertEquals("more stuff", EncodingUtils.getAsciiString(b2, 0, len));
77
78 Assert.assertEquals(-1, buffer.read());
79 Assert.assertEquals(-1, buffer.read(b2));
80 Assert.assertEquals(-1, buffer.read(b2, 0, b2.length));
81 Assert.assertTrue(buffer.isEndOfStream());
82
83 buffer.reset();
84 Assert.assertFalse(buffer.isEndOfStream());
85 }
86
87 @Test
88 public void testOutputBufferOperations() throws IOException {
89 final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
90 final WritableByteChannel channel = Channels.newChannel(outStream);
91 final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, Consts.ASCII);
92 final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
93
94 final ContentEncoder encoder = new ContentEncoderMock(channel, outbuf, metrics);
95
96 final SimpleOutputBuffer buffer = new SimpleOutputBuffer(4, DirectByteBufferAllocator.INSTANCE);
97
98 buffer.write(EncodingUtils.getAsciiBytes("stuff"));
99 buffer.write(';');
100 buffer.produceContent(encoder);
101
102 buffer.write(EncodingUtils.getAsciiBytes("more "));
103 buffer.write(EncodingUtils.getAsciiBytes("stuff"));
104 buffer.produceContent(encoder);
105
106 final byte[] content = outStream.toByteArray();
107 Assert.assertEquals("stuff;more stuff", EncodingUtils.getAsciiString(content));
108 }
109
110 @Test
111 public void testBufferInfo() throws Exception {
112 final SimpleOutputBuffer buffer = new SimpleOutputBuffer(8, DirectByteBufferAllocator.INSTANCE);
113
114 Assert.assertEquals(0, buffer.length());
115 Assert.assertEquals(8, buffer.available());
116 buffer.write(new byte[] {'1', '2', '3', '4'});
117 Assert.assertEquals(4, buffer.length());
118 Assert.assertEquals(4, buffer.available());
119 buffer.write(new byte[] {'1', '2', '3', '4', '5', '6', '7', '8'});
120 Assert.assertEquals(12, buffer.length());
121 Assert.assertEquals(0, buffer.available());
122 }
123
124 @Test
125 public void testInputBufferNullInput() throws IOException {
126 final SimpleInputBuffer buffer = new SimpleInputBuffer(4, DirectByteBufferAllocator.INSTANCE);
127 Assert.assertEquals(0, buffer.read(null));
128 Assert.assertEquals(0, buffer.read(null, 0, 0));
129 }
130
131 @Test
132 public void testOutputBufferNullInput() throws IOException {
133 final SimpleOutputBuffer buffer = new SimpleOutputBuffer(4, DirectByteBufferAllocator.INSTANCE);
134 buffer.write(null);
135 buffer.write(null, 0, 10);
136 Assert.assertFalse(buffer.hasData());
137 }
138
139 @Test
140 public void testDirectByteBufferAllocator() {
141 final DirectByteBufferAllocator allocator = new DirectByteBufferAllocator();
142 ByteBuffer buffer = allocator.allocate(1);
143 Assert.assertNotNull(buffer);
144 Assert.assertTrue(buffer.isDirect());
145 Assert.assertEquals(0, buffer.position());
146 Assert.assertEquals(1, buffer.limit());
147 Assert.assertEquals(1, buffer.capacity());
148
149 buffer = allocator.allocate(2048);
150 Assert.assertTrue(buffer.isDirect());
151 Assert.assertEquals(0, buffer.position());
152 Assert.assertEquals(2048, buffer.limit());
153 Assert.assertEquals(2048, buffer.capacity());
154
155 buffer = allocator.allocate(0);
156 Assert.assertTrue(buffer.isDirect());
157 Assert.assertEquals(0, buffer.position());
158 Assert.assertEquals(0, buffer.limit());
159 Assert.assertEquals(0, buffer.capacity());
160 }
161
162 @Test
163 public void testHeapByteBufferAllocator() {
164 final HeapByteBufferAllocator allocator = new HeapByteBufferAllocator();
165 ByteBuffer buffer = allocator.allocate(1);
166 Assert.assertNotNull(buffer);
167 Assert.assertFalse(buffer.isDirect());
168 Assert.assertEquals(0, buffer.position());
169 Assert.assertEquals(1, buffer.limit());
170 Assert.assertEquals(1, buffer.capacity());
171
172 buffer = allocator.allocate(2048);
173 Assert.assertFalse(buffer.isDirect());
174 Assert.assertEquals(0, buffer.position());
175 Assert.assertEquals(2048, buffer.limit());
176 Assert.assertEquals(2048, buffer.capacity());
177
178 buffer = allocator.allocate(0);
179 Assert.assertFalse(buffer.isDirect());
180 Assert.assertEquals(0, buffer.position());
181 Assert.assertEquals(0, buffer.limit());
182 Assert.assertEquals(0, buffer.capacity());
183 }
184
185 }