29package com.jogamp.common.util;
32import java.io.BufferedInputStream;
33import java.io.BufferedOutputStream;
35import java.io.FileOutputStream;
36import java.io.IOException;
37import java.io.OutputStream;
38import java.net.URISyntaxException;
39import java.net.URLConnection;
40import java.nio.ByteBuffer;
42import org.junit.AfterClass;
43import org.junit.Assert;
44import org.junit.BeforeClass;
47import com.jogamp.common.ExceptionUtils;
48import com.jogamp.common.net.URIDumpUtil;
49import com.jogamp.common.net.Uri;
50import com.jogamp.common.os.MachineDataInfo;
51import com.jogamp.common.os.Platform;
52import com.jogamp.junit.util.SingletonJunitCase;
54import org.junit.FixMethodOrder;
55import org.junit.runners.MethodSorters;
57@FixMethodOrder(MethodSorters.NAME_ASCENDING)
61 static final int tsz = machine.pageSizeInBytes() + machine.pageSizeInBytes() / 2 ;
62 static final byte[] orig =
new byte[tsz];
63 static final String tfilename =
"./test.bin" ;
66 public static void setup() throws IOException {
67 final File tfile =
new File(tfilename);
69 final OutputStream tout =
new BufferedOutputStream(
new FileOutputStream(tfile));
70 for(
int i=0; i<tsz; i++) {
71 final byte b = (byte) (i%256);
80 final File tfile =
new File(tfilename);
87 final String input =
"./dummy/nop/../a.txt";
88 final String expected =
"dummy/a.txt";
92 final String input =
"../dummy/nop/../a.txt";
93 final String expected =
"../dummy/a.txt";
97 final String input =
".././dummy/nop/../a.txt";
98 final String expected =
"../dummy/a.txt";
102 final String input =
"./../dummy/nop/../a.txt";
103 final String expected =
"../dummy/a.txt";
107 final String input =
"../dummy/./nop/../a.txt";
108 final String expected =
"../dummy/a.txt";
112 final String input =
"/dummy/nop/./../a.txt";
113 final String expected =
"/dummy/a.txt";
117 final String input =
"dummy/../nop/./.././aaa/bbb/../../a.txt";
118 final String expected =
"a.txt";
122 final String input =
"/dummy/../nop/./.././aaa/bbb/././ccc/../../../a.txt";
123 final String expected =
"/a.txt";
127 URISyntaxException use =
null;
130 final String input =
"../../error.txt";
131 final String expected =
"error.txt";
133 System.err.println(
"input : "+input);
134 System.err.println(
"expected: "+expected);
135 System.err.println(
"result : "+result);
136 Assert.assertEquals(expected, result);
137 }
catch (
final URISyntaxException _use) {
141 Assert.assertNotNull(
"URISyntaxException expected", use);
144 URISyntaxException use =
null;
147 final String input =
".././a/../../error.txt";
148 final String expected =
"error.txt";
150 System.err.println(
"input : "+input);
151 System.err.println(
"expected: "+expected);
152 System.err.println(
"result : "+result);
153 Assert.assertEquals(expected, result);
154 }
catch (
final URISyntaxException _use) {
158 Assert.assertNotNull(
"URISyntaxException expected", use);
164 final URLConnection urlConn =
IOUtil.
getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
165 Assert.assertNotNull(urlConn);
166 final BufferedInputStream bis =
new BufferedInputStream( urlConn.getInputStream() );
173 Assert.assertEquals(
"Byte number not equal orig vs array", orig.length, bb.length);
174 Assert.assertTrue(
"Bytes not equal orig vs array", Arrays.equals(orig, bb));
180 final URLConnection urlConn =
IOUtil.
getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
181 Assert.assertNotNull(urlConn);
182 final BufferedInputStream bis =
new BufferedInputStream( urlConn.getInputStream() );
189 Assert.assertEquals(
"Byte number not equal orig vs buffer", orig.length, bb.limit());
191 for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;
192 Assert.assertTrue(
"Bytes not equal orig vs array", 0>i);
197 final String tfilename2 =
"./test2.bin" ;
198 final URLConnection urlConn1 =
IOUtil.
getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
199 Assert.assertNotNull(urlConn1);
201 final File file2 =
new File(tfilename2);
202 file2.deleteOnExit();
205 final URLConnection urlConn2 =
IOUtil.
getResource(tfilename2, this.getClass().getClassLoader(), this.getClass());
206 Assert.assertNotNull(urlConn2);
208 final BufferedInputStream bis =
new BufferedInputStream( urlConn2.getInputStream() );
215 Assert.assertEquals(
"Byte number not equal orig vs buffer", orig.length, bb.limit());
217 for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;
218 Assert.assertTrue(
"Bytes not equal orig vs array", 0>i);
226 final URLConnection urlConn =
IOUtil.
getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
227 Assert.assertNotNull(urlConn);
228 final BufferedInputStream bis =
new BufferedInputStream( urlConn.getInputStream() );
230 final int skipBytes = 0;
231 final int byteCount = orig.length;
237 Assert.assertTrue( machine.
pageAlignedSize( byteCount ) >= byteCount );
238 Assert.assertEquals( machine.
pageAlignedSize( byteCount ), bb.capacity() );
239 Assert.assertEquals(orig.length, bb.limit());
241 for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;
242 Assert.assertTrue(
"Bytes not equal orig vs array", 0>i);
243 System.err.println(getTestMethodName()+
" OK: ["+skipBytes+
".."+(skipBytes+byteCount)+
"): "+bb);
248 final URLConnection urlConn =
IOUtil.
getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
249 Assert.assertNotNull(urlConn);
250 final BufferedInputStream bis =
new BufferedInputStream( urlConn.getInputStream() );
252 final int skipBytes = 0;
253 final int byteCount = orig.length + machine.pageSizeInBytes();
259 Assert.assertTrue( machine.
pageAlignedSize( byteCount ) >= byteCount );
260 Assert.assertEquals( machine.
pageAlignedSize( byteCount ), bb.capacity() );
261 Assert.assertEquals( orig.length, bb.limit() );
263 for(i=tsz-1; i>=0 && orig[i]==bb.get(i); i--) ;
264 Assert.assertTrue(
"Bytes not equal orig vs array", 0>i);
265 System.err.println(getTestMethodName()+
" OK: ["+skipBytes+
".."+(skipBytes+byteCount)+
"): "+bb);
270 final URLConnection urlConn =
IOUtil.
getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
271 Assert.assertNotNull(urlConn);
272 final BufferedInputStream bis =
new BufferedInputStream( urlConn.getInputStream() );
274 final int skipBytes = 0;
275 final int byteCount = orig.length / 2;
276 Assert.assertTrue( orig.length >= byteCount );
282 Assert.assertTrue( machine.
pageAlignedSize( byteCount ) >= byteCount );
283 Assert.assertEquals( machine.
pageAlignedSize( byteCount ), bb.capacity() );
284 Assert.assertEquals( byteCount, bb.limit() );
286 for(i=byteCount-1; i>=0 && orig[i]==bb.get(i); i--) ;
287 Assert.assertTrue(
"Bytes not equal orig vs array", 0>i);
288 System.err.println(getTestMethodName()+
" OK: ["+skipBytes+
".."+(skipBytes+byteCount)+
"): "+bb);
293 final URLConnection urlConn =
IOUtil.
getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
294 Assert.assertNotNull(urlConn);
295 final BufferedInputStream bis =
new BufferedInputStream( urlConn.getInputStream() );
297 final int skipBytes = orig.length / 2;
298 final int byteCount = orig.length / 4;
299 Assert.assertTrue( orig.length >= skipBytes + byteCount );
300 Assert.assertTrue( orig.length >= byteCount );
306 Assert.assertTrue( machine.
pageAlignedSize( byteCount ) >= byteCount );
307 Assert.assertEquals( machine.
pageAlignedSize( byteCount ), bb.capacity() );
308 Assert.assertEquals( byteCount, bb.limit() );
310 for(i=byteCount-1; i>=0 && orig[skipBytes+i]==bb.get(i); i--) ;
311 Assert.assertTrue(
"Bytes not equal orig vs array", 0>i);
312 System.err.println(getTestMethodName()+
" OK: ["+skipBytes+
".."+(skipBytes+byteCount)+
"): "+bb);
317 final URLConnection urlConn =
IOUtil.
getResource(tfilename, this.getClass().getClassLoader(), this.getClass());
318 Assert.assertNotNull(urlConn);
319 final BufferedInputStream bis =
new BufferedInputStream( urlConn.getInputStream() );
321 final int skipBytes = orig.length * 2;
322 final int byteCount = orig.length;
323 Assert.assertTrue( orig.length < skipBytes + byteCount );
324 Assert.assertTrue( orig.length >= byteCount );
330 Assert.assertTrue( machine.
pageAlignedSize( byteCount ) >= byteCount );
331 Assert.assertEquals( machine.
pageAlignedSize( byteCount ), bb.capacity() );
332 Assert.assertEquals( 0, bb.limit() );
333 System.err.println(getTestMethodName()+
" OK: ["+skipBytes+
".."+(skipBytes+byteCount)+
"): "+bb);
336 public static void main(
final String args[])
throws IOException {
338 org.junit.runner.JUnitCore.
main(tstname);
static void dumpThrowable(final String additionalDescr, final Throwable t)
Dumps a Throwable to System.err in a decorating message including the current thread name,...
Machine data description for alignment and size onle, see com.jogamp.gluegen.
int pageAlignedSize(final int size)
static String cleanPathString(String path)
static URLConnection getResource(final String resourcePath, final ClassLoader classLoader, final Class<?> relContext)
Locating a resource using getResource(String, ClassLoader):
static ByteBuffer copyStream2ByteBuffer(final InputStream stream)
Copy the complete specified input stream to a NIO ByteBuffer w/ native byte order,...
static void close(final Closeable stream, final boolean throwRuntimeException)
static ByteBuffer copyStreamChunk2ByteBuffer(InputStream stream, int skipBytes, int byteCount)
Copy the specified input stream chunk to a NIO ByteBuffer w/ native byte order, which is being return...
static int copyURLConn2File(final URLConnection conn, final File outFile)
Copy the complete specified URL resource to the specified output file.
static byte[] copyStream2ByteArray(InputStream stream)
Copy the complete specified input stream to a byte array, which is being returned.
void test21CopyStreamChunk01Buffer()
void test22CopyStreamChunk02Buffer()
void test01CleanPathString()
void test11CopyStream01Array()
void test24CopyStreamChunk04Buffer()
void test25CopyStreamChunk05Buffer()
void test13CopyStream03Buffer()
void test12CopyStream02Buffer()
void test23CopyStreamChunk03Buffer()
static void main(final String args[])