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