28package com.jogamp.common.nio;
31import java.io.IOException;
32import java.io.RandomAccessFile;
33import java.nio.channels.FileChannel;
35import org.junit.Assert;
38import com.jogamp.junit.util.SingletonJunitCase;
40import org.junit.FixMethodOrder;
41import org.junit.runners.MethodSorters;
46@FixMethodOrder(MethodSorters.NAME_ASCENDING)
49 static void testImpl(
final String fname,
50 final byte[] payLoad,
final long payLoadOffset,
final long postPayLoadFiller,
51 final byte[] endBytes,
55 testImpl(fname, payLoad, payLoadOffset, postPayLoadFiller, endBytes, sliceShift,
false);
56 testImpl(fname, payLoad, payLoadOffset, postPayLoadFiller, endBytes, sliceShift,
true);
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)
64 final File file =
new File(fname);
70 final RandomAccessFile out =
new RandomAccessFile(file,
"rw");
73 public void setLength(
final long newSize)
throws IOException {
78 FileChannel.MapMode.READ_WRITE,
87 Assert.assertEquals(payLoadOffset, out.length());
88 Assert.assertEquals(payLoadOffset, mos.
length());
89 Assert.assertEquals(0, mos.
position());
91 Assert.assertEquals(payLoadOffset, mos.
position());
96 Assert.assertEquals(payLoadOffset+payLoad.length, out.length());
97 Assert.assertEquals(payLoadOffset+payLoad.length, mos.
length());
98 Assert.assertEquals(payLoadOffset+payLoad.length, mos.
position());
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());
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());
111 mos.
skip(postPayLoadFiller);
112 Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller, mos.
position());
116 Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, mos.
position());
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);
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());
136 Assert.assertTrue(mis.
read()>=0);
137 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.
position());
139 Assert.assertEquals(payLoadOffset+payLoad.length, mos.
position());
140 Assert.assertTrue(mis.
read()>=0);
141 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.
position());
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());
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());
156 Assert.assertTrue(mis.
read()>=0);
157 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.
position());
159 Assert.assertEquals(payLoadOffset+payLoad.length, mos.
position());
160 Assert.assertTrue(mis.
read()>=0);
161 Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.
position());
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());
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());
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());
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());
187 Assert.assertTrue(
false);
188 }
catch(
final IOException ioe ) {
189 Assert.assertNotNull(ioe);
195 Assert.assertEquals(0, out.length());
196 Assert.assertEquals(0, mos.
length());
197 Assert.assertEquals(0, mos.
position());
200 Assert.assertTrue(
false);
201 }
catch(
final IOException ioe ) {
202 Assert.assertNotNull(ioe);
204 Assert.assertEquals(-1, mis.
read());
206 Assert.assertEquals(1, out.length());
207 Assert.assertEquals(1, mos.
length());
208 Assert.assertEquals(1, mos.
position());
210 Assert.assertEquals(0, mos.
position());
211 Assert.assertEquals(0, mis.
position());
212 Assert.assertEquals(
'Z', mis.
read());
224 public void test00() throws IOException {
225 final int sliceShift = 13;
226 testImpl(getSimpleTestName(
".")+
".bin",
"123456789AB".getBytes(), 0L, 0L,
"EOF".getBytes(), sliceShift);
230 public void test01() throws IOException {
231 final int sliceShift = 13;
232 testImpl(getSimpleTestName(
".")+
".bin",
"123456789AB".getBytes(), 9000L, 100L,
"EOF".getBytes(), sliceShift);
236 public void test02() throws IOException {
237 final int sliceShift = 13;
238 testImpl(getSimpleTestName(
".")+
".bin",
"123456789AB".getBytes(), 8189L, 9001L,
"EOF".getBytes(), sliceShift);
242 public void test03() throws IOException {
243 final int sliceShift = 13;
244 testImpl(getSimpleTestName(
".")+
".bin",
"123456789AB".getBytes(), 58189L, 109001L,
"EOF".getBytes(), sliceShift);
248 public void test10() throws IOException {
249 final int sliceShift = 10;
250 final byte[] payLoad =
new byte[4096];
251 for(
int i=0; i<payLoad.length; i++) {
252 payLoad[i] = (byte)(
'A' + i%26);
254 testImpl(getSimpleTestName(
".")+
".bin", payLoad, 0L, 0L,
"EOF".getBytes(), sliceShift);
258 public void test11() throws IOException {
259 final int sliceShift = 10;
260 final byte[] payLoad =
new byte[4096];
261 for(
int i=0; i<payLoad.length; i++) {
262 payLoad[i] = (byte)(
'A' + i%26);
264 testImpl(getSimpleTestName(
".")+
".bin", payLoad, 1030L, 99L,
"EOF".getBytes(), sliceShift);
268 public void test12() throws IOException {
269 final int sliceShift = 10;
270 final byte[] payLoad =
new byte[4096];
271 for(
int i=0; i<payLoad.length; i++) {
272 payLoad[i] = (byte)(
'A' + i%26);
274 testImpl(getSimpleTestName(
".")+
".bin", payLoad, 1021L, 1301L,
"EOF".getBytes(), sliceShift);
278 public void test13() throws IOException {
279 final int sliceShift = 10;
280 final byte[] payLoad =
new byte[4096];
281 for(
int i=0; i<payLoad.length; i++) {
282 payLoad[i] = (byte)(
'A' + i%26);
284 testImpl(getSimpleTestName(
".")+
".bin", payLoad, 3021L, 6301L,
"EOF".getBytes(), sliceShift);
287 static boolean manualTest =
false;
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")) {
296 org.junit.runner.JUnitCore.
main(tstname);
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 void close()
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).
final synchronized void write(final int b)
Testing MappedByteBufferInputStream and MappedByteBufferOutputStream editing functionality.
static void main(final String args[])