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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;
import java.nio.channels.WritableByteChannel;
import java.nio.ByteBuffer;
import java.io.IOException;
import com.yahoo.io.GrowableBufferOutputStream;
/**
* Tests the GrowableBufferOutputStream
*
* @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a>
*/
public class GrowableBufferOutputStreamTestCase extends junit.framework.TestCase {
private byte[] testData;
static class DummyWritableByteChannel implements WritableByteChannel {
private ByteBuffer buffer;
public DummyWritableByteChannel(ByteBuffer buffer) {
this.buffer = buffer;
}
public int write(ByteBuffer src) throws IOException {
int written = Math.min(src.remaining(), buffer.remaining());
if (buffer.remaining() < src.remaining()) {
ByteBuffer tmp = src.slice();
tmp.limit(written);
src.position(src.position() + written);
} else {
buffer.put(src);
}
return written;
}
public boolean isOpen() {
return true;
}
public void close() throws IOException {}
}
public GrowableBufferOutputStreamTestCase(String name) {
super(name);
}
public void setUp() {
testData = new byte[100];
for (int i = 0; i < 100; ++i) {
testData[i] = (byte) i;
}
}
public void testSimple() throws IOException {
GrowableBufferOutputStream g = new GrowableBufferOutputStream(10, 5);
g.write(testData, 0, 100);
g.flush();
assertEquals(10, g.numWritableBuffers());
assertEquals(100, g.writableSize());
ByteBuffer sink = ByteBuffer.allocate(60);
DummyWritableByteChannel channel = new DummyWritableByteChannel(sink);
int written = g.channelWrite(channel);
assertEquals(60, written);
assertEquals(60, sink.position());
assertEquals(40, g.writableSize());
// there should be 4 buffers left now
assertEquals(4, g.numWritableBuffers());
// ensure that we got what we expected
for (int i = 0; i < 60; ++i) {
if (((int) sink.get(i)) != i) {
fail();
}
}
// then we write more data
g.write(testData, 0, 100);
g.flush();
assertEquals(140, g.writableSize());
// ...which implies that we should now have 14 writable buffers
assertEquals(14, g.numWritableBuffers());
// reset the sink so it can consume more data
sink.clear();
// then write more to the DummyWritableByteChannel
written = g.channelWrite(channel);
assertEquals(60, written);
assertEquals(60, sink.position());
assertEquals(80, g.writableSize());
// now there should be 8 buffers
assertEquals(8, g.numWritableBuffers());
// ensure that we got what we expected
for (int i = 0; i < 60; ++i) {
int val = (int) sink.get(i);
int expected = (i + 60) % 100;
if (val != expected) {
fail("Value was " + val + " and not " + i);
}
}
// when we clear there should be no buffers
g.clear();
assertEquals(0, g.numWritableBuffers());
assertEquals(0, g.writableSize());
// ditto after flush after clear
g.flush();
assertEquals(0, g.numWritableBuffers());
// flush the cache too
g.clearAll();
assertEquals(0, g.numWritableBuffers());
}
}
|