View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.buffer;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNotSame;
25  import static org.junit.Assert.assertSame;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.fail;
28  
29  import java.nio.BufferOverflowException;
30  import java.nio.ByteBuffer;
31  import java.nio.ByteOrder;
32  import java.nio.ReadOnlyBufferException;
33  import java.nio.charset.CharacterCodingException;
34  import java.nio.charset.Charset;
35  import java.nio.charset.CharsetDecoder;
36  import java.nio.charset.CharsetEncoder;
37  import java.util.ArrayList;
38  import java.util.Date;
39  import java.util.EnumSet;
40  import java.util.List;
41  
42  import org.apache.mina.util.Bar;
43  import org.junit.Test;
44  
45  /**
46   * Tests {@link IoBuffer}.
47   * 
48   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
49   */
50  public class IoBufferTest {
51  
52      private static interface NonserializableInterface {
53      }
54  
55      public static class NonserializableClass {
56      }
57  
58      @Test
59      public void testNormalizeCapacity() {
60          // A few sanity checks
61          assertEquals(Integer.MAX_VALUE, IoBufferImpl.normalizeCapacity(-10));
62          assertEquals(0, IoBufferImpl.normalizeCapacity(0));
63          assertEquals(Integer.MAX_VALUE, IoBufferImpl.normalizeCapacity(Integer.MAX_VALUE));
64          assertEquals(Integer.MAX_VALUE, IoBufferImpl.normalizeCapacity(Integer.MIN_VALUE));
65          assertEquals(Integer.MAX_VALUE, IoBufferImpl.normalizeCapacity(Integer.MAX_VALUE - 10));
66  
67          // A sanity check test for all the powers of 2
68          for (int i = 0; i < 30; i++) {
69              int n = 1 << i;
70  
71              assertEquals(n, IoBufferImpl.normalizeCapacity(n));
72  
73              if (i > 1) {
74                  // test that n - 1 will be normalized to n (notice that n = 2^i)
75                  assertEquals(n, IoBufferImpl.normalizeCapacity(n - 1));
76              }
77  
78              // test that n + 1 will be normalized to 2^(i + 1)
79              assertEquals(n << 1, IoBufferImpl.normalizeCapacity(n + 1));
80          }
81  
82          // The first performance test measures the time to normalize integers
83          // from 0 to 2^27 (it tests 2^27 integers)
84          long time = System.currentTimeMillis();
85  
86          for (int i = 0; i < 1 << 27; i++) {
87              int n = IoBufferImpl.normalizeCapacity(i);
88  
89              // do a simple superfluous test to prevent possible compiler or JVM
90              // optimizations of not executing non used code/variables
91              if (n == -1) {
92                  System.out.println("n should never be -1");
93              }
94          }
95  
96          long time2 = System.currentTimeMillis();
97          //System.out.println("Time for performance test 1: " + (time2 - time) + "ms");
98  
99          // The second performance test measures the time to normalize integers
100         // from Integer.MAX_VALUE to Integer.MAX_VALUE - 2^27 (it tests 2^27
101         // integers)
102         time = System.currentTimeMillis();
103         for (int i = Integer.MAX_VALUE; i > Integer.MAX_VALUE - (1 << 27); i--) {
104             int n = IoBufferImpl.normalizeCapacity(i);
105 
106             // do a simple superfluous test to prevent possible compiler or JVM
107             // optimizations of not executing non used code/variables
108             if (n == -1) {
109                 System.out.println("n should never be -1");
110             }
111         }
112 
113         time2 = System.currentTimeMillis();
114         //System.out.println("Time for performance test 2: " + (time2 - time) + "ms");
115     }
116     
117     @Test 
118     public void autoExpand() { 
119         IoBuffer buffer = IoBuffer.allocate(8, false); 
120         buffer.setAutoExpand(true); 
121          
122         assertTrue("Should AutoExpand", buffer.isAutoExpand()); 
123          
124         IoBuffer slice = buffer.slice(); 
125         assertFalse("Should *NOT* AutoExpand", buffer.isAutoExpand()); 
126         assertFalse("Should *NOT* AutoExpand", slice.isAutoExpand()); 
127     } 
128 
129     /**
130      * This class extends the AbstractIoBuffer class to have direct access to
131      * the protected IoBuffer.normalizeCapacity() method and to expose it for
132      * the tests.
133      */
134     private static class IoBufferImpl extends AbstractIoBuffer {
135 
136         public static int normalizeCapacity(int requestedCapacity) {
137             return IoBuffer.normalizeCapacity(requestedCapacity);
138         }
139 
140         protected IoBufferImpl(AbstractIoBuffer parent) {
141             super(parent);
142         }
143 
144         protected IoBuffer asReadOnlyBuffer0() {
145             return null;
146         }
147 
148         protected void buf(ByteBuffer newBuf) {
149         }
150 
151         protected IoBuffer duplicate0() {
152             return null;
153         }
154 
155         protected IoBuffer slice0() {
156             return null;
157         }
158 
159         public byte[] array() {
160             return null;
161         }
162 
163         public int arrayOffset() {
164             return 0;
165         }
166 
167         public ByteBuffer buf() {
168             return null;
169         }
170 
171         public void free() {
172         }
173 
174         public boolean hasArray() {
175             return false;
176         }
177 
178     }
179 
180     @Test
181     public void testObjectSerialization() throws Exception {
182         IoBuffer buf = IoBuffer.allocate(16);
183         buf.setAutoExpand(true);
184         List<Object> o = new ArrayList<Object>();
185         o.add(new Date());
186         o.add(long.class);
187 
188         // Test writing an object.
189         buf.putObject(o);
190 
191         // Test reading an object.
192         buf.clear();
193         Object o2 = buf.getObject();
194         assertEquals(o, o2);
195 
196         // This assertion is just to make sure that deserialization occurred.
197         assertNotSame(o, o2);
198     }
199     
200     @Test
201     public void testNonserializableClass() throws Exception {
202         Class<?> c = NonserializableClass.class;
203 
204         IoBuffer buffer = IoBuffer.allocate(16);
205         buffer.setAutoExpand(true);
206         buffer.putObject(c);
207 
208         buffer.flip();
209         Object o = buffer.getObject();
210 
211         assertEquals(c, o);
212         assertSame(c, o);
213     }
214     
215     @Test
216     public void testNonserializableInterface() throws Exception {
217         Class<?> c = NonserializableInterface.class;
218 
219         IoBuffer buffer = IoBuffer.allocate(16);
220         buffer.setAutoExpand(true);
221         buffer.putObject(c);
222 
223         buffer.flip();
224         Object o = buffer.getObject();
225 
226         assertEquals(c, o);
227         assertSame(c, o);
228     }
229     
230     @Test
231     public void testAllocate() throws Exception {
232         for (int i = 10; i < 1048576 * 2; i = i * 11 / 10) // increase by 10%
233         {
234             IoBuffer buf = IoBuffer.allocate(i);
235             assertEquals(0, buf.position());
236             assertEquals(buf.capacity(), buf.remaining());
237             assertTrue(buf.capacity() >= i);
238             assertTrue(buf.capacity() < i * 2);
239         }
240     }
241 
242     @Test
243     public void testAutoExpand() throws Exception {
244         IoBuffer buf = IoBuffer.allocate(1);
245 
246         buf.put((byte) 0);
247         try {
248             buf.put((byte) 0);
249             fail("Buffer can't auto expand, with autoExpand property set at false");
250         } catch (BufferOverflowException e) {
251             // Expected Exception as auto expand property is false
252             assertTrue(true);
253         }
254 
255         buf.setAutoExpand(true);
256         buf.put((byte) 0);
257         assertEquals(2, buf.position());
258         assertEquals(2, buf.limit());
259         assertEquals(2, buf.capacity());
260 
261         buf.setAutoExpand(false);
262         try {
263             buf.put(3, (byte) 0);
264             fail("Buffer can't auto expand, with autoExpand property set at false");
265         } catch (IndexOutOfBoundsException e) {
266             // Expected Exception as auto expand property is false
267             assertTrue(true);
268         }
269 
270         buf.setAutoExpand(true);
271         buf.put(3, (byte) 0);
272         assertEquals(2, buf.position());
273         assertEquals(4, buf.limit());
274         assertEquals(4, buf.capacity());
275 
276         // Make sure the buffer is doubled up.
277         buf = IoBuffer.allocate(1).setAutoExpand(true);
278         int lastCapacity = buf.capacity();
279         for (int i = 0; i < 1048576; i ++) {
280             buf.put((byte) 0);
281             if (lastCapacity != buf.capacity()) {
282                 assertEquals(lastCapacity * 2, buf.capacity());
283                 lastCapacity = buf.capacity();
284             }
285         }
286     }
287 
288     @Test
289     public void testAutoExpandMark() throws Exception {
290         IoBuffer buf = IoBuffer.allocate(4).setAutoExpand(true);
291 
292         buf.put((byte) 0);
293         buf.put((byte) 0);
294         buf.put((byte) 0);
295 
296         // Position should be 3 when we reset this buffer.
297         buf.mark();
298 
299         // Overflow it
300         buf.put((byte) 0);
301         buf.put((byte) 0);
302 
303         assertEquals(5, buf.position());
304         buf.reset();
305         assertEquals(3, buf.position());
306     }
307 
308     @Test
309     public void testAutoShrink() throws Exception {
310         IoBuffer buf = IoBuffer.allocate(8).setAutoShrink(true);
311 
312         // Make sure the buffer doesn't shrink too much (less than the initial
313         // capacity.)
314         buf.sweep((byte) 1);
315         buf.fill(7);
316         buf.compact();
317         assertEquals(8, buf.capacity());
318         assertEquals(1, buf.position());
319         assertEquals(8, buf.limit());
320         buf.clear();
321         assertEquals(1, buf.get());
322 
323         // Expand the buffer.
324         buf.capacity(32).clear();
325         assertEquals(32, buf.capacity());
326 
327         // Make sure the buffer shrinks when only 1/4 is being used.
328         buf.sweep((byte) 1);
329         buf.fill(24);
330         buf.compact();
331         assertEquals(16, buf.capacity());
332         assertEquals(8, buf.position());
333         assertEquals(16, buf.limit());
334         buf.clear();
335         for (int i = 0; i < 8; i ++) {
336             assertEquals(1, buf.get());
337         }
338 
339         // Expand the buffer.
340         buf.capacity(32).clear();
341         assertEquals(32, buf.capacity());
342 
343         // Make sure the buffer shrinks when only 1/8 is being used.
344         buf.sweep((byte) 1);
345         buf.fill(28);
346         buf.compact();
347         assertEquals(8, buf.capacity());
348         assertEquals(4, buf.position());
349         assertEquals(8, buf.limit());
350         buf.clear();
351         for (int i = 0; i < 4; i ++) {
352             assertEquals(1, buf.get());
353         }
354 
355         // Expand the buffer.
356         buf.capacity(32).clear();
357         assertEquals(32, buf.capacity());
358 
359         // Make sure the buffer shrinks when 0 byte is being used.
360         buf.fill(32);
361         buf.compact();
362         assertEquals(8, buf.capacity());
363         assertEquals(0, buf.position());
364         assertEquals(8, buf.limit());
365 
366         // Expand the buffer.
367         buf.capacity(32).clear();
368         assertEquals(32, buf.capacity());
369 
370         // Make sure the buffer doesn't shrink when more than 1/4 is being used.
371         buf.sweep((byte) 1);
372         buf.fill(23);
373         buf.compact();
374         assertEquals(32, buf.capacity());
375         assertEquals(9, buf.position());
376         assertEquals(32, buf.limit());
377         buf.clear();
378         for (int i = 0; i < 9; i ++) {
379             assertEquals(1, buf.get());
380         }
381     }
382 
383     @Test
384     public void testGetString() throws Exception {
385         IoBuffer buf = IoBuffer.allocate(16);
386         CharsetDecoder decoder;
387 
388         Charset charset = Charset.forName("UTF-8");
389         buf.clear();
390         buf.putString("hello", charset.newEncoder());
391         buf.put((byte) 0);
392         buf.flip();
393         assertEquals("hello", buf.getString(charset.newDecoder()));
394 
395         buf.clear();
396         buf.putString("hello", charset.newEncoder());
397         buf.flip();
398         assertEquals("hello", buf.getString(charset.newDecoder()));
399 
400         decoder = Charset.forName("ISO-8859-1").newDecoder();
401         buf.clear();
402         buf.put((byte) 'A');
403         buf.put((byte) 'B');
404         buf.put((byte) 'C');
405         buf.put((byte) 0);
406 
407         buf.position(0);
408         assertEquals("ABC", buf.getString(decoder));
409         assertEquals(4, buf.position());
410 
411         buf.position(0);
412         buf.limit(1);
413         assertEquals("A", buf.getString(decoder));
414         assertEquals(1, buf.position());
415 
416         buf.clear();
417         assertEquals("ABC", buf.getString(10, decoder));
418         assertEquals(10, buf.position());
419 
420         buf.clear();
421         assertEquals("A", buf.getString(1, decoder));
422         assertEquals(1, buf.position());
423 
424         // Test a trailing garbage
425         buf.clear();
426         buf.put((byte) 'A');
427         buf.put((byte) 'B');
428         buf.put((byte) 0);
429         buf.put((byte) 'C');
430         buf.position(0);
431         assertEquals("AB", buf.getString(4, decoder));
432         assertEquals(4, buf.position());
433 
434         buf.clear();
435         buf.fillAndReset(buf.limit());
436         decoder = Charset.forName("UTF-16").newDecoder();
437         buf.put((byte) 0);
438         buf.put((byte) 'A');
439         buf.put((byte) 0);
440         buf.put((byte) 'B');
441         buf.put((byte) 0);
442         buf.put((byte) 'C');
443         buf.put((byte) 0);
444         buf.put((byte) 0);
445 
446         buf.position(0);
447         assertEquals("ABC", buf.getString(decoder));
448         assertEquals(8, buf.position());
449 
450         buf.position(0);
451         buf.limit(2);
452         assertEquals("A", buf.getString(decoder));
453         assertEquals(2, buf.position());
454 
455         buf.position(0);
456         buf.limit(3);
457         assertEquals("A", buf.getString(decoder));
458         assertEquals(2, buf.position());
459 
460         buf.clear();
461         assertEquals("ABC", buf.getString(10, decoder));
462         assertEquals(10, buf.position());
463 
464         buf.clear();
465         assertEquals("A", buf.getString(2, decoder));
466         assertEquals(2, buf.position());
467 
468         buf.clear();
469         try {
470             buf.getString(1, decoder);
471             fail();
472         } catch (IllegalArgumentException e) {
473             // Expected an Exception, signifies test success
474             assertTrue(true);
475         }
476 
477         // Test getting strings from an empty buffer.
478         buf.clear();
479         buf.limit(0);
480         assertEquals("", buf.getString(decoder));
481         assertEquals("", buf.getString(2, decoder));
482 
483         // Test getting strings from non-empty buffer which is filled with 0x00
484         buf.clear();
485         buf.putInt(0);
486         buf.clear();
487         buf.limit(4);
488         assertEquals("", buf.getString(decoder));
489         assertEquals(2, buf.position());
490         assertEquals(4, buf.limit());
491 
492         buf.position(0);
493         assertEquals("", buf.getString(2, decoder));
494         assertEquals(2, buf.position());
495         assertEquals(4, buf.limit());
496     }
497 
498     @Test
499     public void testGetStringWithFailure() throws Exception {
500         String test = "\u30b3\u30e1\u30f3\u30c8\u7de8\u96c6";
501         IoBuffer buffer = IoBuffer.wrap(test.getBytes("Shift_JIS"));
502 
503         // Make sure the limit doesn't change when an exception arose.
504         int oldLimit = buffer.limit();
505         int oldPos = buffer.position();
506         try {
507             buffer.getString(3, Charset.forName("ASCII").newDecoder());
508             fail();
509         } catch (Exception e) {
510             assertEquals(oldLimit, buffer.limit());
511             assertEquals(oldPos, buffer.position());
512         }
513 
514         try {
515             buffer.getString(Charset.forName("ASCII").newDecoder());
516             fail();
517         } catch (Exception e) {
518             assertEquals(oldLimit, buffer.limit());
519             assertEquals(oldPos, buffer.position());
520         }
521     }
522 
523     @Test
524     public void testPutString() throws Exception {
525         CharsetEncoder encoder;
526         IoBuffer buf = IoBuffer.allocate(16);
527         encoder = Charset.forName("ISO-8859-1").newEncoder();
528 
529         buf.putString("ABC", encoder);
530         assertEquals(3, buf.position());
531         buf.clear();
532         assertEquals('A', buf.get(0));
533         assertEquals('B', buf.get(1));
534         assertEquals('C', buf.get(2));
535 
536         buf.putString("D", 5, encoder);
537         assertEquals(5, buf.position());
538         buf.clear();
539         assertEquals('D', buf.get(0));
540         assertEquals(0, buf.get(1));
541 
542         buf.putString("EFG", 2, encoder);
543         assertEquals(2, buf.position());
544         buf.clear();
545         assertEquals('E', buf.get(0));
546         assertEquals('F', buf.get(1));
547         assertEquals('C', buf.get(2)); // C may not be overwritten
548 
549         // UTF-16: We specify byte order to omit BOM.
550         encoder = Charset.forName("UTF-16BE").newEncoder();
551         buf.clear();
552 
553         buf.putString("ABC", encoder);
554         assertEquals(6, buf.position());
555         buf.clear();
556 
557         assertEquals(0, buf.get(0));
558         assertEquals('A', buf.get(1));
559         assertEquals(0, buf.get(2));
560         assertEquals('B', buf.get(3));
561         assertEquals(0, buf.get(4));
562         assertEquals('C', buf.get(5));
563 
564         buf.putString("D", 10, encoder);
565         assertEquals(10, buf.position());
566         buf.clear();
567         assertEquals(0, buf.get(0));
568         assertEquals('D', buf.get(1));
569         assertEquals(0, buf.get(2));
570         assertEquals(0, buf.get(3));
571 
572         buf.putString("EFG", 4, encoder);
573         assertEquals(4, buf.position());
574         buf.clear();
575         assertEquals(0, buf.get(0));
576         assertEquals('E', buf.get(1));
577         assertEquals(0, buf.get(2));
578         assertEquals('F', buf.get(3));
579         assertEquals(0, buf.get(4)); // C may not be overwritten
580         assertEquals('C', buf.get(5)); // C may not be overwritten
581 
582         // Test putting an emptry string
583         buf.putString("", encoder);
584         assertEquals(0, buf.position());
585         buf.putString("", 4, encoder);
586         assertEquals(4, buf.position());
587         assertEquals(0, buf.get(0));
588         assertEquals(0, buf.get(1));
589     }
590 
591     @Test
592     public void testGetPrefixedString() throws Exception {
593         IoBuffer buf = IoBuffer.allocate(16);
594         CharsetEncoder encoder;
595         CharsetDecoder decoder;
596         encoder = Charset.forName("ISO-8859-1").newEncoder();
597         decoder = Charset.forName("ISO-8859-1").newDecoder();
598 
599         buf.putShort((short) 3);
600         buf.putString("ABCD", encoder);
601         buf.clear();
602         assertEquals("ABC", buf.getPrefixedString(decoder));
603     }
604 
605     @Test
606     public void testPutPrefixedString() throws Exception {
607         CharsetEncoder encoder;
608         IoBuffer buf = IoBuffer.allocate(16);
609         buf.fillAndReset(buf.remaining());
610         encoder = Charset.forName("ISO-8859-1").newEncoder();
611 
612         // Without autoExpand
613         buf.putPrefixedString("ABC", encoder);
614         assertEquals(5, buf.position());
615         assertEquals(0, buf.get(0));
616         assertEquals(3, buf.get(1));
617         assertEquals('A', buf.get(2));
618         assertEquals('B', buf.get(3));
619         assertEquals('C', buf.get(4));
620 
621         buf.clear();
622         try {
623             buf.putPrefixedString("123456789012345", encoder);
624             fail();
625         } catch (BufferOverflowException e) {
626             // Expected an Exception, signifies test success
627             assertTrue(true);
628         }
629 
630         // With autoExpand
631         buf.clear();
632         buf.setAutoExpand(true);
633         buf.putPrefixedString("123456789012345", encoder);
634         assertEquals(17, buf.position());
635         assertEquals(0, buf.get(0));
636         assertEquals(15, buf.get(1));
637         assertEquals('1', buf.get(2));
638         assertEquals('2', buf.get(3));
639         assertEquals('3', buf.get(4));
640         assertEquals('4', buf.get(5));
641         assertEquals('5', buf.get(6));
642         assertEquals('6', buf.get(7));
643         assertEquals('7', buf.get(8));
644         assertEquals('8', buf.get(9));
645         assertEquals('9', buf.get(10));
646         assertEquals('0', buf.get(11));
647         assertEquals('1', buf.get(12));
648         assertEquals('2', buf.get(13));
649         assertEquals('3', buf.get(14));
650         assertEquals('4', buf.get(15));
651         assertEquals('5', buf.get(16));
652     }
653 
654     @Test
655     public void testPutPrefixedStringWithPrefixLength() throws Exception {
656         CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder();
657         IoBuffer buf = IoBuffer.allocate(16).sweep().setAutoExpand(true);
658 
659         buf.putPrefixedString("A", 1, encoder);
660         assertEquals(2, buf.position());
661         assertEquals(1, buf.get(0));
662         assertEquals('A', buf.get(1));
663 
664         buf.sweep();
665         buf.putPrefixedString("A", 2, encoder);
666         assertEquals(3, buf.position());
667         assertEquals(0, buf.get(0));
668         assertEquals(1, buf.get(1));
669         assertEquals('A', buf.get(2));
670 
671         buf.sweep();
672         buf.putPrefixedString("A", 4, encoder);
673         assertEquals(5, buf.position());
674         assertEquals(0, buf.get(0));
675         assertEquals(0, buf.get(1));
676         assertEquals(0, buf.get(2));
677         assertEquals(1, buf.get(3));
678         assertEquals('A', buf.get(4));
679     }
680 
681     @Test
682     public void testPutPrefixedStringWithPadding() throws Exception {
683         CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder();
684         IoBuffer buf = IoBuffer.allocate(16).sweep().setAutoExpand(true);
685 
686         buf.putPrefixedString("A", 1, 2, (byte) 32, encoder);
687         assertEquals(3, buf.position());
688         assertEquals(2, buf.get(0));
689         assertEquals('A', buf.get(1));
690         assertEquals(' ', buf.get(2));
691 
692         buf.sweep();
693         buf.putPrefixedString("A", 1, 4, (byte) 32, encoder);
694         assertEquals(5, buf.position());
695         assertEquals(4, buf.get(0));
696         assertEquals('A', buf.get(1));
697         assertEquals(' ', buf.get(2));
698         assertEquals(' ', buf.get(3));
699         assertEquals(' ', buf.get(4));
700     }
701 
702     @Test
703     public void testWideUtf8Characters() throws Exception {
704         Runnable r = new Runnable() {
705             public void run() {
706                 IoBuffer buffer = IoBuffer.allocate(1);
707                 buffer.setAutoExpand(true);
708 
709                 Charset charset = Charset.forName("UTF-8");
710 
711                 CharsetEncoder encoder = charset.newEncoder();
712 
713                 for (int i = 0; i < 5; i++) {
714                     try {
715                         buffer.putString("\u89d2", encoder);
716                         buffer.putPrefixedString("\u89d2", encoder);
717                     } catch (CharacterCodingException e) {
718                         fail(e.getMessage());
719                     }
720                 }
721             }
722         };
723 
724         Thread t = new Thread(r);
725         t.setDaemon(true);
726         t.start();
727 
728         for (int i = 0; i < 50; i++) {
729             Thread.sleep(100);
730             if (!t.isAlive()) {
731                 break;
732             }
733         }
734 
735         if (t.isAlive()) {
736             t.interrupt();
737 
738             fail("Went into endless loop trying to encode character");
739         }
740     }
741 
742     @Test
743     public void testInheritedObjectSerialization() throws Exception {
744         IoBuffer buf = IoBuffer.allocate(16);
745         buf.setAutoExpand(true);
746 
747         Bar expected = new Bar();
748         expected.setFooValue(0x12345678);
749         expected.setBarValue(0x90ABCDEF);
750 
751         // Test writing an object.
752         buf.putObject(expected);
753 
754         // Test reading an object.
755         buf.clear();
756         Bar actual = (Bar) buf.getObject();
757         assertSame(Bar.class, actual.getClass());
758         assertEquals(expected.getFooValue(), actual.getFooValue());
759         assertEquals(expected.getBarValue(), actual.getBarValue());
760 
761         // This assertion is just to make sure that deserialization occurred.
762         assertNotSame(expected, actual);
763     }
764 
765     @Test
766     public void testSweepWithZeros() throws Exception {
767         IoBuffer buf = IoBuffer.allocate(4);
768         buf.putInt(0xdeadbeef);
769         buf.clear();
770         assertEquals(0xdeadbeef, buf.getInt());
771         assertEquals(4, buf.position());
772         assertEquals(4, buf.limit());
773 
774         buf.sweep();
775         assertEquals(0, buf.position());
776         assertEquals(4, buf.limit());
777         assertEquals(0x0, buf.getInt());
778     }
779 
780     @Test
781     public void testSweepNonZeros() throws Exception {
782         IoBuffer buf = IoBuffer.allocate(4);
783         buf.putInt(0xdeadbeef);
784         buf.clear();
785         assertEquals(0xdeadbeef, buf.getInt());
786         assertEquals(4, buf.position());
787         assertEquals(4, buf.limit());
788 
789         buf.sweep((byte) 0x45);
790         assertEquals(0, buf.position());
791         assertEquals(4, buf.limit());
792         assertEquals(0x45454545, buf.getInt());
793     }
794 
795     @Test
796     public void testWrapNioBuffer() throws Exception {
797         ByteBuffer nioBuf = ByteBuffer.allocate(10);
798         nioBuf.position(3);
799         nioBuf.limit(7);
800 
801         IoBuffer buf = IoBuffer.wrap(nioBuf);
802         assertEquals(3, buf.position());
803         assertEquals(7, buf.limit());
804         assertEquals(10, buf.capacity());
805     }
806 
807     @Test
808     public void testWrapSubArray() throws Exception {
809         byte[] array = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
810 
811         IoBuffer buf = IoBuffer.wrap(array, 3, 4);
812         assertEquals(3, buf.position());
813         assertEquals(7, buf.limit());
814         assertEquals(10, buf.capacity());
815 
816         buf.clear();
817         assertEquals(0, buf.position());
818         assertEquals(10, buf.limit());
819         assertEquals(10, buf.capacity());
820     }
821 
822     @Test
823     public void testDuplicate() throws Exception {
824         IoBuffer original;
825         IoBuffer duplicate;
826 
827         // Test if the buffer is duplicated correctly.
828         original = IoBuffer.allocate(16).sweep();
829         original.position(4);
830         original.limit(10);
831         duplicate = original.duplicate();
832         original.put(4, (byte) 127);
833         assertEquals(4, duplicate.position());
834         assertEquals(10, duplicate.limit());
835         assertEquals(16, duplicate.capacity());
836         assertNotSame(original.buf(), duplicate.buf());
837         assertSame(original.buf().array(), duplicate.buf().array());
838         assertEquals(127, duplicate.get(4));
839 
840         // Test a duplicate of a duplicate.
841         original = IoBuffer.allocate(16);
842         duplicate = original.duplicate().duplicate();
843         assertNotSame(original.buf(), duplicate.buf());
844         assertSame(original.buf().array(), duplicate.buf().array());
845 
846         // Try to expand.
847         original = IoBuffer.allocate(16);
848         original.setAutoExpand(true);
849         duplicate = original.duplicate();
850         assertFalse(original.isAutoExpand());
851 
852         try {
853             original.setAutoExpand(true);
854             fail("Derived buffers and their parent can't be expanded");
855         } catch (IllegalStateException e) {
856             // Expected an Exception, signifies test success
857             assertTrue(true);
858         }
859 
860         try {
861             duplicate.setAutoExpand(true);
862             fail("Derived buffers and their parent can't be expanded");
863         } catch (IllegalStateException e) {
864             // Expected an Exception, signifies test success
865             assertTrue(true);
866         }
867     }
868 
869     @Test
870     public void testSlice() throws Exception {
871         IoBuffer original;
872         IoBuffer slice;
873 
874         // Test if the buffer is sliced correctly.
875         original = IoBuffer.allocate(16).sweep();
876         original.position(4);
877         original.limit(10);
878         slice = original.slice();
879         original.put(4, (byte) 127);
880         assertEquals(0, slice.position());
881         assertEquals(6, slice.limit());
882         assertEquals(6, slice.capacity());
883         assertNotSame(original.buf(), slice.buf());
884         assertEquals(127, slice.get(0));
885     }
886 
887     @Test
888     public void testReadOnlyBuffer() throws Exception {
889         IoBuffer original;
890         IoBuffer duplicate;
891 
892         // Test if the buffer is duplicated correctly.
893         original = IoBuffer.allocate(16).sweep();
894         original.position(4);
895         original.limit(10);
896         duplicate = original.asReadOnlyBuffer();
897         original.put(4, (byte) 127);
898         assertEquals(4, duplicate.position());
899         assertEquals(10, duplicate.limit());
900         assertEquals(16, duplicate.capacity());
901         assertNotSame(original.buf(), duplicate.buf());
902         assertEquals(127, duplicate.get(4));
903 
904         // Try to expand.
905         try {
906             original = IoBuffer.allocate(16);
907             duplicate = original.asReadOnlyBuffer();
908             duplicate.putString("A very very very very looooooong string",
909                     Charset.forName("ISO-8859-1").newEncoder());
910             fail("ReadOnly buffer's can't be expanded");
911         } catch (ReadOnlyBufferException e) {
912             // Expected an Exception, signifies test success
913             assertTrue(true);
914         }
915     }
916 
917     @Test
918     public void testGetUnsigned() throws Exception {
919         IoBuffer buf = IoBuffer.allocate(16);
920         buf.put((byte) 0xA4);
921         buf.put((byte) 0xD0);
922         buf.put((byte) 0xB3);
923         buf.put((byte) 0xCD);
924         buf.flip();
925 
926         buf.order(ByteOrder.LITTLE_ENDIAN);
927 
928         buf.mark();
929         assertEquals(0xA4, buf.getUnsigned());
930         buf.reset();
931         assertEquals(0xD0A4, buf.getUnsignedShort());
932         buf.reset();
933         assertEquals(0xCDB3D0A4L, buf.getUnsignedInt());
934     }
935     
936     @Test
937     public void testIndexOf() throws Exception {
938         boolean direct = false;
939         for (int i = 0; i < 2; i++, direct = !direct) {
940             IoBuffer buf = IoBuffer.allocate(16, direct);
941             buf.put((byte) 0x1);
942             buf.put((byte) 0x2);
943             buf.put((byte) 0x3);
944             buf.put((byte) 0x4);
945             buf.put((byte) 0x1);
946             buf.put((byte) 0x2);
947             buf.put((byte) 0x3);
948             buf.put((byte) 0x4);
949             buf.position(2);
950             buf.limit(5);
951 
952             assertEquals(4, buf.indexOf((byte) 0x1));
953             assertEquals(-1, buf.indexOf((byte) 0x2));
954             assertEquals(2, buf.indexOf((byte) 0x3));
955             assertEquals(3, buf.indexOf((byte) 0x4));
956         }
957     }
958 
959     // We need an enum with 64 values
960     private static enum TestEnum {
961         E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19, E20, E21, E22, E23, E24, E25, E26, E27, E28, E29, E30, E31, E32, E33, E34, E35, E36, E37, E38, E39, E40, E41, E42, E43, E44, E45, E46, E77, E48, E49, E50, E51, E52, E53, E54, E55, E56, E57, E58, E59, E60, E61, E62, E63, E64
962     }
963 
964     private static enum TooBigEnum {
965         E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19, E20, E21, E22, E23, E24, E25, E26, E27, E28, E29, E30, E31, E32, E33, E34, E35, E36, E37, E38, E39, E40, E41, E42, E43, E44, E45, E46, E77, E48, E49, E50, E51, E52, E53, E54, E55, E56, E57, E58, E59, E60, E61, E62, E63, E64, E65
966     }
967 
968     @Test
969     public void testPutEnumSet() {
970         IoBuffer buf = IoBuffer.allocate(8);
971 
972         // Test empty set
973         buf.putEnumSet(EnumSet.noneOf(TestEnum.class));
974         buf.flip();
975         assertEquals(0, buf.get());
976 
977         buf.clear();
978         buf.putEnumSetShort(EnumSet.noneOf(TestEnum.class));
979         buf.flip();
980         assertEquals(0, buf.getShort());
981 
982         buf.clear();
983         buf.putEnumSetInt(EnumSet.noneOf(TestEnum.class));
984         buf.flip();
985         assertEquals(0, buf.getInt());
986 
987         buf.clear();
988         buf.putEnumSetLong(EnumSet.noneOf(TestEnum.class));
989         buf.flip();
990         assertEquals(0, buf.getLong());
991 
992         // Test complete set
993         buf.clear();
994         buf.putEnumSet(EnumSet.range(TestEnum.E1, TestEnum.E8));
995         buf.flip();
996         assertEquals((byte) -1, buf.get());
997 
998         buf.clear();
999         buf.putEnumSetShort(EnumSet.range(TestEnum.E1, TestEnum.E16));
1000         buf.flip();
1001         assertEquals((short) -1, buf.getShort());
1002 
1003         buf.clear();
1004         buf.putEnumSetInt(EnumSet.range(TestEnum.E1, TestEnum.E32));
1005         buf.flip();
1006         assertEquals(-1, buf.getInt());
1007 
1008         buf.clear();
1009         buf.putEnumSetLong(EnumSet.allOf(TestEnum.class));
1010         buf.flip();
1011         assertEquals(-1L, buf.getLong());
1012 
1013         // Test high bit set
1014         buf.clear();
1015         buf.putEnumSet(EnumSet.of(TestEnum.E8));
1016         buf.flip();
1017         assertEquals(Byte.MIN_VALUE, buf.get());
1018 
1019         buf.clear();
1020         buf.putEnumSetShort(EnumSet.of(TestEnum.E16));
1021         buf.flip();
1022         assertEquals(Short.MIN_VALUE, buf.getShort());
1023 
1024         buf.clear();
1025         buf.putEnumSetInt(EnumSet.of(TestEnum.E32));
1026         buf.flip();
1027         assertEquals(Integer.MIN_VALUE, buf.getInt());
1028 
1029         buf.clear();
1030         buf.putEnumSetLong(EnumSet.of(TestEnum.E64));
1031         buf.flip();
1032         assertEquals(Long.MIN_VALUE, buf.getLong());
1033 
1034         // Test high low bits set
1035         buf.clear();
1036         buf.putEnumSet(EnumSet.of(TestEnum.E1, TestEnum.E8));
1037         buf.flip();
1038         assertEquals(Byte.MIN_VALUE + 1, buf.get());
1039 
1040         buf.clear();
1041         buf.putEnumSetShort(EnumSet.of(TestEnum.E1, TestEnum.E16));
1042         buf.flip();
1043         assertEquals(Short.MIN_VALUE + 1, buf.getShort());
1044 
1045         buf.clear();
1046         buf.putEnumSetInt(EnumSet.of(TestEnum.E1, TestEnum.E32));
1047         buf.flip();
1048         assertEquals(Integer.MIN_VALUE + 1, buf.getInt());
1049 
1050         buf.clear();
1051         buf.putEnumSetLong(EnumSet.of(TestEnum.E1, TestEnum.E64));
1052         buf.flip();
1053         assertEquals(Long.MIN_VALUE + 1, buf.getLong());
1054     }
1055 
1056     @Test
1057     public void testGetEnumSet() {
1058         IoBuffer buf = IoBuffer.allocate(8);
1059 
1060         // Test empty set
1061         buf.put((byte) 0);
1062         buf.flip();
1063         assertEquals(EnumSet.noneOf(TestEnum.class), buf
1064                 .getEnumSet(TestEnum.class));
1065 
1066         buf.clear();
1067         buf.putShort((short) 0);
1068         buf.flip();
1069         assertEquals(EnumSet.noneOf(TestEnum.class), buf
1070                 .getEnumSet(TestEnum.class));
1071 
1072         buf.clear();
1073         buf.putInt(0);
1074         buf.flip();
1075         assertEquals(EnumSet.noneOf(TestEnum.class), buf
1076                 .getEnumSet(TestEnum.class));
1077 
1078         buf.clear();
1079         buf.putLong(0L);
1080         buf.flip();
1081         assertEquals(EnumSet.noneOf(TestEnum.class), buf
1082                 .getEnumSet(TestEnum.class));
1083 
1084         // Test complete set
1085         buf.clear();
1086         buf.put((byte) -1);
1087         buf.flip();
1088         assertEquals(EnumSet.range(TestEnum.E1, TestEnum.E8), buf
1089                 .getEnumSet(TestEnum.class));
1090 
1091         buf.clear();
1092         buf.putShort((short) -1);
1093         buf.flip();
1094         assertEquals(EnumSet.range(TestEnum.E1, TestEnum.E16), buf
1095                 .getEnumSetShort(TestEnum.class));
1096 
1097         buf.clear();
1098         buf.putInt(-1);
1099         buf.flip();
1100         assertEquals(EnumSet.range(TestEnum.E1, TestEnum.E32), buf
1101                 .getEnumSetInt(TestEnum.class));
1102 
1103         buf.clear();
1104         buf.putLong(-1L);
1105         buf.flip();
1106         assertEquals(EnumSet.allOf(TestEnum.class), buf
1107                 .getEnumSetLong(TestEnum.class));
1108 
1109         // Test high bit set
1110         buf.clear();
1111         buf.put(Byte.MIN_VALUE);
1112         buf.flip();
1113         assertEquals(EnumSet.of(TestEnum.E8), buf.getEnumSet(TestEnum.class));
1114 
1115         buf.clear();
1116         buf.putShort(Short.MIN_VALUE);
1117         buf.flip();
1118         assertEquals(EnumSet.of(TestEnum.E16), buf
1119                 .getEnumSetShort(TestEnum.class));
1120 
1121         buf.clear();
1122         buf.putInt(Integer.MIN_VALUE);
1123         buf.flip();
1124         assertEquals(EnumSet.of(TestEnum.E32), buf
1125                 .getEnumSetInt(TestEnum.class));
1126 
1127         buf.clear();
1128         buf.putLong(Long.MIN_VALUE);
1129         buf.flip();
1130         assertEquals(EnumSet.of(TestEnum.E64), buf
1131                 .getEnumSetLong(TestEnum.class));
1132 
1133         // Test high low bits set
1134         buf.clear();
1135         byte b = Byte.MIN_VALUE + 1;
1136         buf.put(b);
1137         buf.flip();
1138         assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E8), buf
1139                 .getEnumSet(TestEnum.class));
1140 
1141         buf.clear();
1142         short s = Short.MIN_VALUE + 1;
1143         buf.putShort(s);
1144         buf.flip();
1145         assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E16), buf
1146                 .getEnumSetShort(TestEnum.class));
1147 
1148         buf.clear();
1149         buf.putInt(Integer.MIN_VALUE + 1);
1150         buf.flip();
1151         assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E32), buf
1152                 .getEnumSetInt(TestEnum.class));
1153 
1154         buf.clear();
1155         buf.putLong(Long.MIN_VALUE + 1);
1156         buf.flip();
1157         assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E64), buf
1158                 .getEnumSetLong(TestEnum.class));
1159     }
1160 
1161     @Test
1162     public void testBitVectorOverFlow() {
1163         IoBuffer buf = IoBuffer.allocate(8);
1164         try {
1165             buf.putEnumSet(EnumSet.of(TestEnum.E9));
1166             fail("Should have thrown IllegalArgumentException");
1167         } catch (IllegalArgumentException e) {
1168             // Expected an Exception, signifies test success
1169             assertTrue(true);
1170         }
1171 
1172         try {
1173             buf.putEnumSetShort(EnumSet.of(TestEnum.E17));
1174             fail("Should have thrown IllegalArgumentException");
1175         } catch (IllegalArgumentException e) {
1176             // Expected an Exception, signifies test success
1177             assertTrue(true);
1178         }
1179 
1180         try {
1181             buf.putEnumSetInt(EnumSet.of(TestEnum.E33));
1182             fail("Should have thrown IllegalArgumentException");
1183         } catch (IllegalArgumentException e) {
1184             // Expected an Exception, signifies test success
1185             assertTrue(true);
1186         }
1187 
1188         try {
1189             buf.putEnumSetLong(EnumSet.of(TooBigEnum.E65));
1190             fail("Should have thrown IllegalArgumentException");
1191         } catch (IllegalArgumentException e) {
1192             // Expected an Exception, signifies test success
1193             assertTrue(true);
1194         }
1195     }
1196 
1197     @Test
1198     public void testGetPutEnum() {
1199         IoBuffer buf = IoBuffer.allocate(4);
1200 
1201         buf.putEnum(TestEnum.E64);
1202         buf.flip();
1203         assertEquals(TestEnum.E64, buf.getEnum(TestEnum.class));
1204 
1205         buf.clear();
1206         buf.putEnumShort(TestEnum.E64);
1207         buf.flip();
1208         assertEquals(TestEnum.E64, buf.getEnumShort(TestEnum.class));
1209 
1210         buf.clear();
1211         buf.putEnumInt(TestEnum.E64);
1212         buf.flip();
1213         assertEquals(TestEnum.E64, buf.getEnumInt(TestEnum.class));
1214     }
1215 
1216     @Test
1217     public void testGetMediumInt() {
1218         IoBuffer buf = IoBuffer.allocate(3);
1219 
1220         buf.put((byte) 0x01);
1221         buf.put((byte) 0x02);
1222         buf.put((byte) 0x03);
1223         assertEquals(3, buf.position());
1224 
1225         buf.flip();
1226         assertEquals(0x010203, buf.getMediumInt());
1227         assertEquals(0x010203, buf.getMediumInt(0));
1228         buf.flip();
1229         assertEquals(0x010203, buf.getUnsignedMediumInt());
1230         assertEquals(0x010203, buf.getUnsignedMediumInt(0));
1231         buf.flip();
1232         assertEquals(0x010203, buf.getUnsignedMediumInt());
1233         buf.flip().order(ByteOrder.LITTLE_ENDIAN);
1234         assertEquals(0x030201, buf.getMediumInt());
1235         assertEquals(0x030201, buf.getMediumInt(0));
1236 
1237         // Test max medium int
1238         buf.flip().order(ByteOrder.BIG_ENDIAN);
1239         buf.put((byte) 0x7f);
1240         buf.put((byte) 0xff);
1241         buf.put((byte) 0xff);
1242         buf.flip();
1243         assertEquals(0x7fffff, buf.getMediumInt());
1244         assertEquals(0x7fffff, buf.getMediumInt(0));
1245 
1246         // Test negative number
1247         buf.flip().order(ByteOrder.BIG_ENDIAN);
1248         buf.put((byte) 0xff);
1249         buf.put((byte) 0x02);
1250         buf.put((byte) 0x03);
1251         buf.flip();
1252 
1253         assertEquals(0xffff0203, buf.getMediumInt());
1254         assertEquals(0xffff0203, buf.getMediumInt(0));
1255         buf.flip();
1256 
1257         assertEquals(0x00ff0203, buf.getUnsignedMediumInt());
1258         assertEquals(0x00ff0203, buf.getUnsignedMediumInt(0));
1259     }
1260 
1261     @Test
1262     public void testPutMediumInt() {
1263         IoBuffer buf = IoBuffer.allocate(3);
1264 
1265         checkMediumInt(buf, 0);
1266         checkMediumInt(buf, 1);
1267         checkMediumInt(buf, -1);
1268         checkMediumInt(buf, 0x7fffff);
1269     }
1270 
1271     private void checkMediumInt(IoBuffer buf, int x) {
1272         buf.putMediumInt(x);
1273         assertEquals(3, buf.position());
1274         buf.flip();
1275         assertEquals(x, buf.getMediumInt());
1276         assertEquals(3, buf.position());
1277 
1278         buf.putMediumInt(0, x);
1279         assertEquals(3, buf.position());
1280         assertEquals(x, buf.getMediumInt(0));
1281 
1282         buf.flip();
1283     }
1284     
1285     @Test
1286     public void testPutUnsigned() {
1287         IoBuffer buf = IoBuffer.allocate(4);
1288         byte b = (byte)0x80;                // We should get 0x0080
1289         short s = (short)0x8F81;           // We should get 0x0081
1290         int i = 0x8FFFFF82;                // We should get 0x0082
1291         long l = 0x8FFFFFFFFFFFFF83L;      // We should get 0x0083
1292         
1293         buf.mark();
1294 
1295         // Put the unsigned bytes
1296         buf.putUnsigned( b );
1297         buf.putUnsigned( s );
1298         buf.putUnsigned( i );
1299         buf.putUnsigned( l );
1300 
1301         buf.reset();
1302         
1303         // Read back the unsigned bytes
1304         assertEquals( 0x0080, buf.getUnsigned() );
1305         assertEquals( 0x0081, buf.getUnsigned() );
1306         assertEquals( 0x0082, buf.getUnsigned() );
1307         assertEquals( 0x0083, buf.getUnsigned() );
1308     }
1309     
1310     @Test
1311     public void testPutUnsignedIndex() {
1312         IoBuffer buf = IoBuffer.allocate(4);
1313         byte b = (byte)0x80;               // We should get 0x0080
1314         short s = (short)0x8F81;           // We should get 0x0081
1315         int i = 0x8FFFFF82;                // We should get 0x0082
1316         long l = 0x8FFFFFFFFFFFFF83L;      // We should get 0x0083
1317         
1318         buf.mark();
1319 
1320         // Put the unsigned bytes
1321         buf.putUnsigned( 3, b );
1322         buf.putUnsigned( 2, s );
1323         buf.putUnsigned( 1, i );
1324         buf.putUnsigned( 0, l );
1325 
1326         buf.reset();
1327         
1328         // Read back the unsigned bytes
1329         assertEquals( 0x0083, buf.getUnsigned() );
1330         assertEquals( 0x0082, buf.getUnsigned() );
1331         assertEquals( 0x0081, buf.getUnsigned() );
1332         assertEquals( 0x0080, buf.getUnsigned() );
1333     }
1334 
1335     @Test
1336     public void testPutUnsignedShort() {
1337         IoBuffer buf = IoBuffer.allocate(8);
1338         byte b = (byte)0x80;               // We should get 0x0080
1339         short s = (short)0x8181;           // We should get 0x8181
1340         int i = 0x82828282;                // We should get 0x8282
1341         long l = 0x8383838383838383L;      // We should get 0x8383
1342         
1343         buf.mark();
1344 
1345         // Put the unsigned bytes
1346         buf.putUnsignedShort( b );
1347         buf.putUnsignedShort( s );
1348         buf.putUnsignedShort( i );
1349         buf.putUnsignedShort( l );
1350 
1351         buf.reset();
1352         
1353         // Read back the unsigned bytes
1354         assertEquals( 0x0080L, buf.getUnsignedShort() );
1355         assertEquals( 0x8181L, buf.getUnsignedShort() );
1356         assertEquals( 0x8282L, buf.getUnsignedShort() );
1357         assertEquals( 0x8383L, buf.getUnsignedShort() );
1358     }
1359     
1360     @Test
1361     public void testPutUnsignedShortIndex() {
1362         IoBuffer buf = IoBuffer.allocate(8);
1363         byte b = (byte)0x80;               // We should get 0x00000080
1364         short s = (short)0x8181;           // We should get 0x00008181
1365         int i = 0x82828282;                // We should get 0x82828282
1366         long l = 0x8383838383838383L;      // We should get 0x83838383
1367         
1368         buf.mark();
1369 
1370         // Put the unsigned bytes
1371         buf.putUnsignedShort( 3, b );
1372         buf.putUnsignedShort( 2, s );
1373         buf.putUnsignedShort( 1, i );
1374         buf.putUnsignedShort( 0, l );
1375 
1376         buf.reset();
1377         
1378         // Read back the unsigned bytes
1379         assertEquals( 0x0080L, buf.getUnsignedShort() );
1380         assertEquals( 0x8181L, buf.getUnsignedShort() );
1381         assertEquals( 0x8282L, buf.getUnsignedShort() );
1382         assertEquals( 0x8383L, buf.getUnsignedShort() );
1383     }
1384     
1385     @Test
1386     public void testPutUnsignedInt() {
1387         IoBuffer buf = IoBuffer.allocate(16);
1388         byte b = (byte)0x80;               // We should get 0x00000080
1389         short s = (short)0x8181;           // We should get 0x00008181
1390         int i = 0x82828282;                // We should get 0x82828282
1391         long l = 0x8383838383838383L;      // We should get 0x83838383
1392         
1393         buf.mark();
1394 
1395         // Put the unsigned bytes
1396         buf.putUnsignedInt( b );
1397         buf.putUnsignedInt( s );
1398         buf.putUnsignedInt( i );
1399         buf.putUnsignedInt( l );
1400 
1401         buf.reset();
1402         
1403         // Read back the unsigned bytes
1404         assertEquals( 0x0000000000000080L, buf.getUnsignedInt() );
1405         assertEquals( 0x0000000000008181L, buf.getUnsignedInt() );
1406         assertEquals( 0x0000000082828282L, buf.getUnsignedInt() );
1407         assertEquals( 0x0000000083838383L, buf.getUnsignedInt() );
1408     }
1409     
1410     @Test
1411     public void testPutUnsignedIntIndex() {
1412         IoBuffer buf = IoBuffer.allocate(16);
1413         byte b = (byte)0x80;               // We should get 0x00000080
1414         short s = (short)0x8181;           // We should get 0x00008181
1415         int i = 0x82828282;                // We should get 0x82828282
1416         long l = 0x8383838383838383L;      // We should get 0x83838383
1417         
1418         buf.mark();
1419 
1420         // Put the unsigned bytes
1421         buf.putUnsignedInt( 3, b );
1422         buf.putUnsignedInt( 2, s );
1423         buf.putUnsignedInt( 1, i );
1424         buf.putUnsignedInt( 0, l );
1425 
1426         buf.reset();
1427         
1428         // Read back the unsigned bytes
1429         assertEquals( 0x0000000000000080L, buf.getUnsignedInt() );
1430         assertEquals( 0x0000000000008181L, buf.getUnsignedInt() );
1431         assertEquals( 0x0000000082828282L, buf.getUnsignedInt() );
1432         assertEquals( 0x0000000083838383L, buf.getUnsignedInt() );
1433     }
1434 }