GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestByteBufferCopyStream.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.common.os.Platform;
39import com.jogamp.junit.util.SingletonJunitCase;
40
41import org.junit.FixMethodOrder;
42import org.junit.runners.MethodSorters;
43
44/**
45 * Testing {@link MappedByteBufferInputStream} and {@link MappedByteBufferOutputStream}
46 * direct stream to stream copy via mapped buffers.
47 */
48@FixMethodOrder(MethodSorters.NAME_ASCENDING)
50
51 static void testImpl(final String srcFileName, final long size,
52 final MappedByteBufferInputStream.CacheMode srcCacheMode, final int srcSliceShift,
53 final String dstFileName,
54 final MappedByteBufferInputStream.CacheMode dstCacheMode, final int dstSliceShift ) throws IOException {
55 System.err.println("Test: source[CacheMode "+srcCacheMode+", SliceShift "+srcSliceShift+"]");
56 System.err.println(" destin[CacheMode "+dstCacheMode+", SliceShift "+dstSliceShift+"]");
57 final Runtime runtime = Runtime.getRuntime();
58 final long[] usedMem0 = { 0 };
59 final long[] freeMem0 = { 0 };
60 final long[] usedMem1 = { 0 };
61 final long[] freeMem1 = { 0 };
62 final String prefix = "test "+String.format(TestByteBufferInputStream.PrintPrecision+" MiB", size/TestByteBufferInputStream.MIB);
63 TestByteBufferInputStream.dumpMem(prefix+" before", runtime, -1, -1, usedMem0, freeMem0 );
64
65 final long t0 = Platform.currentTimeMillis();
66 final File srcFile = new File(srcFileName);
67 srcFile.delete();
68 srcFile.createNewFile();
69 srcFile.deleteOnExit();
70
71 final RandomAccessFile input;
72 {
73 final RandomAccessFile _input = new RandomAccessFile(srcFile, "rw");
74 _input.setLength(size);
75 _input.close();
76 input = new RandomAccessFile(srcFile, "r");
77 }
78 final long t1 = Platform.currentTimeMillis();
79 final MappedByteBufferInputStream mis = new MappedByteBufferInputStream(input.getChannel(),
80 FileChannel.MapMode.READ_ONLY,
81 srcCacheMode,
82 srcSliceShift);
83 Assert.assertEquals(size, input.length());
84 Assert.assertEquals(size, mis.length());
85 Assert.assertEquals(0, mis.position());
86 Assert.assertEquals(size, mis.remaining());
87
88 final File dstFile = new File(dstFileName);
89 dstFile.delete();
90 dstFile.createNewFile();
91 dstFile.deleteOnExit();
92 final RandomAccessFile output = new RandomAccessFile(dstFile, "rw");
93 final MappedByteBufferInputStream.FileResizeOp szOp = new MappedByteBufferInputStream.FileResizeOp() {
94 @Override
95 public void setLength(final long newSize) throws IOException {
96 output.setLength(newSize);
97 }
98 };
99 final MappedByteBufferOutputStream mos = new MappedByteBufferOutputStream(output.getChannel(),
100 FileChannel.MapMode.READ_WRITE,
101 dstCacheMode,
102 srcSliceShift, szOp);
103 Assert.assertEquals(0, output.length());
104 Assert.assertEquals(0, mos.length());
105 Assert.assertEquals(0, mos.position());
106 Assert.assertEquals(0, mos.remaining());
107
108 OutOfMemoryError oome = null;
109 IOException ioe = null;
110
111 try {
112 mos.write(mis, mis.remaining());
113
114 Assert.assertEquals(size, input.length());
115 Assert.assertEquals(size, output.length());
116 Assert.assertEquals(size, mis.length());
117 Assert.assertEquals(size, mos.length());
118 Assert.assertEquals(size, mis.position());
119 Assert.assertEquals(size, mos.position());
120 Assert.assertEquals(0, mis.remaining());
121 Assert.assertEquals(0, mos.remaining());
122
123 } catch (final IOException e) {
124 if( e.getCause() instanceof OutOfMemoryError ) {
125 oome = (OutOfMemoryError) e.getCause(); // oops
126 } else {
127 ioe = e;
128 }
129 } catch (final OutOfMemoryError m) {
130 oome = m; // oops
131 } finally {
132 mos.close();
133 mis.close();
134 input.close();
135 output.close();
136 srcFile.delete();
137 dstFile.delete();
138 final long t5 = Platform.currentTimeMillis();
139 TestByteBufferInputStream.dumpMem(prefix+" after ", runtime, usedMem0[0], freeMem0[0], usedMem1, freeMem1 );
140 System.gc();
141 final long t6 = Platform.currentTimeMillis();
142 try {
143 Thread.sleep(500);
144 } catch (final InterruptedException e) { }
145 TestByteBufferInputStream.dumpMem(prefix+" gc'ed ", runtime, usedMem0[0], freeMem0[0], usedMem1, freeMem1 );
146 System.err.println("Performance Stats: ");
147 System.err.printf("- File-Create %6d ms\n", t1-t0);
148 System.err.printf("- File-Copy %6d ms\n", t5-t1);
149 System.err.printf("- GC %6d ms\n", t6-t5);
150 System.err.printf("- Total %6d ms\n", t6-t0);
151 }
152 if( null != ioe || null != oome ) {
153 if( null != oome ) {
154 System.err.printf("%s: OutOfMemoryError.2 %s%n", prefix, oome.getMessage());
155 oome.printStackTrace();
156 } else {
157 Assert.assertNull(ioe);
158 }
159 }
160 }
161
162 /** {@value} */
163 static final long halfMiB = 1L << 19;
164 /** {@value} */
165 static final long quaterGiB = 1L << 28;
166 /** {@value} */
167 static final long quaterPlusGiB = quaterGiB + halfMiB;
168 /** {@value} */
169 static final long halfGiB = 1L << 29;
170 /** {@value} */
171 static final long halfPlusGiB = halfGiB + halfMiB;
172 /** {@value} */
173 static final long oneGiB = 1L << 30;
174 /** {@value} */
175 static final long onePlusGiB = oneGiB + halfMiB;
176 /** {@value} */
177 static final long twoGiB = ( 2L << 30 );
178 /** {@value} */
179 static final long twoPlusGiB = twoGiB + halfMiB;
180
181 /** {@value} */
182 static final long lala = ( 1L << 27 );
183
184 @Test
185 public void test00() throws IOException {
186 final long size;
187 if( !manualTest && Platform.OSType.MACOS == Platform.getOSType() ) {
188 size = quaterGiB;
189 } else {
190 size = twoPlusGiB;
191 }
192 final int srcSliceShift = MappedByteBufferInputStream.DEFAULT_SLICE_SHIFT;
193 final int dstSliceShift = MappedByteBufferInputStream.DEFAULT_SLICE_SHIFT;
194 testImpl(getSimpleTestName(".")+"_In.bin", size, MappedByteBufferInputStream.CacheMode.FLUSH_PRE_HARD, srcSliceShift,
195 getSimpleTestName(".")+"_Out.bin", MappedByteBufferInputStream.CacheMode.FLUSH_PRE_HARD, dstSliceShift );
196 }
197
198 @Test
199 public void test01() throws IOException {
200 final long size;
201 if( !manualTest && Platform.OSType.MACOS == Platform.getOSType() ) {
202 size = quaterGiB;
203 } else {
204 size = twoPlusGiB;
205 }
206 final int srcSliceShift = MappedByteBufferInputStream.DEFAULT_SLICE_SHIFT;
207 final int dstSliceShift = MappedByteBufferInputStream.DEFAULT_SLICE_SHIFT;
208 testImpl(getSimpleTestName(".")+"_In.bin", size, MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, srcSliceShift,
209 getSimpleTestName(".")+"_Out.bin", MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, dstSliceShift );
210 }
211
212 @Test
213 public void test02() throws IOException {
214 final long size;
215 if( !manualTest && Platform.OSType.MACOS == Platform.getOSType() ) {
216 size = quaterPlusGiB;
217 } else {
218 size = halfPlusGiB;
219 }
220 final int srcSliceShift = 27; // 125M bytes per slice
221 final int dstSliceShift = 27; // 125M bytes per slice
222 testImpl(getSimpleTestName(".")+"_In.bin", size, MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, srcSliceShift,
223 getSimpleTestName(".")+"_Out.bin", MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, dstSliceShift );
224 }
225
226 @Test
227 public void test11() throws IOException {
228 final int srcSliceShift = 26; // 64M bytes per slice
229 final int dstSliceShift = 25; // 32M bytes per slice
230 final long size = quaterPlusGiB;
231 testImpl(getSimpleTestName(".")+"_In.bin", size, MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, srcSliceShift,
232 getSimpleTestName(".")+"_Out.bin", MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, dstSliceShift );
233 }
234
235 @Test
236 public void test12() throws IOException {
237 final int srcSliceShift = 25; // 32M bytes per slice
238 final int dstSliceShift = 26; // 64M bytes per slice
239 final long size = quaterPlusGiB;
240 testImpl(getSimpleTestName(".")+"_In.bin", size, MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, srcSliceShift,
241 getSimpleTestName(".")+"_Out.bin", MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, dstSliceShift );
242 }
243
244 static boolean manualTest = false;
245
246 public static void main(final String args[]) throws IOException {
247 for(int i=0; i<args.length; i++) {
248 if(args[i].equals("-manual")) {
249 manualTest = true;
250 }
251 }
252 final String tstname = TestByteBufferCopyStream.class.getName();
253 org.junit.runner.JUnitCore.main(tstname);
254 }
255}
An InputStream implementation based on an underlying FileChannel's memory mapped ByteBuffer,...
final synchronized long length()
Returns the total size in bytes of the InputStream.
final synchronized long position()
Returns the absolute position of the InputStream.
final synchronized long remaining()
Returns the number of remaining available bytes of the InputStream, i.e.
static final int DEFAULT_SLICE_SHIFT
Default slice shift, i.e.
An OutputStream implementation based on an underlying FileChannel's memory mapped ByteBuffer.
final synchronized long remaining()
See MappedByteBufferInputStream#remaining().
final synchronized long length()
See MappedByteBufferInputStream#length().
final synchronized long position()
See MappedByteBufferInputStream#position().
Testing MappedByteBufferInputStream and MappedByteBufferOutputStream direct stream to stream copy via...
Testing serial read of ByteBufferInputStream and MappedByteBufferInputStream, i.e.
static void dumpMem(final String pre, final Runtime runtime, final long usedMem0, final long freeMem0, final long[] usedMemN, final long[] freeMemN)
Utility class for querying platform specific properties.
Definition: Platform.java:58
static OSType getOSType()
Returns the OS type.
Definition: Platform.java:401
static long currentTimeMillis()
Returns the unix based current monotonic time in milliseconds.
Definition: Platform.java:543
FLUSH_PRE_HARD
Hard flush the previous lazily cached buffer slice when caching the next buffer slice,...
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.