GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestByteBufferOutputStream.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.common.nio;
29
30import java.io.File;
31import java.io.IOException;
32import java.io.RandomAccessFile;
33import java.nio.channels.FileChannel;
34
35import org.junit.Assert;
36import org.junit.Test;
37
38import com.jogamp.junit.util.SingletonJunitCase;
39
40import org.junit.FixMethodOrder;
41import org.junit.runners.MethodSorters;
42
43/**
44 * Testing {@link MappedByteBufferInputStream} and {@link MappedByteBufferOutputStream} editing functionality.
45 */
46@FixMethodOrder(MethodSorters.NAME_ASCENDING)
48
49 static void testImpl(final String fname,
50 final byte[] payLoad, final long payLoadOffset, final long postPayLoadFiller,
51 final byte[] endBytes,
52 final int sliceShift)
53 throws IOException
54 {
55 testImpl(fname, payLoad, payLoadOffset, postPayLoadFiller, endBytes, sliceShift, false);
56 testImpl(fname, payLoad, payLoadOffset, postPayLoadFiller, endBytes, sliceShift, true);
57 }
58 static void testImpl(final String fname,
59 final byte[] payLoad, final long payLoadOffset, final long postPayLoadFiller,
60 final byte[] endBytes,
61 final int sliceShift, final boolean synchronous)
62 throws IOException
63 {
64 final File file = new File(fname);
65 file.delete();
66 file.createNewFile();
67 file.deleteOnExit();
68
69 try {
70 final RandomAccessFile out = new RandomAccessFile(file, "rw");
71 final MappedByteBufferInputStream.FileResizeOp szOp = new MappedByteBufferInputStream.FileResizeOp() {
72 @Override
73 public void setLength(final long newSize) throws IOException {
74 out.setLength(newSize);
75 }
76 };
77 final MappedByteBufferInputStream mis = new MappedByteBufferInputStream(out.getChannel(),
78 FileChannel.MapMode.READ_WRITE,
80 sliceShift);
82 mos.setSynchronous(synchronous);
83
84 try {
85 // resize to payLoad start and position to it
86 mos.setLength(payLoadOffset);
87 Assert.assertEquals(payLoadOffset, out.length());
88 Assert.assertEquals(payLoadOffset, mos.length());
89 Assert.assertEquals(0, mos.position()); // no change
90 mos.position(payLoadOffset);
91 Assert.assertEquals(payLoadOffset, mos.position());
92
93 // mark, write-expand payLoad
94 mis.mark(1);
95 mos.write(payLoad);
96 Assert.assertEquals(payLoadOffset+payLoad.length, out.length());
97 Assert.assertEquals(payLoadOffset+payLoad.length, mos.length());
98 Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
99
100 // expand + 1
101 mos.setLength(payLoadOffset+payLoad.length+1);
102 Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length());
103 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length());
104 Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); // no change
105
106 // expand up-to very end, ahead of write - position to endBytes start
107 mos.setLength(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length);
108 Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, out.length());
109 Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, mos.length());
110 Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); // no change
111 mos.skip(postPayLoadFiller);
112 Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller, mos.position());
113
114 // write endBytes (no resize)
115 mos.write(endBytes);
116 Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, mos.position());
117
118 // Reset to payLoad, read it and verify
119 mis.reset();
120 Assert.assertEquals(payLoadOffset, mos.position());
121 Assert.assertEquals(payLoadOffset, mis.position());
122 final byte[] tmp = new byte[payLoad.length];
123 Assert.assertEquals(payLoad.length, mis.read(tmp));
124 Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
125 Assert.assertEquals(payLoadOffset+payLoad.length, mis.position());
126 Assert.assertArrayEquals(payLoad, tmp);
127
128 // Shrink to end of payLoad, mark, read >= 0, reset .. redo
129 Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, out.length());
130 Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, mos.length());
131 mos.setLength(payLoadOffset+payLoad.length+1);
132 Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length());
133 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length());
134 Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
135 mis.mark(1);
136 Assert.assertTrue(mis.read()>=0);
137 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position());
138 mis.reset();
139 Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
140 Assert.assertTrue(mis.read()>=0);
141 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position());
142
143 // Shrink -1, read EOS
144 mos.setLength(payLoadOffset+payLoad.length);
145 Assert.assertEquals(payLoadOffset+payLoad.length, out.length());
146 Assert.assertEquals(payLoadOffset+payLoad.length, mos.length());
147 Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
148 Assert.assertEquals(-1, mis.read());
149
150 // Expand + 1, mark, read >= 0, reset .. redo
151 mos.setLength(payLoadOffset+payLoad.length+1);
152 Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length());
153 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length());
154 Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
155 mis.mark(1);
156 Assert.assertTrue(mis.read()>=0);
157 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position());
158 mis.reset();
159 Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
160 Assert.assertTrue(mis.read()>=0);
161 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position());
162
163 // Shrink -1, read EOS, write-expand, reset and verify
164 mos.setLength(payLoadOffset+payLoad.length);
165 Assert.assertEquals(payLoadOffset+payLoad.length, out.length());
166 Assert.assertEquals(payLoadOffset+payLoad.length, mos.length());
167 Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
168 Assert.assertEquals(-1, mis.read());
169 mos.write('Z'); // expand while writing ..
170 Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length());
171 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length());
172 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position());
173 mis.reset();
174 Assert.assertEquals(payLoadOffset+payLoad.length, mos.position());
175 Assert.assertEquals(payLoadOffset+payLoad.length, mis.position());
176 Assert.assertEquals('Z', mis.read());
177 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position());
178 Assert.assertEquals(payLoadOffset+payLoad.length+1, mis.position());
179
180 // Shrink -2, shall clear mark, test reset failure
181 mos.setLength(payLoadOffset+payLoad.length-1);
182 Assert.assertEquals(payLoadOffset+payLoad.length-1, out.length());
183 Assert.assertEquals(payLoadOffset+payLoad.length-1, mos.length());
184 Assert.assertEquals(payLoadOffset+payLoad.length-1, mos.position());
185 try {
186 mis.reset();
187 Assert.assertTrue(false); // shall not reach
188 } catch( final IOException ioe ) {
189 Assert.assertNotNull(ioe);
190 }
191 mis.mark(1);
192
193 // ZERO file, test reset failure, read EOS, write-expand
194 mos.setLength(0);
195 Assert.assertEquals(0, out.length());
196 Assert.assertEquals(0, mos.length());
197 Assert.assertEquals(0, mos.position());
198 try {
199 mis.reset();
200 Assert.assertTrue(false); // shall not reach
201 } catch( final IOException ioe ) {
202 Assert.assertNotNull(ioe);
203 }
204 Assert.assertEquals(-1, mis.read());
205 mos.write('Z'); // expand while writing ..
206 Assert.assertEquals(1, out.length());
207 Assert.assertEquals(1, mos.length());
208 Assert.assertEquals(1, mos.position());
209 mis.position(0);
210 Assert.assertEquals(0, mos.position());
211 Assert.assertEquals(0, mis.position());
212 Assert.assertEquals('Z', mis.read());
213 } finally {
214 mos.close();
215 mis.close();
216 out.close();
217 }
218 } finally {
219 file.delete();
220 }
221 }
222
223 @Test
224 public void test00() throws IOException {
225 final int sliceShift = 13; // 8192 bytes per slice
226 testImpl(getSimpleTestName(".")+".bin", "123456789AB".getBytes(), 0L, 0L, "EOF".getBytes(), sliceShift);
227 }
228
229 @Test
230 public void test01() throws IOException {
231 final int sliceShift = 13; // 8192 bytes per slice
232 testImpl(getSimpleTestName(".")+".bin", "123456789AB".getBytes(), 9000L, 100L, "EOF".getBytes(), sliceShift);
233 }
234
235 @Test
236 public void test02() throws IOException {
237 final int sliceShift = 13; // 8192 bytes per slice
238 testImpl(getSimpleTestName(".")+".bin", "123456789AB".getBytes(), 8189L, 9001L, "EOF".getBytes(), sliceShift);
239 }
240
241 @Test
242 public void test03() throws IOException {
243 final int sliceShift = 13; // 8192 bytes per slice
244 testImpl(getSimpleTestName(".")+".bin", "123456789AB".getBytes(), 58189L, 109001L, "EOF".getBytes(), sliceShift);
245 }
246
247 @Test
248 public void test10() throws IOException {
249 final int sliceShift = 10; // 1024 bytes per slice
250 final byte[] payLoad = new byte[4096];
251 for(int i=0; i<payLoad.length; i++) {
252 payLoad[i] = (byte)('A' + i%26);
253 }
254 testImpl(getSimpleTestName(".")+".bin", payLoad, 0L, 0L, "EOF".getBytes(), sliceShift);
255 }
256
257 @Test
258 public void test11() throws IOException {
259 final int sliceShift = 10; // 1024 bytes per slice
260 final byte[] payLoad = new byte[4096];
261 for(int i=0; i<payLoad.length; i++) {
262 payLoad[i] = (byte)('A' + i%26);
263 }
264 testImpl(getSimpleTestName(".")+".bin", payLoad, 1030L, 99L, "EOF".getBytes(), sliceShift);
265 }
266
267 @Test
268 public void test12() throws IOException {
269 final int sliceShift = 10; // 1024 bytes per slice
270 final byte[] payLoad = new byte[4096];
271 for(int i=0; i<payLoad.length; i++) {
272 payLoad[i] = (byte)('A' + i%26);
273 }
274 testImpl(getSimpleTestName(".")+".bin", payLoad, 1021L, 1301L, "EOF".getBytes(), sliceShift);
275 }
276
277 @Test
278 public void test13() throws IOException {
279 final int sliceShift = 10; // 1024 bytes per slice
280 final byte[] payLoad = new byte[4096];
281 for(int i=0; i<payLoad.length; i++) {
282 payLoad[i] = (byte)('A' + i%26);
283 }
284 testImpl(getSimpleTestName(".")+".bin", payLoad, 3021L, 6301L, "EOF".getBytes(), sliceShift);
285 }
286
287 static boolean manualTest = false;
288
289 public static void main(final String args[]) throws IOException {
290 for(int i=0; i<args.length; i++) {
291 if(args[i].equals("-manual")) {
292 manualTest = true;
293 }
294 }
295 final String tstname = TestByteBufferOutputStream.class.getName();
296 org.junit.runner.JUnitCore.main(tstname);
297 }
298}
An InputStream implementation based on an underlying FileChannel's memory mapped ByteBuffer,...
final synchronized long position()
Returns the absolute position of the InputStream.
final synchronized MappedByteBufferOutputStream getOutputStream(final FileResizeOp fileResizeOp)
Returns a new MappedByteBufferOutputStream instance sharing all resources of this input stream,...
An OutputStream implementation based on an underlying FileChannel's memory mapped ByteBuffer.
final synchronized void setLength(final long newTotalSize)
See MappedByteBufferInputStream#setLength(long).
final synchronized long length()
See MappedByteBufferInputStream#length().
final synchronized long skip(final long n)
See MappedByteBufferInputStream#skip(long).
final synchronized long position()
See MappedByteBufferInputStream#position().
final synchronized void setSynchronous(final boolean s)
See MappedByteBufferInputStream#setSynchronous(boolean).
Testing MappedByteBufferInputStream and MappedByteBufferOutputStream editing functionality.
FLUSH_PRE_SOFT
Soft flush the previous lazily cached buffer slice when caching the next buffer slice,...
File resize interface allowing a file to change its size, e.g.