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", Charset.forName("ISO-8859-1").newEncoder());
909             fail("ReadOnly buffer's can't be expanded");
910         } catch (ReadOnlyBufferException e) {
911             // Expected an Exception, signifies test success
912             assertTrue(true);
913         }
914     }
915 
916     @Test
917     public void testGetUnsigned() throws Exception {
918         IoBuffer buf = IoBuffer.allocate(16);
919         buf.put((byte) 0xA4);
920         buf.put((byte) 0xD0);
921         buf.put((byte) 0xB3);
922         buf.put((byte) 0xCD);
923         buf.flip();
924 
925         buf.order(ByteOrder.LITTLE_ENDIAN);
926 
927         buf.mark();
928         assertEquals(0xA4, buf.getUnsigned());
929         buf.reset();
930         assertEquals(0xD0A4, buf.getUnsignedShort());
931         buf.reset();
932         assertEquals(0xCDB3D0A4L, buf.getUnsignedInt());
933     }
934 
935     @Test
936     public void testIndexOf() throws Exception {
937         boolean direct = false;
938         for (int i = 0; i < 2; i++, direct = !direct) {
939             IoBuffer buf = IoBuffer.allocate(16, direct);
940             buf.put((byte) 0x1);
941             buf.put((byte) 0x2);
942             buf.put((byte) 0x3);
943             buf.put((byte) 0x4);
944             buf.put((byte) 0x1);
945             buf.put((byte) 0x2);
946             buf.put((byte) 0x3);
947             buf.put((byte) 0x4);
948             buf.position(2);
949             buf.limit(5);
950 
951             assertEquals(4, buf.indexOf((byte) 0x1));
952             assertEquals(-1, buf.indexOf((byte) 0x2));
953             assertEquals(2, buf.indexOf((byte) 0x3));
954             assertEquals(3, buf.indexOf((byte) 0x4));
955         }
956     }
957 
958     // We need an enum with 64 values
959     private static enum TestEnum {
960         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
961     }
962 
963     private static enum TooBigEnum {
964         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
965     }
966 
967     @Test
968     public void testPutEnumSet() {
969         IoBuffer buf = IoBuffer.allocate(8);
970 
971         // Test empty set
972         buf.putEnumSet(EnumSet.noneOf(TestEnum.class));
973         buf.flip();
974         assertEquals(0, buf.get());
975 
976         buf.clear();
977         buf.putEnumSetShort(EnumSet.noneOf(TestEnum.class));
978         buf.flip();
979         assertEquals(0, buf.getShort());
980 
981         buf.clear();
982         buf.putEnumSetInt(EnumSet.noneOf(TestEnum.class));
983         buf.flip();
984         assertEquals(0, buf.getInt());
985 
986         buf.clear();
987         buf.putEnumSetLong(EnumSet.noneOf(TestEnum.class));
988         buf.flip();
989         assertEquals(0, buf.getLong());
990 
991         // Test complete set
992         buf.clear();
993         buf.putEnumSet(EnumSet.range(TestEnum.E1, TestEnum.E8));
994         buf.flip();
995         assertEquals((byte) -1, buf.get());
996 
997         buf.clear();
998         buf.putEnumSetShort(EnumSet.range(TestEnum.E1, TestEnum.E16));
999         buf.flip();
1000         assertEquals((short) -1, buf.getShort());
1001 
1002         buf.clear();
1003         buf.putEnumSetInt(EnumSet.range(TestEnum.E1, TestEnum.E32));
1004         buf.flip();
1005         assertEquals(-1, buf.getInt());
1006 
1007         buf.clear();
1008         buf.putEnumSetLong(EnumSet.allOf(TestEnum.class));
1009         buf.flip();
1010         assertEquals(-1L, buf.getLong());
1011 
1012         // Test high bit set
1013         buf.clear();
1014         buf.putEnumSet(EnumSet.of(TestEnum.E8));
1015         buf.flip();
1016         assertEquals(Byte.MIN_VALUE, buf.get());
1017 
1018         buf.clear();
1019         buf.putEnumSetShort(EnumSet.of(TestEnum.E16));
1020         buf.flip();
1021         assertEquals(Short.MIN_VALUE, buf.getShort());
1022 
1023         buf.clear();
1024         buf.putEnumSetInt(EnumSet.of(TestEnum.E32));
1025         buf.flip();
1026         assertEquals(Integer.MIN_VALUE, buf.getInt());
1027 
1028         buf.clear();
1029         buf.putEnumSetLong(EnumSet.of(TestEnum.E64));
1030         buf.flip();
1031         assertEquals(Long.MIN_VALUE, buf.getLong());
1032 
1033         // Test high low bits set
1034         buf.clear();
1035         buf.putEnumSet(EnumSet.of(TestEnum.E1, TestEnum.E8));
1036         buf.flip();
1037         assertEquals(Byte.MIN_VALUE + 1, buf.get());
1038 
1039         buf.clear();
1040         buf.putEnumSetShort(EnumSet.of(TestEnum.E1, TestEnum.E16));
1041         buf.flip();
1042         assertEquals(Short.MIN_VALUE + 1, buf.getShort());
1043 
1044         buf.clear();
1045         buf.putEnumSetInt(EnumSet.of(TestEnum.E1, TestEnum.E32));
1046         buf.flip();
1047         assertEquals(Integer.MIN_VALUE + 1, buf.getInt());
1048 
1049         buf.clear();
1050         buf.putEnumSetLong(EnumSet.of(TestEnum.E1, TestEnum.E64));
1051         buf.flip();
1052         assertEquals(Long.MIN_VALUE + 1, buf.getLong());
1053     }
1054 
1055     @Test
1056     public void testGetEnumSet() {
1057         IoBuffer buf = IoBuffer.allocate(8);
1058 
1059         // Test empty set
1060         buf.put((byte) 0);
1061         buf.flip();
1062         assertEquals(EnumSet.noneOf(TestEnum.class), buf.getEnumSet(TestEnum.class));
1063 
1064         buf.clear();
1065         buf.putShort((short) 0);
1066         buf.flip();
1067         assertEquals(EnumSet.noneOf(TestEnum.class), buf.getEnumSet(TestEnum.class));
1068 
1069         buf.clear();
1070         buf.putInt(0);
1071         buf.flip();
1072         assertEquals(EnumSet.noneOf(TestEnum.class), buf.getEnumSet(TestEnum.class));
1073 
1074         buf.clear();
1075         buf.putLong(0L);
1076         buf.flip();
1077         assertEquals(EnumSet.noneOf(TestEnum.class), buf.getEnumSet(TestEnum.class));
1078 
1079         // Test complete set
1080         buf.clear();
1081         buf.put((byte) -1);
1082         buf.flip();
1083         assertEquals(EnumSet.range(TestEnum.E1, TestEnum.E8), buf.getEnumSet(TestEnum.class));
1084 
1085         buf.clear();
1086         buf.putShort((short) -1);
1087         buf.flip();
1088         assertEquals(EnumSet.range(TestEnum.E1, TestEnum.E16), buf.getEnumSetShort(TestEnum.class));
1089 
1090         buf.clear();
1091         buf.putInt(-1);
1092         buf.flip();
1093         assertEquals(EnumSet.range(TestEnum.E1, TestEnum.E32), buf.getEnumSetInt(TestEnum.class));
1094 
1095         buf.clear();
1096         buf.putLong(-1L);
1097         buf.flip();
1098         assertEquals(EnumSet.allOf(TestEnum.class), buf.getEnumSetLong(TestEnum.class));
1099 
1100         // Test high bit set
1101         buf.clear();
1102         buf.put(Byte.MIN_VALUE);
1103         buf.flip();
1104         assertEquals(EnumSet.of(TestEnum.E8), buf.getEnumSet(TestEnum.class));
1105 
1106         buf.clear();
1107         buf.putShort(Short.MIN_VALUE);
1108         buf.flip();
1109         assertEquals(EnumSet.of(TestEnum.E16), buf.getEnumSetShort(TestEnum.class));
1110 
1111         buf.clear();
1112         buf.putInt(Integer.MIN_VALUE);
1113         buf.flip();
1114         assertEquals(EnumSet.of(TestEnum.E32), buf.getEnumSetInt(TestEnum.class));
1115 
1116         buf.clear();
1117         buf.putLong(Long.MIN_VALUE);
1118         buf.flip();
1119         assertEquals(EnumSet.of(TestEnum.E64), buf.getEnumSetLong(TestEnum.class));
1120 
1121         // Test high low bits set
1122         buf.clear();
1123         byte b = Byte.MIN_VALUE + 1;
1124         buf.put(b);
1125         buf.flip();
1126         assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E8), buf.getEnumSet(TestEnum.class));
1127 
1128         buf.clear();
1129         short s = Short.MIN_VALUE + 1;
1130         buf.putShort(s);
1131         buf.flip();
1132         assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E16), buf.getEnumSetShort(TestEnum.class));
1133 
1134         buf.clear();
1135         buf.putInt(Integer.MIN_VALUE + 1);
1136         buf.flip();
1137         assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E32), buf.getEnumSetInt(TestEnum.class));
1138 
1139         buf.clear();
1140         buf.putLong(Long.MIN_VALUE + 1);
1141         buf.flip();
1142         assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E64), buf.getEnumSetLong(TestEnum.class));
1143     }
1144 
1145     @Test
1146     public void testBitVectorOverFlow() {
1147         IoBuffer buf = IoBuffer.allocate(8);
1148         try {
1149             buf.putEnumSet(EnumSet.of(TestEnum.E9));
1150             fail("Should have thrown IllegalArgumentException");
1151         } catch (IllegalArgumentException e) {
1152             // Expected an Exception, signifies test success
1153             assertTrue(true);
1154         }
1155 
1156         try {
1157             buf.putEnumSetShort(EnumSet.of(TestEnum.E17));
1158             fail("Should have thrown IllegalArgumentException");
1159         } catch (IllegalArgumentException e) {
1160             // Expected an Exception, signifies test success
1161             assertTrue(true);
1162         }
1163 
1164         try {
1165             buf.putEnumSetInt(EnumSet.of(TestEnum.E33));
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.putEnumSetLong(EnumSet.of(TooBigEnum.E65));
1174             fail("Should have thrown IllegalArgumentException");
1175         } catch (IllegalArgumentException e) {
1176             // Expected an Exception, signifies test success
1177             assertTrue(true);
1178         }
1179     }
1180 
1181     @Test
1182     public void testGetPutEnum() {
1183         IoBuffer buf = IoBuffer.allocate(4);
1184 
1185         buf.putEnum(TestEnum.E64);
1186         buf.flip();
1187         assertEquals(TestEnum.E64, buf.getEnum(TestEnum.class));
1188 
1189         buf.clear();
1190         buf.putEnumShort(TestEnum.E64);
1191         buf.flip();
1192         assertEquals(TestEnum.E64, buf.getEnumShort(TestEnum.class));
1193 
1194         buf.clear();
1195         buf.putEnumInt(TestEnum.E64);
1196         buf.flip();
1197         assertEquals(TestEnum.E64, buf.getEnumInt(TestEnum.class));
1198     }
1199 
1200     @Test
1201     public void testGetMediumInt() {
1202         IoBuffer buf = IoBuffer.allocate(3);
1203 
1204         buf.put((byte) 0x01);
1205         buf.put((byte) 0x02);
1206         buf.put((byte) 0x03);
1207         assertEquals(3, buf.position());
1208 
1209         buf.flip();
1210         assertEquals(0x010203, buf.getMediumInt());
1211         assertEquals(0x010203, buf.getMediumInt(0));
1212         buf.flip();
1213         assertEquals(0x010203, buf.getUnsignedMediumInt());
1214         assertEquals(0x010203, buf.getUnsignedMediumInt(0));
1215         buf.flip();
1216         assertEquals(0x010203, buf.getUnsignedMediumInt());
1217         buf.flip().order(ByteOrder.LITTLE_ENDIAN);
1218         assertEquals(0x030201, buf.getMediumInt());
1219         assertEquals(0x030201, buf.getMediumInt(0));
1220 
1221         // Test max medium int
1222         buf.flip().order(ByteOrder.BIG_ENDIAN);
1223         buf.put((byte) 0x7f);
1224         buf.put((byte) 0xff);
1225         buf.put((byte) 0xff);
1226         buf.flip();
1227         assertEquals(0x7fffff, buf.getMediumInt());
1228         assertEquals(0x7fffff, buf.getMediumInt(0));
1229 
1230         // Test negative number
1231         buf.flip().order(ByteOrder.BIG_ENDIAN);
1232         buf.put((byte) 0xff);
1233         buf.put((byte) 0x02);
1234         buf.put((byte) 0x03);
1235         buf.flip();
1236 
1237         assertEquals(0xffff0203, buf.getMediumInt());
1238         assertEquals(0xffff0203, buf.getMediumInt(0));
1239         buf.flip();
1240 
1241         assertEquals(0x00ff0203, buf.getUnsignedMediumInt());
1242         assertEquals(0x00ff0203, buf.getUnsignedMediumInt(0));
1243     }
1244 
1245     @Test
1246     public void testPutMediumInt() {
1247         IoBuffer buf = IoBuffer.allocate(3);
1248 
1249         checkMediumInt(buf, 0);
1250         checkMediumInt(buf, 1);
1251         checkMediumInt(buf, -1);
1252         checkMediumInt(buf, 0x7fffff);
1253     }
1254 
1255     private void checkMediumInt(IoBuffer buf, int x) {
1256         buf.putMediumInt(x);
1257         assertEquals(3, buf.position());
1258         buf.flip();
1259         assertEquals(x, buf.getMediumInt());
1260         assertEquals(3, buf.position());
1261 
1262         buf.putMediumInt(0, x);
1263         assertEquals(3, buf.position());
1264         assertEquals(x, buf.getMediumInt(0));
1265 
1266         buf.flip();
1267     }
1268 
1269     @Test
1270     public void testPutUnsigned() {
1271         IoBuffer buf = IoBuffer.allocate(4);
1272         byte b = (byte) 0x80; // We should get 0x0080
1273         short s = (short) 0x8F81; // We should get 0x0081
1274         int i = 0x8FFFFF82; // We should get 0x0082
1275         long l = 0x8FFFFFFFFFFFFF83L; // We should get 0x0083
1276 
1277         buf.mark();
1278 
1279         // Put the unsigned bytes
1280         buf.putUnsigned(b);
1281         buf.putUnsigned(s);
1282         buf.putUnsigned(i);
1283         buf.putUnsigned(l);
1284 
1285         buf.reset();
1286 
1287         // Read back the unsigned bytes
1288         assertEquals(0x0080, buf.getUnsigned());
1289         assertEquals(0x0081, buf.getUnsigned());
1290         assertEquals(0x0082, buf.getUnsigned());
1291         assertEquals(0x0083, buf.getUnsigned());
1292     }
1293 
1294     @Test
1295     public void testPutUnsignedIndex() {
1296         IoBuffer buf = IoBuffer.allocate(4);
1297         byte b = (byte) 0x80; // We should get 0x0080
1298         short s = (short) 0x8F81; // We should get 0x0081
1299         int i = 0x8FFFFF82; // We should get 0x0082
1300         long l = 0x8FFFFFFFFFFFFF83L; // We should get 0x0083
1301 
1302         buf.mark();
1303 
1304         // Put the unsigned bytes
1305         buf.putUnsigned(3, b);
1306         buf.putUnsigned(2, s);
1307         buf.putUnsigned(1, i);
1308         buf.putUnsigned(0, l);
1309 
1310         buf.reset();
1311 
1312         // Read back the unsigned bytes
1313         assertEquals(0x0083, buf.getUnsigned());
1314         assertEquals(0x0082, buf.getUnsigned());
1315         assertEquals(0x0081, buf.getUnsigned());
1316         assertEquals(0x0080, buf.getUnsigned());
1317     }
1318 
1319     @Test
1320     public void testPutUnsignedShort() {
1321         IoBuffer buf = IoBuffer.allocate(8);
1322         byte b = (byte) 0x80; // We should get 0x0080
1323         short s = (short) 0x8181; // We should get 0x8181
1324         int i = 0x82828282; // We should get 0x8282
1325         long l = 0x8383838383838383L; // We should get 0x8383
1326 
1327         buf.mark();
1328 
1329         // Put the unsigned bytes
1330         buf.putUnsignedShort(b);
1331         buf.putUnsignedShort(s);
1332         buf.putUnsignedShort(i);
1333         buf.putUnsignedShort(l);
1334 
1335         buf.reset();
1336 
1337         // Read back the unsigned bytes
1338         assertEquals(0x0080L, buf.getUnsignedShort());
1339         assertEquals(0x8181L, buf.getUnsignedShort());
1340         assertEquals(0x8282L, buf.getUnsignedShort());
1341         assertEquals(0x8383L, buf.getUnsignedShort());
1342     }
1343 
1344     @Test
1345     public void testPutUnsignedShortIndex() {
1346         IoBuffer buf = IoBuffer.allocate(8);
1347         byte b = (byte) 0x80; // We should get 0x00000080
1348         short s = (short) 0x8181; // We should get 0x00008181
1349         int i = 0x82828282; // We should get 0x82828282
1350         long l = 0x8383838383838383L; // We should get 0x83838383
1351 
1352         buf.mark();
1353 
1354         // Put the unsigned shorts
1355         buf.putUnsignedShort(6, b);
1356         buf.putUnsignedShort(4, s);
1357         buf.putUnsignedShort(2, i);
1358         buf.putUnsignedShort(0, l);
1359 
1360         buf.reset();
1361 
1362         // Read back the unsigned bytes
1363         assertEquals(0x8383L, buf.getUnsignedShort());
1364         assertEquals(0x8282L, buf.getUnsignedShort());
1365         assertEquals(0x8181L, buf.getUnsignedShort());
1366         assertEquals(0x0080L, buf.getUnsignedShort());
1367     }
1368 
1369     @Test
1370     public void testPutUnsignedInt() {
1371         IoBuffer buf = IoBuffer.allocate(16);
1372         byte b = (byte) 0x80; // We should get 0x00000080
1373         short s = (short) 0x8181; // We should get 0x00008181
1374         int i = 0x82828282; // We should get 0x82828282
1375         long l = 0x8383838383838383L; // We should get 0x83838383
1376 
1377         buf.mark();
1378 
1379         // Put the unsigned bytes
1380         buf.putUnsignedInt(b);
1381         buf.putUnsignedInt(s);
1382         buf.putUnsignedInt(i);
1383         buf.putUnsignedInt(l);
1384 
1385         buf.reset();
1386 
1387         // Read back the unsigned bytes
1388         assertEquals(0x0000000000000080L, buf.getUnsignedInt());
1389         assertEquals(0x0000000000008181L, buf.getUnsignedInt());
1390         assertEquals(0x0000000082828282L, buf.getUnsignedInt());
1391         assertEquals(0x0000000083838383L, buf.getUnsignedInt());
1392     }
1393 
1394     @Test
1395     public void testPutUnsignedIntIndex() {
1396         IoBuffer buf = IoBuffer.allocate(16);
1397         byte b = (byte) 0x80; // We should get 0x00000080
1398         short s = (short) 0x8181; // We should get 0x00008181
1399         int i = 0x82828282; // We should get 0x82828282
1400         long l = 0x8383838383838383L; // We should get 0x83838383
1401 
1402         buf.mark();
1403 
1404         // Put the unsigned bytes
1405         buf.putUnsignedInt(12, b);
1406         buf.putUnsignedInt(8, s);
1407         buf.putUnsignedInt(4, i);
1408         buf.putUnsignedInt(0, l);
1409 
1410         buf.reset();
1411 
1412         // Read back the unsigned bytes
1413         assertEquals(0x0000000083838383L, buf.getUnsignedInt());
1414         assertEquals(0x0000000082828282L, buf.getUnsignedInt());
1415         assertEquals(0x0000000000008181L, buf.getUnsignedInt());
1416         assertEquals(0x0000000000000080L, buf.getUnsignedInt());
1417     }
1418 }