1 32 package com.imagero.uio.buffer.arrays; 33 34 import com.imagero.uio.RandomAccessByteArray; 35 import com.imagero.uio.RandomAccessFactory; 36 import com.imagero.uio.RandomAccessRO; 37 import com.imagero.uio.Sys; 38 import com.imagero.uio.io.BitInputStream; 39 import com.imagero.uio.io.RLE4InputStream; 40 import com.imagero.uio.io.RLE8InputStream; 41 import com.imagero.uio.io.RLEInputStream; 42 43 import java.io.ByteArrayInputStream ; 44 import java.io.ByteArrayOutputStream ; 45 import java.io.DataInputStream ; 46 import java.io.DataOutputStream ; 47 import java.io.IOException ; 48 import java.util.StringTokenizer ; 49 50 55 public class ArraysTest extends junit.framework.TestCase { 56 57 public static boolean UNIT_TEST = false; 58 59 public ArraysTest() { 60 super("ArraysTest"); 61 } 62 63 public void testInt() throws IOException { 64 if (UNIT_TEST) { 65 Sys.out.println("testInt"); 66 int unitSize = 4; 67 int[] a = new int[2]; 68 byte[] b = new byte[]{(byte) 0xEE, (byte) 0x0, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88, 0x77}; 69 70 DataInputStream dis = new DataInputStream (new ByteArrayInputStream (b)); 72 for (int i = 0; i < a.length; i++) { 73 a[i] = dis.readInt(); 74 } 75 76 IntArrayBufferManager iadsm = new IntArrayBufferManager(a, a.length); 78 79 byte[] bLE = new byte[unitSize * a.length]; 81 iadsm.readLE(bLE, 0); 82 83 RandomAccessRO r0 = new RandomAccessByteArray(bLE, RandomAccessFactory.LITTLE_ENDIAN); 85 int[] aLE = new int[a.length]; 86 r0.readFully(aLE); 87 88 for (int i = 0; i < aLE.length; i++) { 89 assertEquals(a[i], aLE[i]); 90 } 91 92 byte[] bBE = new byte[unitSize * a.length]; 94 iadsm.readBE(bBE, 0); 95 96 RandomAccessRO r1 = new RandomAccessByteArray(bBE, RandomAccessFactory.BIG_ENDIAN); 98 int[] aBE = new int[a.length]; 99 r1.readFully(aBE); 100 101 for (int i = 0; i < aBE.length; i++) { 102 assertEquals(a[i], aBE[i]); 103 } 104 } 105 } 106 107 public void testChar() throws IOException { 108 if (UNIT_TEST) { 109 Sys.out.println("testChar"); 110 int unitSize = 2; 111 char[] a = new char[4]; 112 byte[] b = new byte[]{(byte) 0xEE, (byte) 0xDD, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88, 0x77}; 113 114 DataInputStream dis = new DataInputStream (new ByteArrayInputStream (b)); 116 for (int i = 0; i < a.length; i++) { 117 a[i] = dis.readChar(); 118 } 119 120 CharArrayBufferManager iadsm = new CharArrayBufferManager(a, a.length); 122 123 byte[] bLE = new byte[unitSize * a.length]; 125 iadsm.readLE(bLE, 0); 126 127 RandomAccessRO r0 = new RandomAccessByteArray(bLE, RandomAccessFactory.LITTLE_ENDIAN); 129 char[] aLE = new char[a.length]; 130 r0.readFully(aLE); 131 132 for (int i = 0; i < aLE.length; i++) { 133 assertEquals(a[i], aLE[i]); 134 } 135 136 byte[] bBE = new byte[unitSize * a.length]; 138 iadsm.readBE(bBE, 0); 139 140 RandomAccessRO r1 = new RandomAccessByteArray(bBE, RandomAccessFactory.BIG_ENDIAN); 142 char[] aBE = new char[a.length]; 143 r1.readFully(aBE); 144 145 for (int i = 0; i < aBE.length; i++) { 146 assertEquals(a[i], aBE[i]); 147 } 148 } 149 } 150 151 public void testShort() throws IOException { 152 if (UNIT_TEST) { 153 Sys.out.println("testShort"); 154 int unitSize = 2; 155 short[] a = new short[4]; 156 byte[] b = new byte[]{(byte) 0xEE, (byte) 0xDD, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88, 0x77}; 157 158 DataInputStream dis = new DataInputStream (new ByteArrayInputStream (b)); 160 for (int i = 0; i < a.length; i++) { 161 a[i] = dis.readShort(); 162 } 163 164 ShortArrayBufferManager iadsm = new ShortArrayBufferManager(a, a.length); 166 167 byte[] bLE = new byte[unitSize * a.length]; 169 iadsm.readLE(bLE, 0); 170 171 RandomAccessRO r0 = new RandomAccessByteArray(bLE, RandomAccessFactory.LITTLE_ENDIAN); 173 short[] aLE = new short[a.length]; 174 r0.readFully(aLE); 175 176 for (int i = 0; i < aLE.length; i++) { 177 assertEquals(a[i], aLE[i]); 178 } 179 180 byte[] bBE = new byte[unitSize * a.length]; 182 iadsm.readBE(bBE, 0); 183 184 RandomAccessRO r1 = new RandomAccessByteArray(bBE, RandomAccessFactory.BIG_ENDIAN); 186 short[] aBE = new short[a.length]; 187 r1.readFully(aBE); 188 189 for (int i = 0; i < aBE.length; i++) { 190 assertEquals(a[i], aBE[i]); 191 } 192 } 193 } 194 195 public void testDouble() throws IOException { 196 if (UNIT_TEST) { 197 Sys.out.println("testDouble"); 198 int unitSize = 8; 199 double[] a = new double[]{2.2d, 3.3d}; 200 byte[] b; 201 202 final ByteArrayOutputStream bout = new ByteArrayOutputStream (); 203 DataOutputStream dos = new DataOutputStream (bout); 204 dos.writeDouble(a[0]); 205 dos.writeDouble(a[1]); 206 dos.flush(); 207 dos.close(); 208 b = bout.toByteArray(); 209 210 DataInputStream dis = new DataInputStream (new ByteArrayInputStream (b)); 211 for (int i = 0; i < a.length; i++) { 212 a[i] = dis.readDouble(); 213 } 214 215 DoubleArrayBufferManager iadsm = new DoubleArrayBufferManager(a, a.length); 216 217 byte[] b1 = new byte[unitSize * a.length]; 218 iadsm.readBE(b1, 0); 219 220 RandomAccessRO r0 = new RandomAccessByteArray(b1, RandomAccessFactory.BIG_ENDIAN); 221 double[] aBE = new double[a.length]; 222 r0.readFully(aBE); 223 224 for (int i = 0; i < aBE.length; i++) { 225 assertTrue(a[i] == aBE[i]); 226 } 227 228 byte[] b2 = new byte[unitSize * a.length]; 229 iadsm.readLE(b2, 0); 230 231 RandomAccessRO r01 = new RandomAccessByteArray(b2, RandomAccessFactory.LITTLE_ENDIAN); 232 double[] aLE = new double[a.length]; 233 r01.readFully(aLE); 234 235 for (int i = 0; i < aLE.length; i++) { 236 assertTrue(a[i] == aLE[i]); 237 } 238 } 239 } 240 241 public void testFloat() throws IOException { 242 if (UNIT_TEST) { 243 Sys.out.println("testFloat"); 244 int unitSize = 4; 245 float[] a = new float[]{2.2f, 3.3f}; 246 byte[] b; 247 248 final ByteArrayOutputStream bout = new ByteArrayOutputStream (); 249 DataOutputStream dos = new DataOutputStream (bout); 250 dos.writeFloat(a[0]); 251 dos.writeFloat(a[1]); 252 dos.flush(); 253 dos.close(); 254 b = bout.toByteArray(); 255 256 DataInputStream dis = new DataInputStream (new ByteArrayInputStream (b)); 257 for (int i = 0; i < a.length; i++) { 258 a[i] = dis.readFloat(); 259 } 260 261 FloatArrayBufferManager iadsm = new FloatArrayBufferManager(a, a.length); 262 263 byte[] b1 = new byte[unitSize * a.length]; 264 iadsm.readBE(b1, 0); 265 266 RandomAccessRO r0 = new RandomAccessByteArray(b1, RandomAccessFactory.BIG_ENDIAN); 267 float[] aLE = new float[a.length]; 268 r0.readFully(aLE); 269 270 for (int i = 0; i < aLE.length; i++) { 271 assertTrue(a[i] == aLE[i]); 272 } 273 274 byte[] b2 = new byte[unitSize * a.length]; 275 iadsm.readLE(b2, 0); 276 277 RandomAccessRO r01 = new RandomAccessByteArray(b2, RandomAccessFactory.LITTLE_ENDIAN); 278 float[] aBE = new float[a.length]; 279 r01.readFully(aBE); 280 281 for (int i = 0; i < aBE.length; i++) { 282 assertTrue(a[i] == aBE[i]); 283 } 284 } 285 } 286 287 public void testLong() throws IOException { 288 if (UNIT_TEST) { 289 Sys.out.println("testLong"); 290 int unitSize = 8; 291 long[] a = new long[2]; 292 byte[] b = new byte[]{ 293 (byte) 0xEE, (byte) 0xDD, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88, 0x77, 294 (byte) 0xEE, (byte) 0xDD, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88, 0x77}; 295 296 DataInputStream dis = new DataInputStream (new ByteArrayInputStream (b)); 297 a[0] = dis.readLong(); 298 a[1] = dis.readLong(); 299 300 LongArrayBufferManager iadsm = new LongArrayBufferManager(a, a.length); 301 302 byte[] bLE = new byte[unitSize * a.length]; 303 iadsm.readLE(bLE, 0); 304 305 RandomAccessRO r0 = new RandomAccessByteArray(bLE, RandomAccessFactory.LITTLE_ENDIAN); 306 long[] aLE = new long[a.length]; 307 r0.readFully(aLE); 308 309 for (int i = 0; i < aLE.length; i++) { 310 assertEquals(a[i], aLE[i]); 311 } 312 313 byte[] bBE = new byte[unitSize * a.length]; 314 iadsm.readBE(bBE, 0); 315 316 RandomAccessRO r1 = new RandomAccessByteArray(bBE, RandomAccessFactory.BIG_ENDIAN); 317 long[] aBE = new long[a.length]; 318 r1.readFully(aBE); 319 320 for (int i = 0; i < aBE.length; i++) { 321 assertEquals(a[i], aBE[i]); 322 } 323 } 324 } 325 326 public void testRLE8() { 327 if (UNIT_TEST) { 328 Sys.out.println("testRLE8"); 329 330 String c = 331 "0F FF 00 00 " + 332 "02 FF 09 00 04 FF 00 00 " + 333 "04 FF 03 00 03 FF 02 00 03 FF 00 00 " + 334 "04 FF 03 00 04 FF 02 00 02 FF 00 00 " + 335 "04 FF 03 00 04 FF 02 00 02 FF 00 00 " + 336 "04 FF 03 00 04 FF 02 00 02 FF 00 00 " + 337 "04 FF 03 00 03 FF 02 00 03 FF 00 00 " + 338 "04 FF 03 00 01 FF 03 00 04 FF 00 00 " + 339 "04 FF 03 00 01 FF 03 00 04 FF 00 00 " + 340 "04 FF 03 00 03 FF 02 00 03 FF 00 00 " + 341 "04 FF 03 00 04 FF 02 00 02 FF 00 00 " + 342 "04 FF 03 00 04 FF 02 00 02 FF 00 00 " + 343 "04 FF 03 00 03 FF 03 00 02 FF 00 00 " + 344 "02 FF 0A 00 03 FF 00 00 " + 345 "0F FF 00 00 00 01"; 346 347 String d = 348 "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF " + 349 "FF FF 00 00 00 00 00 00 00 00 00 FF FF FF FF " + 350 "FF FF FF FF 00 00 00 FF FF FF 00 00 FF FF FF " + 351 "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " + 352 "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " + 353 "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " + 354 "FF FF FF FF 00 00 00 FF FF FF 00 00 FF FF FF " + 355 "FF FF FF FF 00 00 00 FF 00 00 00 FF FF FF FF " + 356 "FF FF FF FF 00 00 00 FF 00 00 00 FF FF FF FF " + 357 "FF FF FF FF 00 00 00 FF FF FF 00 00 FF FF FF " + 358 "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " + 359 "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " + 360 "FF FF FF FF 00 00 00 FF FF FF 00 00 00 FF FF " + 361 "FF FF 00 00 00 00 00 00 00 00 00 00 FF FF FF " + 362 "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF "; 363 364 ByteArrayOutputStream cout = new ByteArrayOutputStream (); 365 StringTokenizer cst = new StringTokenizer (c, " ", false); 366 while (cst.hasMoreElements()) { 367 String s = cst.nextToken(); 368 cout.write(Integer.parseInt(s, 16)); 369 } 370 byte[] cb = cout.toByteArray(); 371 372 ByteArrayOutputStream dout = new ByteArrayOutputStream (); 373 StringTokenizer dst = new StringTokenizer (d, " ", false); 374 while (dst.hasMoreElements()) { 375 String s = dst.nextToken(); 376 dout.write(Integer.parseInt(s, 16)); 377 } 378 byte[] db = dout.toByteArray(); 379 380 ByteArrayOutputStream eout = new ByteArrayOutputStream (); 381 382 RLE8InputStream in = new RLE8InputStream(new ByteArrayInputStream (cb)); 383 try { 384 while (in.available() > 0) { 385 try { 386 int a = in.read() & 0xFF; 387 eout.write(a); 388 } 389 catch (RLE8InputStream.EndOfLineException ex) { 390 continue; 391 } 392 catch (RLE8InputStream.EndOfBitmapException ex) { 393 break; 394 } 395 } 396 } 397 catch (IOException ex) { 398 ex.printStackTrace(); 399 } 400 byte[] eb = eout.toByteArray(); 401 402 for (int i = 0; i < eb.length; i++) { 403 assertTrue(eb[i] == db[i]); 404 } 405 } 406 } 407 408 public void testRLE4() { 409 if (UNIT_TEST) { 410 Sys.out.println("testRLE8"); 411 412 String c = 413 "0F 11 00 00" + 414 "02 11 09 00 04 11 00 00 " + 415 "04 11 03 00 03 11 02 00 03 11 00 00" + 416 "04 11 03 00 04 11 02 00 02 11 00 00" + 417 "04 11 03 00 04 11 02 00 02 11 00 00" + 418 "04 11 03 00 04 11 02 00 02 11 00 00" + 419 "04 11 03 00 03 11 02 00 03 11 00 00" + 420 "04 11 03 00 01 11 03 00 04 11 00 00" + 421 "04 11 03 00 01 11 03 00 04 11 00 00" + 422 "04 11 03 00 03 11 02 00 03 11 00 00" + 423 "04 11 03 00 04 11 02 00 02 11 00 00" + 424 "04 11 03 00 04 11 02 00 02 11 00 00" + 425 "04 11 03 00 03 11 03 00 02 11 00 00" + 426 "02 11 0A 00 03 11 00 00" + 427 "0F 11 00 00 00 01"; 428 429 String d = 430 "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF " + 431 "FF FF 00 00 00 00 00 00 00 00 00 FF FF FF FF " + 432 "FF FF FF FF 00 00 00 FF FF FF 00 00 FF FF FF " + 433 "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " + 434 "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " + 435 "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " + 436 "FF FF FF FF 00 00 00 FF FF FF 00 00 FF FF FF " + 437 "FF FF FF FF 00 00 00 FF 00 00 00 FF FF FF FF " + 438 "FF FF FF FF 00 00 00 FF 00 00 00 FF FF FF FF " + 439 "FF FF FF FF 00 00 00 FF FF FF 00 00 FF FF FF " + 440 "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " + 441 "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " + 442 "FF FF FF FF 00 00 00 FF FF FF 00 00 00 FF FF " + 443 "FF FF 00 00 00 00 00 00 00 00 00 00 FF FF FF " + 444 "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF "; 445 446 ByteArrayOutputStream cout = new ByteArrayOutputStream (); 447 StringTokenizer cst = new StringTokenizer (c, " ", false); 448 while (cst.hasMoreElements()) { 449 String s = cst.nextToken(); 450 cout.write(Integer.parseInt(s, 16)); 451 } 452 byte[] cb = cout.toByteArray(); 453 454 ByteArrayOutputStream dout = new ByteArrayOutputStream (); 455 StringTokenizer dst = new StringTokenizer (d, " ", false); 456 while (dst.hasMoreElements()) { 457 String s = dst.nextToken(); 458 dout.write(Integer.parseInt(s, 16)); 459 } 460 byte[] db = dout.toByteArray(); 461 462 ByteArrayOutputStream eout = new ByteArrayOutputStream (); 463 464 RLEInputStream in = new RLE4InputStream(new BitInputStream(new ByteArrayInputStream (cb))); 465 try { 466 while (in.available() > 0) { 467 try { 468 int a = in.read() & 0xFF; 469 eout.write(a); 470 } 471 catch (RLE4InputStream.EndOfLineException ex) { 472 continue; 473 } 474 catch (RLE4InputStream.EndOfBitmapException ex) { 475 break; 476 } 477 } 478 } 479 catch (IOException ex) { 480 ex.printStackTrace(); 481 } 482 byte[] eb = eout.toByteArray(); 483 484 for (int i = 0; i < eb.length; i++) { 485 assertTrue(eb[i] == db[i]); 486 } 487 } 488 } 489 } 490 | Popular Tags |