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 java.nio.BufferOverflowException;
23  import java.nio.ByteOrder;
24  import java.nio.ReadOnlyBufferException;
25  import java.nio.charset.CharacterCodingException;
26  import java.nio.charset.Charset;
27  import java.nio.charset.CharsetDecoder;
28  import java.nio.charset.CharsetEncoder;
29  import java.util.ArrayList;
30  import java.util.Date;
31  import java.util.List;
32  
33  import junit.framework.Assert;
34  import junit.framework.TestCase;
35  
36  /**
37   * Tests {@link ByteBuffer}.
38   *
39   * @author The Apache Directory Project (mina-dev@directory.apache.org)
40   * @version $Rev: 595517 $, $Date: 2007-11-16 10:31:56 +0900 (금, 16 11월 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 o = new ArrayList();
518         o.add(new Date());
519         o.add(long.class);
520 
521         // Test writing an object.
522         buf.putObject(o);
523 
524         // Test reading an object.
525         buf.clear();
526         Object o2 = buf.getObject();
527         Assert.assertEquals(o, o2);
528 
529         // This assertion is just to make sure that deserialization occurred.
530         Assert.assertNotSame(o, o2);
531     }
532 
533     public void testSweepWithZeros() throws Exception {
534         ByteBuffer buf = ByteBuffer.allocate(4);
535         buf.putInt(0xdeadbeef);
536         buf.clear();
537         Assert.assertEquals(0xdeadbeef, buf.getInt());
538         Assert.assertEquals(4, buf.position());
539         Assert.assertEquals(4, buf.limit());
540 
541         buf.sweep();
542         Assert.assertEquals(0, buf.position());
543         Assert.assertEquals(4, buf.limit());
544         Assert.assertEquals(0x0, buf.getInt());
545     }
546 
547     public void testSweepNonZeros() throws Exception {
548         ByteBuffer buf = ByteBuffer.allocate(4);
549         buf.putInt(0xdeadbeef);
550         buf.clear();
551         Assert.assertEquals(0xdeadbeef, buf.getInt());
552         Assert.assertEquals(4, buf.position());
553         Assert.assertEquals(4, buf.limit());
554 
555         buf.sweep((byte) 0x45);
556         Assert.assertEquals(0, buf.position());
557         Assert.assertEquals(4, buf.limit());
558         Assert.assertEquals(0x45454545, buf.getInt());
559     }
560 
561     public void testWrapNioBuffer() throws Exception {
562         java.nio.ByteBuffer nioBuf = java.nio.ByteBuffer.allocate(10);
563         nioBuf.position(3);
564         nioBuf.limit(7);
565 
566         ByteBuffer buf = ByteBuffer.wrap(nioBuf);
567         Assert.assertEquals(3, buf.position());
568         Assert.assertEquals(7, buf.limit());
569         Assert.assertEquals(10, buf.capacity());
570     }
571 
572     public void testWrapSubArray() throws Exception {
573         byte[] array = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
574 
575         ByteBuffer buf = ByteBuffer.wrap(array, 3, 4);
576         Assert.assertEquals(3, buf.position());
577         Assert.assertEquals(7, buf.limit());
578         Assert.assertEquals(10, buf.capacity());
579 
580         buf.clear();
581         Assert.assertEquals(0, buf.position());
582         Assert.assertEquals(10, buf.limit());
583         Assert.assertEquals(10, buf.capacity());
584     }
585 
586     public void testPoolExpiration() throws Exception {
587         PooledByteBufferAllocator allocator = (PooledByteBufferAllocator) ByteBuffer
588                 .getAllocator();
589 
590         // Make a buffer pooled.
591         ByteBuffer buf = ByteBuffer.allocate(16);
592         buf.release();
593 
594         // Let everything flushed.
595         allocator.setTimeout(1);
596         Thread.sleep(2000);
597 
598         // Make sure old buffers are flushed.
599         Assert.assertNotSame(buf, ByteBuffer.allocate(16));
600 
601         // Make sure new buffers are not flushed.
602         allocator.setTimeout(10);
603         buf = ByteBuffer.allocate(16);
604         buf.release();
605         Thread.sleep(2000);
606         Assert.assertSame(buf.buf(), ByteBuffer.allocate(16).buf());
607 
608         // Return to the default settings
609         allocator.setTimeout(60);
610     }
611 
612     public void testAllocatorDisposal() throws Exception {
613         PooledByteBufferAllocator allocator = (PooledByteBufferAllocator) ByteBuffer
614                 .getAllocator();
615 
616         // dispose() should fail because the allocator is in use.
617         try {
618             allocator.dispose();
619             Assert.fail();
620         } catch (IllegalStateException e) {
621             // OK
622         }
623 
624         // Change the allocator.
625         ByteBuffer.setAllocator(new PooledByteBufferAllocator());
626 
627         // Dispose the old allocator.
628         allocator.dispose();
629 
630         // Allocation request to the disposed allocator should fail.
631         try {
632             allocator.allocate(16, true);
633             Assert.fail();
634         } catch (IllegalStateException e) {
635             // OK
636         }
637     }
638 
639     public void testDuplicate() throws Exception {
640         java.nio.ByteBuffer nioBuf;
641         ByteBuffer original;
642         ByteBuffer duplicate;
643 
644         // Test if the buffer is duplicated correctly.
645         original = ByteBuffer.allocate(16).sweep();
646         nioBuf = original.buf();
647         original.position(4);
648         original.limit(10);
649         duplicate = original.duplicate();
650         original.put(4, (byte) 127);
651         Assert.assertEquals(4, duplicate.position());
652         Assert.assertEquals(10, duplicate.limit());
653         Assert.assertEquals(16, duplicate.capacity());
654         Assert.assertNotSame(original.buf(), duplicate.buf());
655         Assert.assertEquals(127, duplicate.get(4));
656         original.release();
657         duplicate.release();
658 
659         //// Check if pooled correctly.
660         original = ByteBuffer.allocate(16);
661         Assert.assertSame(nioBuf, original.buf());
662         original.release();
663 
664         // Try to release duplicate first.
665         original = ByteBuffer.allocate(16);
666         duplicate = original.duplicate();
667         duplicate.release();
668         original.release();
669 
670         //// Check if pooled correctly.
671         original = ByteBuffer.allocate(16);
672         Assert.assertSame(nioBuf, original.buf());
673         original.release();
674 
675         // Test a duplicate of a duplicate.
676         original = ByteBuffer.allocate(16);
677         duplicate = original.duplicate();
678         ByteBuffer anotherDuplicate = duplicate.duplicate();
679         anotherDuplicate.release();
680         original.release();
681         duplicate.release();
682         try {
683             duplicate.release();
684             Assert.fail();
685         } catch (IllegalStateException e) {
686             // OK
687         }
688         try {
689             anotherDuplicate.release();
690             Assert.fail();
691         } catch (IllegalStateException e) {
692             // OK
693         }
694 
695         //// Check if pooled correctly.
696         original = ByteBuffer.allocate(16);
697         Assert.assertSame(nioBuf, original.buf());
698         original.release();
699 
700         // Try to expand.
701         try {
702             original = ByteBuffer.allocate(16);
703             duplicate = original.duplicate();
704             duplicate.setAutoExpand(true);
705             duplicate.putString("A very very very very looooooong string",
706                     Charset.forName("ISO-8859-1").newEncoder());
707             Assert.fail();
708         } catch (IllegalStateException e) {
709             // OK
710         }
711     }
712 
713     public void testSlice() throws Exception {
714         ByteBuffer original;
715         ByteBuffer slice;
716 
717         // Test if the buffer is sliced correctly.
718         original = ByteBuffer.allocate(16).sweep();
719         original.position(4);
720         original.limit(10);
721         slice = original.slice();
722         original.put(4, (byte) 127);
723         Assert.assertEquals(0, slice.position());
724         Assert.assertEquals(6, slice.limit());
725         Assert.assertEquals(6, slice.capacity());
726         Assert.assertNotSame(original.buf(), slice.buf());
727         Assert.assertEquals(127, slice.get(0));
728         original.release();
729         slice.release();
730     }
731 
732     public void testReadOnlyBuffer() throws Exception {
733         ByteBuffer original;
734         ByteBuffer duplicate;
735 
736         // Test if the buffer is duplicated correctly.
737         original = ByteBuffer.allocate(16).sweep();
738         original.position(4);
739         original.limit(10);
740         duplicate = original.asReadOnlyBuffer();
741         original.put(4, (byte) 127);
742         Assert.assertEquals(4, duplicate.position());
743         Assert.assertEquals(10, duplicate.limit());
744         Assert.assertEquals(16, duplicate.capacity());
745         Assert.assertNotSame(original.buf(), duplicate.buf());
746         Assert.assertEquals(127, duplicate.get(4));
747         original.release();
748         duplicate.release();
749 
750         // Try to expand.
751         try {
752             original = ByteBuffer.allocate(16);
753             duplicate = original.asReadOnlyBuffer();
754             duplicate.putString("A very very very very looooooong string",
755                     Charset.forName("ISO-8859-1").newEncoder());
756             Assert.fail();
757         } catch (ReadOnlyBufferException e) {
758             // OK
759         }
760     }
761 
762     public void testGetUnsigned() throws Exception {
763         ByteBuffer buf = ByteBuffer.allocate(16);
764         buf.put((byte) 0xA4);
765         buf.put((byte) 0xD0);
766         buf.put((byte) 0xB3);
767         buf.put((byte) 0xCD);
768         buf.flip();
769 
770         buf.order(ByteOrder.LITTLE_ENDIAN);
771 
772         buf.mark();
773         Assert.assertEquals(0xA4, buf.getUnsigned());
774         buf.reset();
775         Assert.assertEquals(0xD0A4, buf.getUnsignedShort());
776         buf.reset();
777         Assert.assertEquals(0xCDB3D0A4L, buf.getUnsignedInt());
778     }
779 }