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.common;
21  
22  import junit.framework.Assert;
23  import junit.framework.TestCase;
24  
25  import java.nio.BufferOverflowException;
26  import java.nio.ByteOrder;
27  import java.nio.ReadOnlyBufferException;
28  import java.nio.charset.CharacterCodingException;
29  import java.nio.charset.Charset;
30  import java.nio.charset.CharsetDecoder;
31  import java.nio.charset.CharsetEncoder;
32  import java.util.ArrayList;
33  import java.util.Date;
34  import java.util.List;
35  
36  /**
37   * Tests {@link ByteBuffer}.
38   *
39   * @author The Apache Directory Project (mina-dev@directory.apache.org)
40   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13  7월 2007) $
41   */
42  public class ByteBufferTest extends TestCase {
43  
44      public static void main(String[] args) {
45          junit.textui.TestRunner.run(ByteBufferTest.class);
46      }
47  
48      protected void setUp() throws Exception {
49      }
50  
51      protected void tearDown() throws Exception {
52      }
53  
54      public void testAllocate() throws Exception {
55          for (int i = 10; i < 1048576 * 2; i = i * 11 / 10) // increase by 10%
56          {
57              ByteBuffer buf = ByteBuffer.allocate(i);
58              Assert.assertEquals(0, buf.position());
59              Assert.assertEquals(buf.capacity(), buf.remaining());
60              Assert.assertTrue(buf.capacity() >= i);
61              Assert.assertTrue(buf.capacity() < i * 2);
62          }
63      }
64  
65      public void testRelease() throws Exception {
66          for (int i = 10; i < 1048576 * 2; i = i * 11 / 10) // increase by 10%
67          {
68              ByteBuffer buf = ByteBuffer.allocate(i);
69              Assert.assertEquals(0, buf.position());
70              Assert.assertEquals(buf.capacity(), buf.remaining());
71              Assert.assertTrue(buf.capacity() >= i);
72              Assert.assertTrue(buf.capacity() < i * 2);
73              buf.release();
74          }
75      }
76  
77      public void testLeakageDetection() throws Exception {
78          ByteBuffer buf = ByteBuffer.allocate(1024);
79          buf.release();
80          try {
81              buf.release();
82              Assert.fail("Releasing a buffer twice should fail.");
83          } catch (IllegalStateException e) {
84  
85          }
86      }
87  
88      public void testAcquireRelease() throws Exception {
89          ByteBuffer buf = ByteBuffer.allocate(1024);
90          buf.acquire();
91          buf.release();
92          buf.acquire();
93          buf.acquire();
94          buf.release();
95          buf.release();
96          buf.release();
97          try {
98              buf.release();
99              Assert.fail("Releasing a buffer twice should fail.");
100         } catch (IllegalStateException e) {
101         }
102     }
103 
104     public void testAutoExpand() throws Exception {
105         ByteBuffer buf = ByteBuffer.allocate(1);
106 
107         buf.put((byte) 0);
108         try {
109             buf.put((byte) 0);
110             Assert.fail();
111         } catch (BufferOverflowException e) {
112             // ignore
113         }
114 
115         buf.setAutoExpand(true);
116         buf.put((byte) 0);
117         Assert.assertEquals(2, buf.position());
118         Assert.assertEquals(2, buf.limit());
119         Assert.assertEquals(2, buf.capacity());
120 
121         buf.setAutoExpand(false);
122         try {
123             buf.put(3, (byte) 0);
124             Assert.fail();
125         } catch (IndexOutOfBoundsException e) {
126             // ignore
127         }
128 
129         buf.setAutoExpand(true);
130         buf.put(3, (byte) 0);
131         Assert.assertEquals(2, buf.position());
132         Assert.assertEquals(4, buf.limit());
133         Assert.assertEquals(4, buf.capacity());
134     }
135 
136     public void testAutoExpandMark() throws Exception {
137         ByteBuffer buf = ByteBuffer.allocate(4).setAutoExpand(true);
138 
139         buf.put((byte) 0);
140         buf.put((byte) 0);
141         buf.put((byte) 0);
142 
143         // Position should be 3 when we reset this buffer.
144         buf.mark();
145 
146         // Overflow it
147         buf.put((byte) 0);
148         buf.put((byte) 0);
149 
150         Assert.assertEquals(5, buf.position());
151         buf.reset();
152         Assert.assertEquals(3, buf.position());
153     }
154 
155     public void testPooledProperty() throws Exception {
156         ByteBuffer buf = ByteBuffer.allocate(16);
157         java.nio.ByteBuffer nioBuf = buf.buf();
158         buf.release();
159         buf = ByteBuffer.allocate(16);
160         Assert.assertSame(nioBuf, buf.buf());
161         buf.setPooled(false);
162         buf.release();
163         Assert.assertNotSame(nioBuf, ByteBuffer.allocate(16).buf());
164     }
165 
166     public void testGetString() throws Exception {
167         ByteBuffer buf = ByteBuffer.allocate(16);
168         CharsetDecoder decoder;
169 
170         Charset charset = Charset.forName("UTF-8");
171         buf.clear();
172         buf.putString("hello", charset.newEncoder());
173         buf.put((byte) 0);
174         buf.flip();
175         Assert.assertEquals("hello", buf.getString(charset.newDecoder()));
176 
177         buf.clear();
178         buf.putString("hello", charset.newEncoder());
179         buf.flip();
180         Assert.assertEquals("hello", buf.getString(charset.newDecoder()));
181 
182         decoder = Charset.forName("ISO-8859-1").newDecoder();
183         buf.clear();
184         buf.put((byte) 'A');
185         buf.put((byte) 'B');
186         buf.put((byte) 'C');
187         buf.put((byte) 0);
188 
189         buf.position(0);
190         Assert.assertEquals("ABC", buf.getString(decoder));
191         Assert.assertEquals(4, buf.position());
192 
193         buf.position(0);
194         buf.limit(1);
195         Assert.assertEquals("A", buf.getString(decoder));
196         Assert.assertEquals(1, buf.position());
197 
198         buf.clear();
199         Assert.assertEquals("ABC", buf.getString(10, decoder));
200         Assert.assertEquals(10, buf.position());
201 
202         buf.clear();
203         Assert.assertEquals("A", buf.getString(1, decoder));
204         Assert.assertEquals(1, buf.position());
205 
206         // Test a trailing garbage
207         buf.clear();
208         buf.put((byte) 'A');
209         buf.put((byte) 'B');
210         buf.put((byte) 0);
211         buf.put((byte) 'C');
212         buf.position(0);
213         Assert.assertEquals("AB", buf.getString(4, decoder));
214         Assert.assertEquals(4, buf.position());
215 
216         buf.clear();
217         buf.fillAndReset(buf.limit());
218         decoder = Charset.forName("UTF-16").newDecoder();
219         buf.put((byte) 0);
220         buf.put((byte) 'A');
221         buf.put((byte) 0);
222         buf.put((byte) 'B');
223         buf.put((byte) 0);
224         buf.put((byte) 'C');
225         buf.put((byte) 0);
226         buf.put((byte) 0);
227 
228         buf.position(0);
229         Assert.assertEquals("ABC", buf.getString(decoder));
230         Assert.assertEquals(8, buf.position());
231 
232         buf.position(0);
233         buf.limit(2);
234         Assert.assertEquals("A", buf.getString(decoder));
235         Assert.assertEquals(2, buf.position());
236 
237         buf.position(0);
238         buf.limit(3);
239         Assert.assertEquals("A", buf.getString(decoder));
240         Assert.assertEquals(2, buf.position());
241 
242         buf.clear();
243         Assert.assertEquals("ABC", buf.getString(10, decoder));
244         Assert.assertEquals(10, buf.position());
245 
246         buf.clear();
247         Assert.assertEquals("A", buf.getString(2, decoder));
248         Assert.assertEquals(2, buf.position());
249 
250         buf.clear();
251         try {
252             buf.getString(1, decoder);
253             Assert.fail();
254         } catch (IllegalArgumentException e) {
255             // ignore
256         }
257 
258         // Test getting strings from an empty buffer.
259         buf.clear();
260         buf.limit(0);
261         Assert.assertEquals("", buf.getString(decoder));
262         Assert.assertEquals("", buf.getString(2, decoder));
263 
264         // Test getting strings from non-empty buffer which is filled with 0x00
265         buf.clear();
266         buf.putInt(0);
267         buf.clear();
268         buf.limit(4);
269         Assert.assertEquals("", buf.getString(decoder));
270         Assert.assertEquals(2, buf.position());
271         Assert.assertEquals(4, buf.limit());
272 
273         buf.position(0);
274         Assert.assertEquals("", buf.getString(2, decoder));
275         Assert.assertEquals(2, buf.position());
276         Assert.assertEquals(4, buf.limit());
277     }
278 
279     public void testGetStringWithFailure() throws Exception {
280         String test = "\u30b3\u30e1\u30f3\u30c8\u7de8\u96c6";
281         ByteBuffer buffer = ByteBuffer.wrap(test.getBytes("Shift_JIS"));
282 
283         // Make sure the limit doesn't change when an exception arose.
284         int oldLimit = buffer.limit();
285         int oldPos = buffer.position();
286         try {
287             buffer.getString(3, Charset.forName("ASCII").newDecoder());
288             Assert.fail();
289         } catch (Exception e) {
290             Assert.assertEquals(oldLimit, buffer.limit());
291             Assert.assertEquals(oldPos, buffer.position());
292         }
293 
294         try {
295             buffer.getString(Charset.forName("ASCII").newDecoder());
296             Assert.fail();
297         } catch (Exception e) {
298             Assert.assertEquals(oldLimit, buffer.limit());
299             Assert.assertEquals(oldPos, buffer.position());
300         }
301     }
302 
303     public void testPutString() throws Exception {
304         CharsetEncoder encoder;
305         ByteBuffer buf = ByteBuffer.allocate(16);
306         encoder = Charset.forName("ISO-8859-1").newEncoder();
307 
308         buf.putString("ABC", encoder);
309         Assert.assertEquals(3, buf.position());
310         buf.clear();
311         Assert.assertEquals('A', buf.get(0));
312         Assert.assertEquals('B', buf.get(1));
313         Assert.assertEquals('C', buf.get(2));
314 
315         buf.putString("D", 5, encoder);
316         Assert.assertEquals(5, buf.position());
317         buf.clear();
318         Assert.assertEquals('D', buf.get(0));
319         Assert.assertEquals(0, buf.get(1));
320 
321         buf.putString("EFG", 2, encoder);
322         Assert.assertEquals(2, buf.position());
323         buf.clear();
324         Assert.assertEquals('E', buf.get(0));
325         Assert.assertEquals('F', buf.get(1));
326         Assert.assertEquals('C', buf.get(2)); // C may not be overwritten
327 
328         // UTF-16: We specify byte order to omit BOM.
329         encoder = Charset.forName("UTF-16BE").newEncoder();
330         buf.clear();
331 
332         buf.putString("ABC", encoder);
333         Assert.assertEquals(6, buf.position());
334         buf.clear();
335 
336         Assert.assertEquals(0, buf.get(0));
337         Assert.assertEquals('A', buf.get(1));
338         Assert.assertEquals(0, buf.get(2));
339         Assert.assertEquals('B', buf.get(3));
340         Assert.assertEquals(0, buf.get(4));
341         Assert.assertEquals('C', buf.get(5));
342 
343         buf.putString("D", 10, encoder);
344         Assert.assertEquals(10, buf.position());
345         buf.clear();
346         Assert.assertEquals(0, buf.get(0));
347         Assert.assertEquals('D', buf.get(1));
348         Assert.assertEquals(0, buf.get(2));
349         Assert.assertEquals(0, buf.get(3));
350 
351         buf.putString("EFG", 4, encoder);
352         Assert.assertEquals(4, buf.position());
353         buf.clear();
354         Assert.assertEquals(0, buf.get(0));
355         Assert.assertEquals('E', buf.get(1));
356         Assert.assertEquals(0, buf.get(2));
357         Assert.assertEquals('F', buf.get(3));
358         Assert.assertEquals(0, buf.get(4)); // C may not be overwritten
359         Assert.assertEquals('C', buf.get(5)); // C may not be overwritten
360 
361         // Test putting an emptry string
362         buf.putString("", encoder);
363         Assert.assertEquals(0, buf.position());
364         buf.putString("", 4, encoder);
365         Assert.assertEquals(4, buf.position());
366         Assert.assertEquals(0, buf.get(0));
367         Assert.assertEquals(0, buf.get(1));
368     }
369 
370     public void testGetPrefixedString() throws Exception {
371         ByteBuffer buf = ByteBuffer.allocate(16);
372         CharsetEncoder encoder;
373         CharsetDecoder decoder;
374         encoder = Charset.forName("ISO-8859-1").newEncoder();
375         decoder = Charset.forName("ISO-8859-1").newDecoder();
376 
377         buf.putShort((short) 3);
378         buf.putString("ABCD", encoder);
379         buf.clear();
380         Assert.assertEquals("ABC", buf.getPrefixedString(decoder));
381     }
382 
383     public void testPutPrefixedString() throws Exception {
384         CharsetEncoder encoder;
385         ByteBuffer buf = ByteBuffer.allocate(16);
386         buf.fillAndReset(buf.remaining());
387         encoder = Charset.forName("ISO-8859-1").newEncoder();
388 
389         // Without autoExpand
390         buf.putPrefixedString("ABC", encoder);
391         Assert.assertEquals(5, buf.position());
392         Assert.assertEquals(0, buf.get(0));
393         Assert.assertEquals(3, buf.get(1));
394         Assert.assertEquals('A', buf.get(2));
395         Assert.assertEquals('B', buf.get(3));
396         Assert.assertEquals('C', buf.get(4));
397 
398         buf.clear();
399         try {
400             buf.putPrefixedString("123456789012345", encoder);
401             Assert.fail();
402         } catch (BufferOverflowException e) {
403             // OK
404         }
405 
406         // With autoExpand
407         buf.clear();
408         buf.setAutoExpand(true);
409         buf.putPrefixedString("123456789012345", encoder);
410         Assert.assertEquals(17, buf.position());
411         Assert.assertEquals(0, buf.get(0));
412         Assert.assertEquals(15, buf.get(1));
413         Assert.assertEquals('1', buf.get(2));
414         Assert.assertEquals('2', buf.get(3));
415         Assert.assertEquals('3', buf.get(4));
416         Assert.assertEquals('4', buf.get(5));
417         Assert.assertEquals('5', buf.get(6));
418         Assert.assertEquals('6', buf.get(7));
419         Assert.assertEquals('7', buf.get(8));
420         Assert.assertEquals('8', buf.get(9));
421         Assert.assertEquals('9', buf.get(10));
422         Assert.assertEquals('0', buf.get(11));
423         Assert.assertEquals('1', buf.get(12));
424         Assert.assertEquals('2', buf.get(13));
425         Assert.assertEquals('3', buf.get(14));
426         Assert.assertEquals('4', buf.get(15));
427         Assert.assertEquals('5', buf.get(16));
428     }
429 
430     public void testPutPrefixedStringWithPrefixLength() throws Exception {
431         CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder();
432         ByteBuffer buf = ByteBuffer.allocate(16).sweep().setAutoExpand(true);
433 
434         buf.putPrefixedString("A", 1, encoder);
435         Assert.assertEquals(2, buf.position());
436         Assert.assertEquals(1, buf.get(0));
437         Assert.assertEquals('A', buf.get(1));
438 
439         buf.sweep();
440         buf.putPrefixedString("A", 2, encoder);
441         Assert.assertEquals(3, buf.position());
442         Assert.assertEquals(0, buf.get(0));
443         Assert.assertEquals(1, buf.get(1));
444         Assert.assertEquals('A', buf.get(2));
445 
446         buf.sweep();
447         buf.putPrefixedString("A", 4, encoder);
448         Assert.assertEquals(5, buf.position());
449         Assert.assertEquals(0, buf.get(0));
450         Assert.assertEquals(0, buf.get(1));
451         Assert.assertEquals(0, buf.get(2));
452         Assert.assertEquals(1, buf.get(3));
453         Assert.assertEquals('A', buf.get(4));
454     }
455 
456     public void testPutPrefixedStringWithPadding() throws Exception {
457         CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder();
458         ByteBuffer buf = ByteBuffer.allocate(16).sweep().setAutoExpand(true);
459 
460         buf.putPrefixedString("A", 1, 2, (byte) 32, encoder);
461         Assert.assertEquals(3, buf.position());
462         Assert.assertEquals(2, buf.get(0));
463         Assert.assertEquals('A', buf.get(1));
464         Assert.assertEquals(' ', buf.get(2));
465 
466         buf.sweep();
467         buf.putPrefixedString("A", 1, 4, (byte) 32, encoder);
468         Assert.assertEquals(5, buf.position());
469         Assert.assertEquals(4, buf.get(0));
470         Assert.assertEquals('A', buf.get(1));
471         Assert.assertEquals(' ', buf.get(2));
472         Assert.assertEquals(' ', buf.get(3));
473         Assert.assertEquals(' ', buf.get(4));
474     }
475 
476     public void testWideUtf8Characters() throws Exception {
477         Runnable r = new Runnable() {
478             public void run() {
479                 ByteBuffer buffer = ByteBuffer.allocate(1);
480                 buffer.setAutoExpand(true);
481 
482                 Charset charset = Charset.forName("UTF-8");
483 
484                 CharsetEncoder encoder = charset.newEncoder();
485 
486                 for (int i = 0; i < 5; i++) {
487                     try {
488                         buffer.putString("\u89d2", encoder);
489                     } catch (CharacterCodingException e) {
490                         fail(e.getMessage());
491                     }
492                 }
493             }
494         };
495 
496         Thread t = new Thread(r);
497         t.setDaemon(true);
498         t.start();
499 
500         for (int i = 0; i < 50; i++) {
501             Thread.sleep(100);
502             if (!t.isAlive()) {
503                 break;
504             }
505         }
506 
507         if (t.isAlive()) {
508             t.interrupt();
509 
510             fail("Went into endless loop trying to encode character");
511         }
512     }
513 
514     public void testObjectSerialization() throws Exception {
515         ByteBuffer buf = ByteBuffer.allocate(16);
516         buf.setAutoExpand(true);
517         List<Object> o = new ArrayList<Object>();
518         o.add(new Date());
519 
520         // Test writing an object.
521         buf.putObject(o);
522 
523         // Test reading an object.
524         buf.clear();
525         Object o2 = buf.getObject();
526         Assert.assertEquals(o, o2);
527 
528         // This assertion is just to make sure that deserialization occurred.
529         Assert.assertNotSame(o, o2);
530     }
531 
532     public void testSweepWithZeros() throws Exception {
533         ByteBuffer buf = ByteBuffer.allocate(4);
534         buf.putInt(0xdeadbeef);
535         buf.clear();
536         Assert.assertEquals(0xdeadbeef, buf.getInt());
537         Assert.assertEquals(4, buf.position());
538         Assert.assertEquals(4, buf.limit());
539 
540         buf.sweep();
541         Assert.assertEquals(0, buf.position());
542         Assert.assertEquals(4, buf.limit());
543         Assert.assertEquals(0x0, buf.getInt());
544     }
545 
546     public void testSweepNonZeros() throws Exception {
547         ByteBuffer buf = ByteBuffer.allocate(4);
548         buf.putInt(0xdeadbeef);
549         buf.clear();
550         Assert.assertEquals(0xdeadbeef, buf.getInt());
551         Assert.assertEquals(4, buf.position());
552         Assert.assertEquals(4, buf.limit());
553 
554         buf.sweep((byte) 0x45);
555         Assert.assertEquals(0, buf.position());
556         Assert.assertEquals(4, buf.limit());
557         Assert.assertEquals(0x45454545, buf.getInt());
558     }
559 
560     public void testWrapNioBuffer() throws Exception {
561         java.nio.ByteBuffer nioBuf = java.nio.ByteBuffer.allocate(10);
562         nioBuf.position(3);
563         nioBuf.limit(7);
564 
565         ByteBuffer buf = ByteBuffer.wrap(nioBuf);
566         Assert.assertEquals(3, buf.position());
567         Assert.assertEquals(7, buf.limit());
568         Assert.assertEquals(10, buf.capacity());
569     }
570 
571     public void testWrapSubArray() throws Exception {
572         byte[] array = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
573 
574         ByteBuffer buf = ByteBuffer.wrap(array, 3, 4);
575         Assert.assertEquals(3, buf.position());
576         Assert.assertEquals(7, buf.limit());
577         Assert.assertEquals(10, buf.capacity());
578 
579         buf.clear();
580         Assert.assertEquals(0, buf.position());
581         Assert.assertEquals(10, buf.limit());
582         Assert.assertEquals(10, buf.capacity());
583     }
584 
585     public void testPoolExpiration() throws Exception {
586         PooledByteBufferAllocator allocator = (PooledByteBufferAllocator) ByteBuffer
587                 .getAllocator();
588 
589         // Make a buffer pooled.
590         ByteBuffer buf = ByteBuffer.allocate(16);
591         buf.release();
592 
593         // Let everything flushed.
594         allocator.setTimeout(1);
595         Thread.sleep(2000);
596 
597         // Make sure old buffers are flushed.
598         Assert.assertNotSame(buf, ByteBuffer.allocate(16));
599 
600         // Make sure new buffers are not flushed.
601         allocator.setTimeout(10);
602         buf = ByteBuffer.allocate(16);
603         buf.release();
604         Thread.sleep(2000);
605         Assert.assertSame(buf.buf(), ByteBuffer.allocate(16).buf());
606 
607         // Return to the default settings
608         allocator.setTimeout(60);
609     }
610 
611     public void testAllocatorDisposal() throws Exception {
612         PooledByteBufferAllocator allocator = (PooledByteBufferAllocator) ByteBuffer
613                 .getAllocator();
614 
615         // dispose() should fail because the allocator is in use.
616         try {
617             allocator.dispose();
618             Assert.fail();
619         } catch (IllegalStateException e) {
620             // OK
621         }
622 
623         // Change the allocator.
624         ByteBuffer.setAllocator(new PooledByteBufferAllocator());
625 
626         // Dispose the old allocator.
627         allocator.dispose();
628 
629         // Allocation request to the disposed allocator should fail.
630         try {
631             allocator.allocate(16, true);
632             Assert.fail();
633         } catch (IllegalStateException e) {
634             // OK
635         }
636     }
637 
638     public void testDuplicate() throws Exception {
639         java.nio.ByteBuffer nioBuf;
640         ByteBuffer original;
641         ByteBuffer duplicate;
642 
643         // Test if the buffer is duplicated correctly.
644         original = ByteBuffer.allocate(16).sweep();
645         nioBuf = original.buf();
646         original.position(4);
647         original.limit(10);
648         duplicate = original.duplicate();
649         original.put(4, (byte) 127);
650         Assert.assertEquals(4, duplicate.position());
651         Assert.assertEquals(10, duplicate.limit());
652         Assert.assertEquals(16, duplicate.capacity());
653         Assert.assertNotSame(original.buf(), duplicate.buf());
654         Assert.assertEquals(127, duplicate.get(4));
655         original.release();
656         duplicate.release();
657 
658         //// Check if pooled correctly.
659         original = ByteBuffer.allocate(16);
660         Assert.assertSame(nioBuf, original.buf());
661         original.release();
662 
663         // Try to release duplicate first.
664         original = ByteBuffer.allocate(16);
665         duplicate = original.duplicate();
666         duplicate.release();
667         original.release();
668 
669         //// Check if pooled correctly.
670         original = ByteBuffer.allocate(16);
671         Assert.assertSame(nioBuf, original.buf());
672         original.release();
673 
674         // Test a duplicate of a duplicate.
675         original = ByteBuffer.allocate(16);
676         duplicate = original.duplicate();
677         ByteBuffer anotherDuplicate = duplicate.duplicate();
678         anotherDuplicate.release();
679         original.release();
680         duplicate.release();
681         try {
682             duplicate.release();
683             Assert.fail();
684         } catch (IllegalStateException e) {
685             // OK
686         }
687         try {
688             anotherDuplicate.release();
689             Assert.fail();
690         } catch (IllegalStateException e) {
691             // OK
692         }
693 
694         //// Check if pooled correctly.
695         original = ByteBuffer.allocate(16);
696         Assert.assertSame(nioBuf, original.buf());
697         original.release();
698 
699         // Try to expand.
700         try {
701             original = ByteBuffer.allocate(16);
702             duplicate = original.duplicate();
703             duplicate.setAutoExpand(true);
704             duplicate.putString("A very very very very looooooong string",
705                     Charset.forName("ISO-8859-1").newEncoder());
706             Assert.fail();
707         } catch (IllegalStateException e) {
708             // OK
709         }
710     }
711 
712     public void testSlice() throws Exception {
713         ByteBuffer original;
714         ByteBuffer slice;
715 
716         // Test if the buffer is sliced correctly.
717         original = ByteBuffer.allocate(16).sweep();
718         original.position(4);
719         original.limit(10);
720         slice = original.slice();
721         original.put(4, (byte) 127);
722         Assert.assertEquals(0, slice.position());
723         Assert.assertEquals(6, slice.limit());
724         Assert.assertEquals(6, slice.capacity());
725         Assert.assertNotSame(original.buf(), slice.buf());
726         Assert.assertEquals(127, slice.get(0));
727         original.release();
728         slice.release();
729     }
730 
731     public void testReadOnlyBuffer() throws Exception {
732         ByteBuffer original;
733         ByteBuffer duplicate;
734 
735         // Test if the buffer is duplicated correctly.
736         original = ByteBuffer.allocate(16).sweep();
737         original.position(4);
738         original.limit(10);
739         duplicate = original.asReadOnlyBuffer();
740         original.put(4, (byte) 127);
741         Assert.assertEquals(4, duplicate.position());
742         Assert.assertEquals(10, duplicate.limit());
743         Assert.assertEquals(16, duplicate.capacity());
744         Assert.assertNotSame(original.buf(), duplicate.buf());
745         Assert.assertEquals(127, duplicate.get(4));
746         original.release();
747         duplicate.release();
748 
749         // Try to expand.
750         try {
751             original = ByteBuffer.allocate(16);
752             duplicate = original.asReadOnlyBuffer();
753             duplicate.putString("A very very very very looooooong string",
754                     Charset.forName("ISO-8859-1").newEncoder());
755             Assert.fail();
756         } catch (ReadOnlyBufferException e) {
757             // OK
758         }
759     }
760 
761     public void testGetUnsigned() throws Exception {
762         ByteBuffer buf = ByteBuffer.allocate(16);
763         buf.put((byte) 0xA4);
764         buf.put((byte) 0xD0);
765         buf.put((byte) 0xB3);
766         buf.put((byte) 0xCD);
767         buf.flip();
768 
769         buf.order(ByteOrder.LITTLE_ENDIAN);
770 
771         buf.mark();
772         Assert.assertEquals(0xA4, buf.getUnsigned());
773         buf.reset();
774         Assert.assertEquals(0xD0A4, buf.getUnsignedShort());
775         buf.reset();
776         Assert.assertEquals(0xCDB3D0A4L, buf.getUnsignedInt());
777     }
778 }