Source Code Cross Referenced for ByteBufferTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » nio » tests » java » nio » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.nio.tests.java.nio 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
0003:         *  contributor license agreements.  See the NOTICE file distributed with
0004:         *  this work for additional information regarding copyright ownership.
0005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
0006:         *  (the "License"); you may not use this file except in compliance with
0007:         *  the License.  You may obtain a copy of the License at
0008:         *
0009:         *     http://www.apache.org/licenses/LICENSE-2.0
0010:         *
0011:         *  Unless required by applicable law or agreed to in writing, software
0012:         *  distributed under the License is distributed on an "AS IS" BASIS,
0013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         *  See the License for the specific language governing permissions and
0015:         *  limitations under the License.
0016:         */
0017:
0018:        package org.apache.harmony.nio.tests.java.nio;
0019:
0020:        import java.nio.BufferOverflowException;
0021:        import java.nio.BufferUnderflowException;
0022:        import java.nio.ByteBuffer;
0023:        import java.nio.ByteOrder;
0024:        import java.nio.CharBuffer;
0025:        import java.nio.DoubleBuffer;
0026:        import java.nio.FloatBuffer;
0027:        import java.nio.IntBuffer;
0028:        import java.nio.InvalidMarkException;
0029:        import java.nio.LongBuffer;
0030:        import java.nio.ReadOnlyBufferException;
0031:        import java.nio.ShortBuffer;
0032:        import java.util.Arrays;
0033:
0034:        /**
0035:         * Tests java.nio.ByteBuffer
0036:         * 
0037:         */
0038:        public class ByteBufferTest extends AbstractBufferTest {
0039:            protected static final int SMALL_TEST_LENGTH = 5;
0040:            protected static final int BUFFER_LENGTH = 250;
0041:
0042:            protected ByteBuffer buf;
0043:
0044:            protected void setUp() throws Exception {
0045:                buf = ByteBuffer.allocate(10);
0046:                baseBuf = buf;
0047:            }
0048:
0049:            protected void tearDown() throws Exception {
0050:                super .tearDown();
0051:            }
0052:
0053:            public void testArray() {
0054:                if (buf.hasArray()) {
0055:                    byte array[] = buf.array();
0056:                    assertContentEquals(buf, array, buf.arrayOffset(), buf
0057:                            .capacity());
0058:
0059:                    loadTestData1(array, buf.arrayOffset(), buf.capacity());
0060:                    assertContentEquals(buf, array, buf.arrayOffset(), buf
0061:                            .capacity());
0062:
0063:                    loadTestData2(array, buf.arrayOffset(), buf.capacity());
0064:                    assertContentEquals(buf, array, buf.arrayOffset(), buf
0065:                            .capacity());
0066:
0067:                    loadTestData1(buf);
0068:                    assertContentEquals(buf, array, buf.arrayOffset(), buf
0069:                            .capacity());
0070:
0071:                    loadTestData2(buf);
0072:                    assertContentEquals(buf, array, buf.arrayOffset(), buf
0073:                            .capacity());
0074:                } else {
0075:                    if (buf.isReadOnly()) {
0076:                        try {
0077:                            buf.array();
0078:                            fail("Should throw Exception"); //$NON-NLS-1$
0079:                        } catch (UnsupportedOperationException e) {
0080:                            // expected
0081:                            // Note:can not tell when to throw 
0082:                            // UnsupportedOperationException
0083:                            // or ReadOnlyBufferException, so catch all.
0084:                        }
0085:                    } else {
0086:                        try {
0087:                            buf.array();
0088:                            fail("Should throw Exception"); //$NON-NLS-1$
0089:                        } catch (UnsupportedOperationException e) {
0090:                            // expected
0091:                        }
0092:                    }
0093:                }
0094:            }
0095:
0096:            public void testArrayOffset() {
0097:                if (buf.hasArray()) {
0098:                    byte array[] = buf.array();
0099:                    assertContentEquals(buf, array, buf.arrayOffset(), buf
0100:                            .capacity());
0101:
0102:                    loadTestData1(array, buf.arrayOffset(), buf.capacity());
0103:                    assertContentEquals(buf, array, buf.arrayOffset(), buf
0104:                            .capacity());
0105:
0106:                    loadTestData2(array, buf.arrayOffset(), buf.capacity());
0107:                    assertContentEquals(buf, array, buf.arrayOffset(), buf
0108:                            .capacity());
0109:
0110:                    loadTestData1(buf);
0111:                    assertContentEquals(buf, array, buf.arrayOffset(), buf
0112:                            .capacity());
0113:
0114:                    loadTestData2(buf);
0115:                    assertContentEquals(buf, array, buf.arrayOffset(), buf
0116:                            .capacity());
0117:                } else {
0118:                    if (buf.isReadOnly()) {
0119:                        try {
0120:                            buf.arrayOffset();
0121:                            fail("Should throw Exception"); //$NON-NLS-1$
0122:                        } catch (UnsupportedOperationException e) {
0123:                            // expected
0124:                            // Note:can not tell when to throw 
0125:                            // UnsupportedOperationException
0126:                            // or ReadOnlyBufferException, so catch all.
0127:                        }
0128:                    } else {
0129:                        try {
0130:                            buf.arrayOffset();
0131:                            fail("Should throw Exception"); //$NON-NLS-1$
0132:                        } catch (UnsupportedOperationException e) {
0133:                            // expected
0134:                        }
0135:                    }
0136:                }
0137:            }
0138:
0139:            public void testAsReadOnlyBuffer() {
0140:                buf.clear();
0141:                buf.mark();
0142:                buf.position(buf.limit());
0143:
0144:                // readonly's contents should be the same as buf
0145:                ByteBuffer readonly = buf.asReadOnlyBuffer();
0146:                assertNotSame(buf, readonly);
0147:                assertTrue(readonly.isReadOnly());
0148:                assertEquals(buf.position(), readonly.position());
0149:                assertEquals(buf.limit(), readonly.limit());
0150:                assertEquals(buf.isDirect(), readonly.isDirect());
0151:                assertEquals(buf.order(), readonly.order());
0152:                assertContentEquals(buf, readonly);
0153:
0154:                // readonly's position, mark, and limit should be independent to buf
0155:                readonly.reset();
0156:                assertEquals(readonly.position(), 0);
0157:                readonly.clear();
0158:                assertEquals(buf.position(), buf.limit());
0159:                buf.reset();
0160:                assertEquals(buf.position(), 0);
0161:            }
0162:
0163:            public void testCompact() {
0164:                if (buf.isReadOnly()) {
0165:                    try {
0166:                        buf.compact();
0167:                        fail("Should throw Exception"); //$NON-NLS-1$
0168:                    } catch (ReadOnlyBufferException e) {
0169:                        // expected
0170:                    }
0171:                    return;
0172:                }
0173:
0174:                // case: buffer is full
0175:                buf.clear();
0176:                buf.mark();
0177:                loadTestData1(buf);
0178:                ByteBuffer ret = buf.compact();
0179:                assertSame(ret, buf);
0180:                assertEquals(buf.position(), buf.capacity());
0181:                assertEquals(buf.limit(), buf.capacity());
0182:                assertContentLikeTestData1(buf, 0, (byte) 0, buf.capacity());
0183:                try {
0184:                    buf.reset();
0185:                    fail("Should throw Exception"); //$NON-NLS-1$
0186:                } catch (InvalidMarkException e) {
0187:                    // expected
0188:                }
0189:
0190:                // case: buffer is empty
0191:                buf.position(0);
0192:                buf.limit(0);
0193:                buf.mark();
0194:                ret = buf.compact();
0195:                assertSame(ret, buf);
0196:                assertEquals(buf.position(), 0);
0197:                assertEquals(buf.limit(), buf.capacity());
0198:                assertContentLikeTestData1(buf, 0, (byte) 0, buf.capacity());
0199:                try {
0200:                    buf.reset();
0201:                    fail("Should throw Exception"); //$NON-NLS-1$
0202:                } catch (InvalidMarkException e) {
0203:                    // expected
0204:                }
0205:
0206:                // case: normal
0207:                assertTrue(buf.capacity() > SMALL_TEST_LENGTH);
0208:                buf.position(1);
0209:                buf.limit(SMALL_TEST_LENGTH);
0210:                buf.mark();
0211:                ret = buf.compact();
0212:                assertSame(ret, buf);
0213:                assertEquals(buf.position(), 4);
0214:                assertEquals(buf.limit(), buf.capacity());
0215:                assertContentLikeTestData1(buf, 0, (byte) 1, 4);
0216:                try {
0217:                    buf.reset();
0218:                    fail("Should throw Exception"); //$NON-NLS-1$
0219:                } catch (InvalidMarkException e) {
0220:                    // expected
0221:                }
0222:            }
0223:
0224:            public void testCompareTo() {
0225:                // compare to self
0226:                assertEquals(0, buf.compareTo(buf));
0227:
0228:                // normal cases
0229:                if (!buf.isReadOnly()) {
0230:                    assertTrue(buf.capacity() > SMALL_TEST_LENGTH);
0231:                    buf.clear();
0232:                    ByteBuffer other = ByteBuffer.allocate(buf.capacity());
0233:                    loadTestData1(buf);
0234:                    loadTestData1(other);
0235:                    assertEquals(0, buf.compareTo(other));
0236:                    assertEquals(0, other.compareTo(buf));
0237:                    buf.position(1);
0238:                    assertTrue(buf.compareTo(other) > 0);
0239:                    assertTrue(other.compareTo(buf) < 0);
0240:                    other.position(2);
0241:                    assertTrue(buf.compareTo(other) < 0);
0242:                    assertTrue(other.compareTo(buf) > 0);
0243:                    buf.position(2);
0244:                    other.limit(SMALL_TEST_LENGTH);
0245:                    assertTrue(buf.compareTo(other) > 0);
0246:                    assertTrue(other.compareTo(buf) < 0);
0247:                }
0248:
0249:                assertTrue(ByteBuffer.wrap(new byte[21]).compareTo(
0250:                        ByteBuffer.allocateDirect(21)) == 0);
0251:            }
0252:
0253:            public void testDuplicate() {
0254:                buf.clear();
0255:                buf.mark();
0256:                buf.position(buf.limit());
0257:
0258:                // duplicate's contents should be the same as buf
0259:                ByteBuffer duplicate = buf.duplicate();
0260:                assertNotSame(buf, duplicate);
0261:                assertEquals(buf.position(), duplicate.position());
0262:                assertEquals(buf.limit(), duplicate.limit());
0263:                assertEquals(buf.isReadOnly(), duplicate.isReadOnly());
0264:                assertEquals(buf.isDirect(), duplicate.isDirect());
0265:                assertEquals(buf.order(), duplicate.order());
0266:                assertContentEquals(buf, duplicate);
0267:
0268:                // duplicate's position, mark, and limit should be independent to buf
0269:                duplicate.reset();
0270:                assertEquals(duplicate.position(), 0);
0271:                duplicate.clear();
0272:                assertEquals(buf.position(), buf.limit());
0273:                buf.reset();
0274:                assertEquals(buf.position(), 0);
0275:
0276:                // duplicate share the same content with buf
0277:                if (!duplicate.isReadOnly()) {
0278:                    loadTestData1(buf);
0279:                    assertContentEquals(buf, duplicate);
0280:                    loadTestData2(duplicate);
0281:                    assertContentEquals(buf, duplicate);
0282:                }
0283:            }
0284:
0285:            public void testEquals() {
0286:                // equal to self
0287:                assertTrue(buf.equals(buf));
0288:                ByteBuffer readonly = buf.asReadOnlyBuffer();
0289:                assertTrue(buf.equals(readonly));
0290:                ByteBuffer duplicate = buf.duplicate();
0291:                assertTrue(buf.equals(duplicate));
0292:
0293:                // always false, if type mismatch
0294:                assertFalse(buf.equals(Boolean.TRUE));
0295:
0296:                assertTrue(buf.capacity() > SMALL_TEST_LENGTH);
0297:
0298:                buf.limit(buf.capacity()).position(0);
0299:                readonly.limit(readonly.capacity()).position(1);
0300:                assertFalse(buf.equals(readonly));
0301:
0302:                buf.limit(buf.capacity() - 1).position(0);
0303:                duplicate.limit(duplicate.capacity()).position(0);
0304:                assertFalse(buf.equals(duplicate));
0305:            }
0306:
0307:            /*
0308:             * Class under test for byte get()
0309:             */
0310:            public void testGet() {
0311:                buf.clear();
0312:                for (int i = 0; i < buf.capacity(); i++) {
0313:                    assertEquals(buf.position(), i);
0314:                    assertEquals(buf.get(), buf.get(i));
0315:                }
0316:                try {
0317:                    buf.get();
0318:                    fail("Should throw Exception"); //$NON-NLS-1$
0319:                } catch (BufferUnderflowException e) {
0320:                    // expected
0321:                }
0322:            }
0323:
0324:            /*
0325:             * Class under test for java.nio.ByteBuffer get(byte[])
0326:             */
0327:            public void testGetbyteArray() {
0328:                byte array[] = new byte[1];
0329:                buf.clear();
0330:                for (int i = 0; i < buf.capacity(); i++) {
0331:                    assertEquals(buf.position(), i);
0332:                    ByteBuffer ret = buf.get(array);
0333:                    assertEquals(array[0], buf.get(i));
0334:                    assertSame(ret, buf);
0335:                }
0336:                try {
0337:                    buf.get(array);
0338:                    fail("Should throw Exception"); //$NON-NLS-1$
0339:                } catch (BufferUnderflowException e) {
0340:                    // expected
0341:                }
0342:                try {
0343:                    buf.get((byte[]) null);
0344:                    fail("Should throw Exception"); //$NON-NLS-1$
0345:                } catch (NullPointerException e) {
0346:                    // expected
0347:                }
0348:            }
0349:
0350:            /*
0351:             * Class under test for java.nio.ByteBuffer get(byte[], int, int)
0352:             */
0353:            public void testGetbyteArrayintint() {
0354:                buf.clear();
0355:                byte array[] = new byte[buf.capacity()];
0356:
0357:                try {
0358:                    buf
0359:                            .get(new byte[buf.capacity() + 1], 0, buf
0360:                                    .capacity() + 1);
0361:                    fail("Should throw Exception"); //$NON-NLS-1$
0362:                } catch (BufferUnderflowException e) {
0363:                    // expected
0364:                }
0365:                assertEquals(buf.position(), 0);
0366:                try {
0367:                    buf.get(array, -1, array.length);
0368:                    fail("Should throw Exception"); //$NON-NLS-1$
0369:                } catch (IndexOutOfBoundsException e) {
0370:                    // expected
0371:                }
0372:                buf.get(array, array.length, 0);
0373:                try {
0374:                    buf.get(array, array.length + 1, 1);
0375:                    fail("Should throw Exception"); //$NON-NLS-1$
0376:                } catch (IndexOutOfBoundsException e) {
0377:                    // expected
0378:                }
0379:                assertEquals(buf.position(), 0);
0380:                try {
0381:                    buf.get(array, 2, -1);
0382:                    fail("Should throw Exception"); //$NON-NLS-1$
0383:                } catch (IndexOutOfBoundsException e) {
0384:                    // expected
0385:                }
0386:                try {
0387:                    buf.get(array, 2, array.length);
0388:                    fail("Should throw Exception"); //$NON-NLS-1$
0389:                } catch (IndexOutOfBoundsException e) {
0390:                    // expected
0391:                }
0392:                try {
0393:                    buf.get((byte[]) null, -1, 0);
0394:                    fail("Should throw Exception"); //$NON-NLS-1$
0395:                } catch (NullPointerException e) {
0396:                    // expected
0397:                }
0398:                try {
0399:                    buf.get(array, 1, Integer.MAX_VALUE);
0400:                    fail("Should throw Exception"); //$NON-NLS-1$
0401:                } catch (IndexOutOfBoundsException e) {
0402:                    // expected
0403:                }
0404:                try {
0405:                    buf.get(array, Integer.MAX_VALUE, 1);
0406:                    fail("Should throw Exception"); //$NON-NLS-1$
0407:                } catch (IndexOutOfBoundsException e) {
0408:                    // expected
0409:                }
0410:                assertEquals(buf.position(), 0);
0411:
0412:                buf.clear();
0413:                ByteBuffer ret = buf.get(array, 0, array.length);
0414:                assertEquals(buf.position(), buf.capacity());
0415:                assertContentEquals(buf, array, 0, array.length);
0416:                assertSame(ret, buf);
0417:            }
0418:
0419:            /*
0420:             * Class under test for byte get(int)
0421:             */
0422:            public void testGetint() {
0423:                buf.clear();
0424:                for (int i = 0; i < buf.capacity(); i++) {
0425:                    assertEquals(buf.position(), i);
0426:                    assertEquals(buf.get(), buf.get(i));
0427:                }
0428:                try {
0429:                    buf.get(-1);
0430:                    fail("Should throw Exception"); //$NON-NLS-1$
0431:                } catch (IndexOutOfBoundsException e) {
0432:                    // expected
0433:                }
0434:                try {
0435:                    buf.get(buf.limit());
0436:                    fail("Should throw Exception"); //$NON-NLS-1$
0437:                } catch (IndexOutOfBoundsException e) {
0438:                    // expected
0439:                }
0440:            }
0441:
0442:            public void testHasArray() {
0443:                if (buf.hasArray()) {
0444:                    assertNotNull(buf.array());
0445:                } else {
0446:                    if (buf.isReadOnly()) {
0447:                        try {
0448:                            buf.array();
0449:                            fail("Should throw Exception"); //$NON-NLS-1$
0450:                        } catch (UnsupportedOperationException e) {
0451:                            // expected
0452:                            // Note:can not tell when to throw 
0453:                            // UnsupportedOperationException
0454:                            // or ReadOnlyBufferException, so catch all.
0455:                        }
0456:                    } else {
0457:                        try {
0458:                            buf.array();
0459:                            fail("Should throw Exception"); //$NON-NLS-1$
0460:                        } catch (UnsupportedOperationException e) {
0461:                            // expected
0462:                        }
0463:                    }
0464:                }
0465:            }
0466:
0467:            public void testHashCode() {
0468:                buf.clear();
0469:                loadTestData1(buf);
0470:                ByteBuffer readonly = buf.asReadOnlyBuffer();
0471:                ByteBuffer duplicate = buf.duplicate();
0472:                assertTrue(buf.hashCode() == readonly.hashCode());
0473:                assertTrue(buf.capacity() > SMALL_TEST_LENGTH);
0474:                duplicate.position(buf.capacity() / 2);
0475:                assertTrue(buf.hashCode() != duplicate.hashCode());
0476:            }
0477:
0478:            //for the testHashCode() method of readonly subclasses
0479:            protected void readOnlyHashCode() {
0480:                //create a new buffer initiated with some data 
0481:                ByteBuffer buf = ByteBuffer.allocate(BUFFER_LENGTH);
0482:                loadTestData1(buf);
0483:                buf = buf.asReadOnlyBuffer();
0484:                buf.clear();
0485:                ByteBuffer readonly = buf.asReadOnlyBuffer();
0486:                ByteBuffer duplicate = buf.duplicate();
0487:                assertEquals(buf.hashCode(), readonly.hashCode());
0488:                duplicate.position(buf.capacity() / 2);
0489:                assertTrue(buf.hashCode() != duplicate.hashCode());
0490:            }
0491:
0492:            public void testIsDirect() {
0493:                buf.isDirect();
0494:            }
0495:
0496:            public void testOrder() {
0497:                // BIG_ENDIAN is the default byte order
0498:                assertEquals(ByteOrder.BIG_ENDIAN, buf.order());
0499:
0500:                buf.order(ByteOrder.LITTLE_ENDIAN);
0501:                assertEquals(ByteOrder.LITTLE_ENDIAN, buf.order());
0502:
0503:                buf.order(ByteOrder.BIG_ENDIAN);
0504:                assertEquals(ByteOrder.BIG_ENDIAN, buf.order());
0505:
0506:                // Regression test for HARMONY-798
0507:                buf.order((ByteOrder) null);
0508:                assertEquals(ByteOrder.LITTLE_ENDIAN, buf.order());
0509:
0510:                buf.order(ByteOrder.BIG_ENDIAN);
0511:            }
0512:
0513:            /*
0514:             * Class under test for java.nio.ByteBuffer put(byte)
0515:             */
0516:            public void testPutbyte() {
0517:                if (buf.isReadOnly()) {
0518:                    try {
0519:                        buf.clear();
0520:                        buf.put((byte) 0);
0521:                        fail("Should throw Exception"); //$NON-NLS-1$
0522:                    } catch (ReadOnlyBufferException e) {
0523:                        // expected
0524:                    }
0525:                    return;
0526:                }
0527:
0528:                buf.clear();
0529:                for (int i = 0; i < buf.capacity(); i++) {
0530:                    assertEquals(buf.position(), i);
0531:                    ByteBuffer ret = buf.put((byte) i);
0532:                    assertEquals(buf.get(i), (byte) i);
0533:                    assertSame(ret, buf);
0534:                }
0535:                try {
0536:                    buf.put((byte) 0);
0537:                    fail("Should throw Exception"); //$NON-NLS-1$
0538:                } catch (BufferOverflowException e) {
0539:                    // expected
0540:                }
0541:            }
0542:
0543:            /*
0544:             * Class under test for java.nio.ByteBuffer put(byte[])
0545:             */
0546:            public void testPutbyteArray() {
0547:                byte array[] = new byte[1];
0548:                if (buf.isReadOnly()) {
0549:                    try {
0550:                        buf.put(array);
0551:                        fail("Should throw Exception"); //$NON-NLS-1$
0552:                    } catch (ReadOnlyBufferException e) {
0553:                        // expected
0554:                    }
0555:                    return;
0556:                }
0557:
0558:                buf.clear();
0559:                for (int i = 0; i < buf.capacity(); i++) {
0560:                    assertEquals(buf.position(), i);
0561:                    array[0] = (byte) i;
0562:                    ByteBuffer ret = buf.put(array);
0563:                    assertEquals(buf.get(i), (byte) i);
0564:                    assertSame(ret, buf);
0565:                }
0566:                try {
0567:                    buf.put(array);
0568:                    fail("Should throw Exception"); //$NON-NLS-1$
0569:                } catch (BufferOverflowException e) {
0570:                    // expected
0571:                }
0572:                try {
0573:                    buf.put((byte[]) null);
0574:                    fail("Should throw Exception"); //$NON-NLS-1$
0575:                } catch (NullPointerException e) {
0576:                    // expected
0577:                }
0578:            }
0579:
0580:            /*
0581:             * Class under test for java.nio.ByteBuffer put(byte[], int, int)
0582:             */
0583:            public void testPutbyteArrayintint() {
0584:                buf.clear();
0585:                byte array[] = new byte[buf.capacity()];
0586:                if (buf.isReadOnly()) {
0587:                    try {
0588:                        buf.put(array, 0, array.length);
0589:                        fail("Should throw Exception"); //$NON-NLS-1$
0590:                    } catch (ReadOnlyBufferException e) {
0591:                        // expected
0592:                    }
0593:                    return;
0594:                }
0595:
0596:                try {
0597:                    buf
0598:                            .put(new byte[buf.capacity() + 1], 0, buf
0599:                                    .capacity() + 1);
0600:                    fail("Should throw Exception"); //$NON-NLS-1$
0601:                } catch (BufferOverflowException e) {
0602:                    // expected
0603:                }
0604:                assertEquals(buf.position(), 0);
0605:                try {
0606:                    buf.put(array, -1, array.length);
0607:                    fail("Should throw Exception"); //$NON-NLS-1$
0608:                } catch (IndexOutOfBoundsException e) {
0609:                    // expected
0610:                }
0611:                try {
0612:                    buf.put(array, array.length + 1, 0);
0613:                    fail("Should throw Exception"); //$NON-NLS-1$
0614:                } catch (IndexOutOfBoundsException e) {
0615:                    // expected
0616:                }
0617:                buf.put(array, array.length, 0);
0618:                assertEquals(buf.position(), 0);
0619:                try {
0620:                    buf.put(array, 0, -1);
0621:                    fail("Should throw Exception"); //$NON-NLS-1$
0622:                } catch (IndexOutOfBoundsException e) {
0623:                    // expected
0624:                }
0625:                try {
0626:                    buf.put(array, 2, array.length);
0627:                    fail("Should throw Exception"); //$NON-NLS-1$
0628:                } catch (IndexOutOfBoundsException e) {
0629:                    // expected
0630:                }
0631:
0632:                try {
0633:                    buf.put(array, 2, Integer.MAX_VALUE);
0634:                    fail("Should throw Exception"); //$NON-NLS-1$
0635:                } catch (IndexOutOfBoundsException e) {
0636:                    // expected
0637:                }
0638:                try {
0639:                    buf.put(array, Integer.MAX_VALUE, 1);
0640:                    fail("Should throw Exception"); //$NON-NLS-1$
0641:                } catch (IndexOutOfBoundsException e) {
0642:                    // expected
0643:                }
0644:                try {
0645:                    buf.put((byte[]) null, 2, Integer.MAX_VALUE);
0646:                    fail("Should throw Exception"); //$NON-NLS-1$
0647:                } catch (NullPointerException e) {
0648:                    // expected
0649:                }
0650:
0651:                assertEquals(buf.position(), 0);
0652:
0653:                loadTestData2(array, 0, array.length);
0654:                ByteBuffer ret = buf.put(array, 0, array.length);
0655:                assertEquals(buf.position(), buf.capacity());
0656:                assertContentEquals(buf, array, 0, array.length);
0657:                assertSame(ret, buf);
0658:            }
0659:
0660:            /*
0661:             * Class under test for java.nio.ByteBuffer put(java.nio.ByteBuffer)
0662:             */
0663:            public void testPutByteBuffer() {
0664:                ByteBuffer other = ByteBuffer.allocate(buf.capacity());
0665:                if (buf.isReadOnly()) {
0666:                    try {
0667:                        buf.clear();
0668:                        buf.put(other);
0669:                        fail("Should throw Exception"); //$NON-NLS-1$
0670:                    } catch (ReadOnlyBufferException e) {
0671:                        // expected
0672:                    }
0673:                    try {
0674:                        buf.clear();
0675:                        buf.put((ByteBuffer) null);
0676:                        fail("Should throw Exception"); //$NON-NLS-1$
0677:                    } catch (ReadOnlyBufferException e) {
0678:                        // expected
0679:                    }
0680:                    return;
0681:                }
0682:
0683:                try {
0684:                    buf.put(buf);
0685:                    fail("Should throw Exception"); //$NON-NLS-1$
0686:                } catch (IllegalArgumentException e) {
0687:                    // expected
0688:                }
0689:                try {
0690:                    buf.put(ByteBuffer.allocate(buf.capacity() + 1));
0691:                    fail("Should throw Exception"); //$NON-NLS-1$
0692:                } catch (BufferOverflowException e) {
0693:                    // expected
0694:                }
0695:
0696:                try {
0697:                    buf.put((ByteBuffer) null);
0698:                    fail("Should throw Exception"); //$NON-NLS-1$
0699:                } catch (NullPointerException e) {
0700:                    // expected
0701:                }
0702:                loadTestData2(other);
0703:                other.clear();
0704:                buf.clear();
0705:                ByteBuffer ret = buf.put(other);
0706:                assertEquals(other.position(), other.capacity());
0707:                assertEquals(buf.position(), buf.capacity());
0708:                assertContentEquals(other, buf);
0709:                assertSame(ret, buf);
0710:            }
0711:
0712:            /*
0713:             * Class under test for java.nio.ByteBuffer put(int, byte)
0714:             */
0715:            public void testPutintbyte() {
0716:                if (buf.isReadOnly()) {
0717:                    try {
0718:                        buf.put(0, (byte) 0);
0719:                        fail("Should throw Exception"); //$NON-NLS-1$
0720:                    } catch (ReadOnlyBufferException e) {
0721:                        // expected
0722:                    }
0723:                    return;
0724:                }
0725:
0726:                buf.clear();
0727:                for (int i = 0; i < buf.capacity(); i++) {
0728:                    assertEquals(buf.position(), 0);
0729:                    ByteBuffer ret = buf.put(i, (byte) i);
0730:                    assertEquals(buf.get(i), (byte) i);
0731:                    assertSame(ret, buf);
0732:                }
0733:                try {
0734:                    buf.put(-1, (byte) 0);
0735:                    fail("Should throw Exception"); //$NON-NLS-1$
0736:                } catch (IndexOutOfBoundsException e) {
0737:                    // expected
0738:                }
0739:                try {
0740:                    buf.put(buf.limit(), (byte) 0);
0741:                    fail("Should throw Exception"); //$NON-NLS-1$
0742:                } catch (IndexOutOfBoundsException e) {
0743:                    // expected
0744:                }
0745:            }
0746:
0747:            public void testSlice() {
0748:                assertTrue(buf.capacity() > SMALL_TEST_LENGTH);
0749:                buf.position(1);
0750:                buf.limit(buf.capacity() - 1);
0751:
0752:                ByteBuffer slice = buf.slice();
0753:                assertEquals(buf.isReadOnly(), slice.isReadOnly());
0754:                assertEquals(buf.isDirect(), slice.isDirect());
0755:                assertEquals(buf.order(), slice.order());
0756:                assertEquals(slice.position(), 0);
0757:                assertEquals(slice.limit(), buf.remaining());
0758:                assertEquals(slice.capacity(), buf.remaining());
0759:                try {
0760:                    slice.reset();
0761:                    fail("Should throw Exception"); //$NON-NLS-1$
0762:                } catch (InvalidMarkException e) {
0763:                    // expected
0764:                }
0765:
0766:                // slice share the same content with buf
0767:                if (!slice.isReadOnly()) {
0768:                    loadTestData1(slice);
0769:                    assertContentLikeTestData1(buf, 1, (byte) 0, slice
0770:                            .capacity());
0771:                    buf.put(2, (byte) 100);
0772:                    assertEquals(slice.get(1), 100);
0773:                }
0774:            }
0775:
0776:            public void testToString() {
0777:                String str = buf.toString();
0778:                assertTrue(str.indexOf("Byte") >= 0 || str.indexOf("byte") >= 0);
0779:                assertTrue(str.indexOf("" + buf.position()) >= 0);
0780:                assertTrue(str.indexOf("" + buf.limit()) >= 0);
0781:                assertTrue(str.indexOf("" + buf.capacity()) >= 0);
0782:            }
0783:
0784:            public void testAsCharBuffer() {
0785:                CharBuffer charBuffer;
0786:                byte bytes[] = new byte[2];
0787:                char value;
0788:
0789:                // test BIG_ENDIAN char buffer, read
0790:                buf.clear();
0791:                buf.order(ByteOrder.BIG_ENDIAN);
0792:                charBuffer = buf.asCharBuffer();
0793:                assertSame(ByteOrder.BIG_ENDIAN, charBuffer.order());
0794:                while (charBuffer.remaining() > 0) {
0795:                    buf.get(bytes);
0796:                    value = charBuffer.get();
0797:                    assertEquals(bytes2char(bytes, buf.order()), value);
0798:                }
0799:
0800:                // test LITTLE_ENDIAN char buffer, read
0801:                buf.clear();
0802:                buf.order(ByteOrder.LITTLE_ENDIAN);
0803:                charBuffer = buf.asCharBuffer();
0804:                assertSame(ByteOrder.LITTLE_ENDIAN, charBuffer.order());
0805:                while (charBuffer.remaining() > 0) {
0806:                    buf.get(bytes);
0807:                    value = charBuffer.get();
0808:                    assertEquals(bytes2char(bytes, buf.order()), value);
0809:                }
0810:
0811:                if (!buf.isReadOnly()) {
0812:                    // test BIG_ENDIAN char buffer, write
0813:                    buf.clear();
0814:                    buf.order(ByteOrder.BIG_ENDIAN);
0815:                    charBuffer = buf.asCharBuffer();
0816:                    assertSame(ByteOrder.BIG_ENDIAN, charBuffer.order());
0817:                    while (charBuffer.remaining() > 0) {
0818:                        value = (char) charBuffer.remaining();
0819:                        charBuffer.put(value);
0820:                        buf.get(bytes);
0821:                        assertTrue(Arrays.equals(bytes, char2bytes(value, buf
0822:                                .order())));
0823:                    }
0824:
0825:                    // test LITTLE_ENDIAN char buffer, write
0826:                    buf.clear();
0827:                    buf.order(ByteOrder.LITTLE_ENDIAN);
0828:                    charBuffer = buf.asCharBuffer();
0829:                    assertSame(ByteOrder.LITTLE_ENDIAN, charBuffer.order());
0830:                    while (charBuffer.remaining() > 0) {
0831:                        value = (char) charBuffer.remaining();
0832:                        charBuffer.put(value);
0833:                        buf.get(bytes);
0834:                        assertTrue(Arrays.equals(bytes, char2bytes(value, buf
0835:                                .order())));
0836:                    }
0837:                }
0838:                buf.clear();
0839:                buf.order(ByteOrder.BIG_ENDIAN);
0840:            }
0841:
0842:            public void testAsDoubleBuffer() {
0843:                DoubleBuffer doubleBuffer;
0844:                byte bytes[] = new byte[8];
0845:                double value;
0846:
0847:                // test BIG_ENDIAN double buffer, read
0848:                buf.clear();
0849:                buf.order(ByteOrder.BIG_ENDIAN);
0850:                doubleBuffer = buf.asDoubleBuffer();
0851:                assertSame(ByteOrder.BIG_ENDIAN, doubleBuffer.order());
0852:                while (doubleBuffer.remaining() > 0) {
0853:                    buf.get(bytes);
0854:                    value = doubleBuffer.get();
0855:                    if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double
0856:                            .isNaN(value))) {
0857:                        assertEquals(bytes2double(bytes, buf.order()), value,
0858:                                0.00);
0859:                    }
0860:                }
0861:
0862:                // test LITTLE_ENDIAN double buffer, read
0863:                buf.clear();
0864:                buf.order(ByteOrder.LITTLE_ENDIAN);
0865:                doubleBuffer = buf.asDoubleBuffer();
0866:                assertSame(ByteOrder.LITTLE_ENDIAN, doubleBuffer.order());
0867:                while (doubleBuffer.remaining() > 0) {
0868:                    buf.get(bytes);
0869:                    value = doubleBuffer.get();
0870:                    if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double
0871:                            .isNaN(value))) {
0872:                        assertEquals(bytes2double(bytes, buf.order()), value,
0873:                                0.00);
0874:                    }
0875:                }
0876:
0877:                if (!buf.isReadOnly()) {
0878:                    // test BIG_ENDIAN double buffer, write
0879:                    buf.clear();
0880:                    buf.order(ByteOrder.BIG_ENDIAN);
0881:                    doubleBuffer = buf.asDoubleBuffer();
0882:                    assertSame(ByteOrder.BIG_ENDIAN, doubleBuffer.order());
0883:                    while (doubleBuffer.remaining() > 0) {
0884:                        value = (double) doubleBuffer.remaining();
0885:                        doubleBuffer.put(value);
0886:                        buf.get(bytes);
0887:                        assertTrue(Arrays.equals(bytes, double2bytes(value, buf
0888:                                .order())));
0889:                    }
0890:
0891:                    // test LITTLE_ENDIAN double buffer, write
0892:                    buf.clear();
0893:                    buf.order(ByteOrder.LITTLE_ENDIAN);
0894:                    doubleBuffer = buf.asDoubleBuffer();
0895:                    assertSame(ByteOrder.LITTLE_ENDIAN, doubleBuffer.order());
0896:                    while (doubleBuffer.remaining() > 0) {
0897:                        value = (double) doubleBuffer.remaining();
0898:                        doubleBuffer.put(value);
0899:                        buf.get(bytes);
0900:                        assertTrue(Arrays.equals(bytes, double2bytes(value, buf
0901:                                .order())));
0902:                    }
0903:                }
0904:
0905:                buf.clear();
0906:                buf.order(ByteOrder.BIG_ENDIAN);
0907:            }
0908:
0909:            public void testAsFloatBuffer() {
0910:                FloatBuffer floatBuffer;
0911:                byte bytes[] = new byte[4];
0912:                float value;
0913:
0914:                // test BIG_ENDIAN float buffer, read
0915:                buf.clear();
0916:                buf.order(ByteOrder.BIG_ENDIAN);
0917:                floatBuffer = buf.asFloatBuffer();
0918:                assertSame(ByteOrder.BIG_ENDIAN, floatBuffer.order());
0919:                while (floatBuffer.remaining() > 0) {
0920:                    buf.get(bytes);
0921:                    value = floatBuffer.get();
0922:                    if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float
0923:                            .isNaN(value))) {
0924:                        assertEquals(bytes2float(bytes, buf.order()), value,
0925:                                0.00);
0926:                    }
0927:                }
0928:
0929:                // test LITTLE_ENDIAN float buffer, read
0930:                buf.clear();
0931:                buf.order(ByteOrder.LITTLE_ENDIAN);
0932:                floatBuffer = buf.asFloatBuffer();
0933:                assertSame(ByteOrder.LITTLE_ENDIAN, floatBuffer.order());
0934:                while (floatBuffer.remaining() > 0) {
0935:                    buf.get(bytes);
0936:                    value = floatBuffer.get();
0937:                    if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float
0938:                            .isNaN(value))) {
0939:                        assertEquals(bytes2float(bytes, buf.order()), value,
0940:                                0.00);
0941:                    }
0942:                }
0943:
0944:                if (!buf.isReadOnly()) {
0945:                    // test BIG_ENDIAN float buffer, write
0946:                    buf.clear();
0947:                    buf.order(ByteOrder.BIG_ENDIAN);
0948:                    floatBuffer = buf.asFloatBuffer();
0949:                    assertSame(ByteOrder.BIG_ENDIAN, floatBuffer.order());
0950:                    while (floatBuffer.remaining() > 0) {
0951:                        value = (float) floatBuffer.remaining();
0952:                        floatBuffer.put(value);
0953:                        buf.get(bytes);
0954:                        assertTrue(Arrays.equals(bytes, float2bytes(value, buf
0955:                                .order())));
0956:                    }
0957:
0958:                    // test LITTLE_ENDIAN float buffer, write
0959:                    buf.clear();
0960:                    buf.order(ByteOrder.LITTLE_ENDIAN);
0961:                    floatBuffer = buf.asFloatBuffer();
0962:                    assertSame(ByteOrder.LITTLE_ENDIAN, floatBuffer.order());
0963:                    while (floatBuffer.remaining() > 0) {
0964:                        value = (float) floatBuffer.remaining();
0965:                        floatBuffer.put(value);
0966:                        buf.get(bytes);
0967:                        assertTrue(Arrays.equals(bytes, float2bytes(value, buf
0968:                                .order())));
0969:                    }
0970:                }
0971:
0972:                buf.clear();
0973:                buf.order(ByteOrder.BIG_ENDIAN);
0974:            }
0975:
0976:            public void testAsIntBuffer() {
0977:                IntBuffer intBuffer;
0978:                byte bytes[] = new byte[4];
0979:                int value;
0980:
0981:                // test BIG_ENDIAN int buffer, read
0982:                buf.clear();
0983:                buf.order(ByteOrder.BIG_ENDIAN);
0984:                intBuffer = buf.asIntBuffer();
0985:                assertSame(ByteOrder.BIG_ENDIAN, intBuffer.order());
0986:                while (intBuffer.remaining() > 0) {
0987:                    buf.get(bytes);
0988:                    value = intBuffer.get();
0989:                    assertEquals(bytes2int(bytes, buf.order()), value);
0990:                }
0991:
0992:                // test LITTLE_ENDIAN int buffer, read
0993:                buf.clear();
0994:                buf.order(ByteOrder.LITTLE_ENDIAN);
0995:                intBuffer = buf.asIntBuffer();
0996:                assertSame(ByteOrder.LITTLE_ENDIAN, intBuffer.order());
0997:                while (intBuffer.remaining() > 0) {
0998:                    buf.get(bytes);
0999:                    value = intBuffer.get();
1000:                    assertEquals(bytes2int(bytes, buf.order()), value);
1001:                }
1002:
1003:                if (!buf.isReadOnly()) {
1004:                    // test BIG_ENDIAN int buffer, write
1005:                    buf.clear();
1006:                    buf.order(ByteOrder.BIG_ENDIAN);
1007:                    intBuffer = buf.asIntBuffer();
1008:                    assertSame(ByteOrder.BIG_ENDIAN, intBuffer.order());
1009:                    while (intBuffer.remaining() > 0) {
1010:                        value = (int) intBuffer.remaining();
1011:                        intBuffer.put(value);
1012:                        buf.get(bytes);
1013:                        assertTrue(Arrays.equals(bytes, int2bytes(value, buf
1014:                                .order())));
1015:                    }
1016:
1017:                    // test LITTLE_ENDIAN int buffer, write
1018:                    buf.clear();
1019:                    buf.order(ByteOrder.LITTLE_ENDIAN);
1020:                    intBuffer = buf.asIntBuffer();
1021:                    assertSame(ByteOrder.LITTLE_ENDIAN, intBuffer.order());
1022:                    while (intBuffer.remaining() > 0) {
1023:                        value = (int) intBuffer.remaining();
1024:                        intBuffer.put(value);
1025:                        buf.get(bytes);
1026:                        assertTrue(Arrays.equals(bytes, int2bytes(value, buf
1027:                                .order())));
1028:                    }
1029:                }
1030:
1031:                buf.clear();
1032:                buf.order(ByteOrder.BIG_ENDIAN);
1033:            }
1034:
1035:            public void testAsLongBuffer() {
1036:                LongBuffer longBuffer;
1037:                byte bytes[] = new byte[8];
1038:                long value;
1039:
1040:                // test BIG_ENDIAN long buffer, read
1041:                buf.clear();
1042:                buf.order(ByteOrder.BIG_ENDIAN);
1043:                longBuffer = buf.asLongBuffer();
1044:                assertSame(ByteOrder.BIG_ENDIAN, longBuffer.order());
1045:                while (longBuffer.remaining() > 0) {
1046:                    buf.get(bytes);
1047:                    value = longBuffer.get();
1048:                    assertEquals(bytes2long(bytes, buf.order()), value);
1049:                }
1050:
1051:                // test LITTLE_ENDIAN long buffer, read
1052:                buf.clear();
1053:                buf.order(ByteOrder.LITTLE_ENDIAN);
1054:                longBuffer = buf.asLongBuffer();
1055:                assertSame(ByteOrder.LITTLE_ENDIAN, longBuffer.order());
1056:                while (longBuffer.remaining() > 0) {
1057:                    buf.get(bytes);
1058:                    value = longBuffer.get();
1059:                    assertEquals(bytes2long(bytes, buf.order()), value);
1060:                }
1061:
1062:                if (!buf.isReadOnly()) {
1063:                    // test BIG_ENDIAN long buffer, write
1064:                    buf.clear();
1065:                    buf.order(ByteOrder.BIG_ENDIAN);
1066:                    longBuffer = buf.asLongBuffer();
1067:                    assertSame(ByteOrder.BIG_ENDIAN, longBuffer.order());
1068:                    while (longBuffer.remaining() > 0) {
1069:                        value = (long) longBuffer.remaining();
1070:                        longBuffer.put(value);
1071:                        buf.get(bytes);
1072:                        assertTrue(Arrays.equals(bytes, long2bytes(value, buf
1073:                                .order())));
1074:                    }
1075:
1076:                    // test LITTLE_ENDIAN long buffer, write
1077:                    buf.clear();
1078:                    buf.order(ByteOrder.LITTLE_ENDIAN);
1079:                    longBuffer = buf.asLongBuffer();
1080:                    assertSame(ByteOrder.LITTLE_ENDIAN, longBuffer.order());
1081:                    while (longBuffer.remaining() > 0) {
1082:                        value = (long) longBuffer.remaining();
1083:                        longBuffer.put(value);
1084:                        buf.get(bytes);
1085:                        assertTrue(Arrays.equals(bytes, long2bytes(value, buf
1086:                                .order())));
1087:                    }
1088:                }
1089:
1090:                buf.clear();
1091:                buf.order(ByteOrder.BIG_ENDIAN);
1092:            }
1093:
1094:            public void testAsShortBuffer() {
1095:                ShortBuffer shortBuffer;
1096:                byte bytes[] = new byte[2];
1097:                short value;
1098:
1099:                // test BIG_ENDIAN short buffer, read
1100:                buf.clear();
1101:                buf.order(ByteOrder.BIG_ENDIAN);
1102:                shortBuffer = buf.asShortBuffer();
1103:                assertSame(ByteOrder.BIG_ENDIAN, shortBuffer.order());
1104:                while (shortBuffer.remaining() > 0) {
1105:                    buf.get(bytes);
1106:                    value = shortBuffer.get();
1107:                    assertEquals(bytes2short(bytes, buf.order()), value);
1108:                }
1109:
1110:                // test LITTLE_ENDIAN short buffer, read
1111:                buf.clear();
1112:                buf.order(ByteOrder.LITTLE_ENDIAN);
1113:                shortBuffer = buf.asShortBuffer();
1114:                assertSame(ByteOrder.LITTLE_ENDIAN, shortBuffer.order());
1115:                while (shortBuffer.remaining() > 0) {
1116:                    buf.get(bytes);
1117:                    value = shortBuffer.get();
1118:                    assertEquals(bytes2short(bytes, buf.order()), value);
1119:                }
1120:
1121:                if (!buf.isReadOnly()) {
1122:                    // test BIG_ENDIAN short buffer, write
1123:                    buf.clear();
1124:                    buf.order(ByteOrder.BIG_ENDIAN);
1125:                    shortBuffer = buf.asShortBuffer();
1126:                    assertSame(ByteOrder.BIG_ENDIAN, shortBuffer.order());
1127:                    while (shortBuffer.remaining() > 0) {
1128:                        value = (short) shortBuffer.remaining();
1129:                        shortBuffer.put(value);
1130:                        buf.get(bytes);
1131:                        assertTrue(Arrays.equals(bytes, short2bytes(value, buf
1132:                                .order())));
1133:                    }
1134:
1135:                    // test LITTLE_ENDIAN short buffer, write
1136:                    buf.clear();
1137:                    buf.order(ByteOrder.LITTLE_ENDIAN);
1138:                    shortBuffer = buf.asShortBuffer();
1139:                    assertSame(ByteOrder.LITTLE_ENDIAN, shortBuffer.order());
1140:                    while (shortBuffer.remaining() > 0) {
1141:                        value = (short) shortBuffer.remaining();
1142:                        shortBuffer.put(value);
1143:                        buf.get(bytes);
1144:                        assertTrue(Arrays.equals(bytes, short2bytes(value, buf
1145:                                .order())));
1146:                    }
1147:                }
1148:
1149:                buf.clear();
1150:                buf.order(ByteOrder.BIG_ENDIAN);
1151:            }
1152:
1153:            public void testGetChar() {
1154:                int nbytes = 2;
1155:                byte bytes[] = new byte[nbytes];
1156:                char value;
1157:                buf.clear();
1158:                for (int i = 0; buf.remaining() >= nbytes; i++) {
1159:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1160:                            : ByteOrder.LITTLE_ENDIAN);
1161:                    assertEquals(i * nbytes, buf.position());
1162:                    buf.mark();
1163:                    buf.get(bytes);
1164:                    buf.reset();
1165:                    value = buf.getChar();
1166:                    assertEquals(bytes2char(bytes, buf.order()), value);
1167:                }
1168:
1169:                try {
1170:                    buf.getChar();
1171:                    fail("Should throw Exception"); //$NON-NLS-1$
1172:                } catch (BufferUnderflowException e) {
1173:                    // expected
1174:                }
1175:
1176:                buf.order(ByteOrder.BIG_ENDIAN);
1177:            }
1178:
1179:            public void testGetCharint() {
1180:                int nbytes = 2;
1181:                byte bytes[] = new byte[nbytes];
1182:                char value;
1183:                buf.clear();
1184:                for (int i = 0; i <= buf.limit() - nbytes; i++) {
1185:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1186:                            : ByteOrder.LITTLE_ENDIAN);
1187:                    buf.position(i);
1188:                    value = buf.getChar(i);
1189:                    assertEquals(i, buf.position());
1190:                    buf.get(bytes);
1191:                    assertEquals(bytes2char(bytes, buf.order()), value);
1192:                }
1193:
1194:                try {
1195:                    buf.getChar(-1);
1196:                    fail("Should throw Exception"); //$NON-NLS-1$
1197:                } catch (IndexOutOfBoundsException e) {
1198:                    // expected
1199:                }
1200:                try {
1201:                    buf.getChar(buf.limit() - nbytes + 1);
1202:                    fail("Should throw Exception"); //$NON-NLS-1$
1203:                } catch (IndexOutOfBoundsException e) {
1204:                    // expected
1205:                }
1206:
1207:                buf.order(ByteOrder.BIG_ENDIAN);
1208:            }
1209:
1210:            public void testPutChar() {
1211:                if (buf.isReadOnly()) {
1212:                    try {
1213:                        buf.clear();
1214:                        buf.putChar((char) 1);
1215:                        fail("Should throw Exception"); //$NON-NLS-1$
1216:                    } catch (ReadOnlyBufferException e) {
1217:                        // expected
1218:                    }
1219:                    return;
1220:                }
1221:
1222:                int nbytes = 2;
1223:                byte bytes[] = new byte[nbytes];
1224:                char value = 0;
1225:                buf.clear();
1226:                for (int i = 0; buf.remaining() >= nbytes; i++) {
1227:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1228:                            : ByteOrder.LITTLE_ENDIAN);
1229:                    value = (char) i;
1230:                    buf.mark();
1231:                    buf.putChar(value);
1232:                    assertEquals((i + 1) * nbytes, buf.position());
1233:                    buf.reset();
1234:                    buf.get(bytes);
1235:                    assertTrue(Arrays.equals(char2bytes(value, buf.order()),
1236:                            bytes));
1237:                }
1238:
1239:                try {
1240:                    buf.putChar(value);
1241:                    fail("Should throw Exception"); //$NON-NLS-1$
1242:                } catch (BufferOverflowException e) {
1243:                    // expected
1244:                }
1245:
1246:                buf.order(ByteOrder.BIG_ENDIAN);
1247:            }
1248:
1249:            public void testPutCharint() {
1250:                if (buf.isReadOnly()) {
1251:                    try {
1252:                        buf.putChar(0, (char) 1);
1253:                        fail("Should throw Exception"); //$NON-NLS-1$
1254:                    } catch (ReadOnlyBufferException e) {
1255:                        // expected
1256:                    }
1257:                    return;
1258:                }
1259:
1260:                int nbytes = 2;
1261:                byte bytes[] = new byte[nbytes];
1262:                char value = 0;
1263:                buf.clear();
1264:                for (int i = 0; i <= buf.limit() - nbytes; i++) {
1265:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1266:                            : ByteOrder.LITTLE_ENDIAN);
1267:                    value = (char) i;
1268:                    buf.position(i);
1269:                    buf.putChar(i, value);
1270:                    assertEquals(i, buf.position());
1271:                    buf.get(bytes);
1272:                    assertTrue(Arrays.equals(char2bytes(value, buf.order()),
1273:                            bytes));
1274:                }
1275:
1276:                try {
1277:                    buf.putChar(-1, value);
1278:                    fail("Should throw Exception"); //$NON-NLS-1$
1279:                } catch (IndexOutOfBoundsException e) {
1280:                    // expected
1281:                }
1282:                try {
1283:                    buf.putChar(buf.limit() - nbytes + 1, value);
1284:                    fail("Should throw Exception"); //$NON-NLS-1$
1285:                } catch (IndexOutOfBoundsException e) {
1286:                    // expected
1287:                }
1288:
1289:                buf.order(ByteOrder.BIG_ENDIAN);
1290:
1291:                try {
1292:                    ByteBuffer.allocateDirect(16).putChar(Integer.MAX_VALUE,
1293:                            'h');
1294:                } catch (IndexOutOfBoundsException e) {
1295:                    //expected 
1296:                }
1297:            }
1298:
1299:            public void testGetDouble() {
1300:                int nbytes = 8;
1301:                byte bytes[] = new byte[nbytes];
1302:                double value;
1303:                buf.clear();
1304:                for (int i = 0; buf.remaining() >= nbytes; i++) {
1305:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1306:                            : ByteOrder.LITTLE_ENDIAN);
1307:                    assertEquals(i * nbytes, buf.position());
1308:                    buf.mark();
1309:                    buf.get(bytes);
1310:                    buf.reset();
1311:                    value = buf.getDouble();
1312:                    if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double
1313:                            .isNaN(value))) {
1314:                        assertEquals(bytes2double(bytes, buf.order()), value,
1315:                                0.00);
1316:                    }
1317:                }
1318:
1319:                try {
1320:                    buf.getDouble();
1321:                    fail("Should throw Exception"); //$NON-NLS-1$
1322:                } catch (BufferUnderflowException e) {
1323:                    // expected
1324:                }
1325:
1326:                buf.order(ByteOrder.BIG_ENDIAN);
1327:            }
1328:
1329:            public void testGetDoubleint() {
1330:                int nbytes = 8;
1331:                byte bytes[] = new byte[nbytes];
1332:                double value;
1333:                buf.clear();
1334:                for (int i = 0; i <= buf.limit() - nbytes; i++) {
1335:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1336:                            : ByteOrder.LITTLE_ENDIAN);
1337:                    buf.position(i);
1338:                    value = buf.getDouble(i);
1339:                    assertEquals(i, buf.position());
1340:                    buf.get(bytes);
1341:                    if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double
1342:                            .isNaN(value))) {
1343:                        assertEquals(bytes2double(bytes, buf.order()), value,
1344:                                0.00);
1345:                    }
1346:                }
1347:
1348:                try {
1349:                    buf.getDouble(-1);
1350:                    fail("Should throw Exception"); //$NON-NLS-1$
1351:                } catch (IndexOutOfBoundsException e) {
1352:                    // expected
1353:                }
1354:                try {
1355:                    buf.getDouble(buf.limit() - nbytes + 1);
1356:                    fail("Should throw Exception"); //$NON-NLS-1$
1357:                } catch (IndexOutOfBoundsException e) {
1358:                    // expected
1359:                }
1360:
1361:                buf.order(ByteOrder.BIG_ENDIAN);
1362:
1363:                try {
1364:                    ByteBuffer.allocateDirect(16).getDouble(Integer.MAX_VALUE);
1365:                } catch (IndexOutOfBoundsException e) {
1366:                    //expected 
1367:                }
1368:            }
1369:
1370:            public void testPutDouble() {
1371:                if (buf.isReadOnly()) {
1372:                    try {
1373:                        buf.clear();
1374:                        buf.putDouble((double) 1);
1375:                        fail("Should throw Exception"); //$NON-NLS-1$
1376:                    } catch (ReadOnlyBufferException e) {
1377:                        // expected
1378:                    }
1379:                    return;
1380:                }
1381:
1382:                int nbytes = 8;
1383:                byte bytes[] = new byte[nbytes];
1384:                double value = 0;
1385:                buf.clear();
1386:                for (int i = 0; buf.remaining() >= nbytes; i++) {
1387:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1388:                            : ByteOrder.LITTLE_ENDIAN);
1389:                    value = (double) i;
1390:                    buf.mark();
1391:                    buf.putDouble(value);
1392:                    assertEquals((i + 1) * nbytes, buf.position());
1393:                    buf.reset();
1394:                    buf.get(bytes);
1395:                    assertTrue(Arrays.equals(double2bytes(value, buf.order()),
1396:                            bytes));
1397:                }
1398:
1399:                try {
1400:                    buf.putDouble(value);
1401:                    fail("Should throw Exception"); //$NON-NLS-1$
1402:                } catch (BufferOverflowException e) {
1403:                    // expected
1404:                }
1405:
1406:                buf.order(ByteOrder.BIG_ENDIAN);
1407:            }
1408:
1409:            public void testPutDoubleint() {
1410:                if (buf.isReadOnly()) {
1411:                    try {
1412:                        buf.putDouble(0, (double) 1);
1413:                        fail("Should throw Exception"); //$NON-NLS-1$
1414:                    } catch (ReadOnlyBufferException e) {
1415:                        // expected
1416:                    }
1417:                    return;
1418:                }
1419:
1420:                int nbytes = 8;
1421:                byte bytes[] = new byte[nbytes];
1422:                double value = 0;
1423:                buf.clear();
1424:                for (int i = 0; i <= buf.limit() - nbytes; i++) {
1425:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1426:                            : ByteOrder.LITTLE_ENDIAN);
1427:                    value = (double) i;
1428:                    buf.position(i);
1429:                    buf.putDouble(i, value);
1430:                    assertEquals(i, buf.position());
1431:                    buf.get(bytes);
1432:                    assertTrue(Arrays.equals(double2bytes(value, buf.order()),
1433:                            bytes));
1434:                }
1435:
1436:                try {
1437:                    buf.putDouble(-1, value);
1438:                    fail("Should throw Exception"); //$NON-NLS-1$
1439:                } catch (IndexOutOfBoundsException e) {
1440:                    // expected
1441:                }
1442:                try {
1443:                    buf.putDouble(buf.limit() - nbytes + 1, value);
1444:                    fail("Should throw Exception"); //$NON-NLS-1$
1445:                } catch (IndexOutOfBoundsException e) {
1446:                    // expected
1447:                }
1448:
1449:                buf.order(ByteOrder.BIG_ENDIAN);
1450:            }
1451:
1452:            public void testGetFloat() {
1453:                int nbytes = 4;
1454:                byte bytes[] = new byte[nbytes];
1455:                float value;
1456:                buf.clear();
1457:                for (int i = 0; buf.remaining() >= nbytes; i++) {
1458:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1459:                            : ByteOrder.LITTLE_ENDIAN);
1460:                    assertEquals(i * nbytes, buf.position());
1461:                    buf.mark();
1462:                    buf.get(bytes);
1463:                    buf.reset();
1464:                    value = buf.getFloat();
1465:                    if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float
1466:                            .isNaN(value))) {
1467:                        assertEquals(bytes2float(bytes, buf.order()), value,
1468:                                0.00);
1469:                    }
1470:                }
1471:
1472:                try {
1473:                    buf.getFloat();
1474:                    fail("Should throw Exception"); //$NON-NLS-1$
1475:                } catch (BufferUnderflowException e) {
1476:                    // expected
1477:                }
1478:
1479:                buf.order(ByteOrder.BIG_ENDIAN);
1480:            }
1481:
1482:            public void testGetFloatint() {
1483:                int nbytes = 4;
1484:                byte bytes[] = new byte[nbytes];
1485:                float value;
1486:                buf.clear();
1487:                for (int i = 0; i <= buf.limit() - nbytes; i++) {
1488:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1489:                            : ByteOrder.LITTLE_ENDIAN);
1490:                    buf.position(i);
1491:                    value = buf.getFloat(i);
1492:                    assertEquals(i, buf.position());
1493:                    buf.get(bytes);
1494:                    if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float
1495:                            .isNaN(value))) {
1496:                        assertEquals(bytes2float(bytes, buf.order()), value,
1497:                                0.00);
1498:                    }
1499:                }
1500:
1501:                try {
1502:                    buf.getFloat(-1);
1503:                    fail("Should throw Exception"); //$NON-NLS-1$
1504:                } catch (IndexOutOfBoundsException e) {
1505:                    // expected
1506:                }
1507:                try {
1508:                    buf.getFloat(buf.limit() - nbytes + 1);
1509:                    fail("Should throw Exception"); //$NON-NLS-1$
1510:                } catch (IndexOutOfBoundsException e) {
1511:                    // expected
1512:                }
1513:
1514:                buf.order(ByteOrder.BIG_ENDIAN);
1515:            }
1516:
1517:            public void testPutFloat() {
1518:                if (buf.isReadOnly()) {
1519:                    try {
1520:                        buf.clear();
1521:                        buf.putFloat((float) 1);
1522:                        fail("Should throw Exception"); //$NON-NLS-1$
1523:                    } catch (ReadOnlyBufferException e) {
1524:                        // expected
1525:                    }
1526:                    return;
1527:                }
1528:
1529:                int nbytes = 4;
1530:                byte bytes[] = new byte[nbytes];
1531:                float value = 0;
1532:                buf.clear();
1533:                for (int i = 0; buf.remaining() >= nbytes; i++) {
1534:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1535:                            : ByteOrder.LITTLE_ENDIAN);
1536:                    value = (float) i;
1537:                    buf.mark();
1538:                    buf.putFloat(value);
1539:                    assertEquals((i + 1) * nbytes, buf.position());
1540:                    buf.reset();
1541:                    buf.get(bytes);
1542:                    assertTrue(Arrays.equals(float2bytes(value, buf.order()),
1543:                            bytes));
1544:                }
1545:
1546:                try {
1547:                    buf.putFloat(value);
1548:                    fail("Should throw Exception"); //$NON-NLS-1$
1549:                } catch (BufferOverflowException e) {
1550:                    // expected
1551:                }
1552:
1553:                buf.order(ByteOrder.BIG_ENDIAN);
1554:            }
1555:
1556:            public void testPutFloatint() {
1557:                if (buf.isReadOnly()) {
1558:                    try {
1559:                        buf.putFloat(0, (float) 1);
1560:                        fail("Should throw Exception"); //$NON-NLS-1$
1561:                    } catch (ReadOnlyBufferException e) {
1562:                        // expected
1563:                    }
1564:                    return;
1565:                }
1566:
1567:                int nbytes = 4;
1568:                byte bytes[] = new byte[nbytes];
1569:                float value = 0;
1570:                buf.clear();
1571:                for (int i = 0; i <= buf.limit() - nbytes; i++) {
1572:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1573:                            : ByteOrder.LITTLE_ENDIAN);
1574:                    value = (float) i;
1575:                    buf.position(i);
1576:                    buf.putFloat(i, value);
1577:                    assertEquals(i, buf.position());
1578:                    buf.get(bytes);
1579:                    assertTrue(Arrays.equals(float2bytes(value, buf.order()),
1580:                            bytes));
1581:                }
1582:
1583:                try {
1584:                    buf.putFloat(-1, value);
1585:                    fail("Should throw Exception"); //$NON-NLS-1$
1586:                } catch (IndexOutOfBoundsException e) {
1587:                    // expected
1588:                }
1589:                try {
1590:                    buf.putFloat(buf.limit() - nbytes + 1, value);
1591:                    fail("Should throw Exception"); //$NON-NLS-1$
1592:                } catch (IndexOutOfBoundsException e) {
1593:                    // expected
1594:                }
1595:
1596:                buf.order(ByteOrder.BIG_ENDIAN);
1597:            }
1598:
1599:            public void testGetInt() {
1600:                int nbytes = 4;
1601:                byte bytes[] = new byte[nbytes];
1602:                int value;
1603:                buf.clear();
1604:                for (int i = 0; buf.remaining() >= nbytes; i++) {
1605:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1606:                            : ByteOrder.LITTLE_ENDIAN);
1607:                    assertEquals(i * nbytes, buf.position());
1608:                    buf.mark();
1609:                    buf.get(bytes);
1610:                    buf.reset();
1611:                    value = buf.getInt();
1612:                    assertEquals(bytes2int(bytes, buf.order()), value);
1613:                }
1614:
1615:                try {
1616:                    buf.getInt();
1617:                    fail("Should throw Exception"); //$NON-NLS-1$
1618:                } catch (BufferUnderflowException e) {
1619:                    // expected
1620:                }
1621:
1622:                buf.order(ByteOrder.BIG_ENDIAN);
1623:            }
1624:
1625:            public void testGetIntint() {
1626:                int nbytes = 4;
1627:                byte bytes[] = new byte[nbytes];
1628:                int value;
1629:                buf.clear();
1630:                for (int i = 0; i <= buf.limit() - nbytes; i++) {
1631:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1632:                            : ByteOrder.LITTLE_ENDIAN);
1633:                    buf.position(i);
1634:                    value = buf.getInt(i);
1635:                    assertEquals(i, buf.position());
1636:                    buf.get(bytes);
1637:                    assertEquals(bytes2int(bytes, buf.order()), value);
1638:                }
1639:
1640:                try {
1641:                    buf.getInt(-1);
1642:                    fail("Should throw Exception"); //$NON-NLS-1$
1643:                } catch (IndexOutOfBoundsException e) {
1644:                    // expected
1645:                }
1646:                try {
1647:                    buf.getInt(buf.limit() - nbytes + 1);
1648:                    fail("Should throw Exception"); //$NON-NLS-1$
1649:                } catch (IndexOutOfBoundsException e) {
1650:                    // expected
1651:                }
1652:
1653:                buf.order(ByteOrder.BIG_ENDIAN);
1654:                try {
1655:                    ByteBuffer.allocateDirect(16).getInt(Integer.MAX_VALUE);
1656:                } catch (IndexOutOfBoundsException e) {
1657:                    //expected 
1658:                }
1659:            }
1660:
1661:            public void testPutInt() {
1662:                if (buf.isReadOnly()) {
1663:                    try {
1664:                        buf.clear();
1665:                        buf.putInt((int) 1);
1666:                        fail("Should throw Exception"); //$NON-NLS-1$
1667:                    } catch (ReadOnlyBufferException e) {
1668:                        // expected
1669:                    }
1670:                    return;
1671:                }
1672:
1673:                int nbytes = 4;
1674:                byte bytes[] = new byte[nbytes];
1675:                int value = 0;
1676:                buf.clear();
1677:                for (int i = 0; buf.remaining() >= nbytes; i++) {
1678:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1679:                            : ByteOrder.LITTLE_ENDIAN);
1680:                    value = (int) i;
1681:                    buf.mark();
1682:                    buf.putInt(value);
1683:                    assertEquals((i + 1) * nbytes, buf.position());
1684:                    buf.reset();
1685:                    buf.get(bytes);
1686:                    assertTrue(Arrays.equals(int2bytes(value, buf.order()),
1687:                            bytes));
1688:                }
1689:
1690:                try {
1691:                    buf.putInt(value);
1692:                    fail("Should throw Exception"); //$NON-NLS-1$
1693:                } catch (BufferOverflowException e) {
1694:                    // expected
1695:                }
1696:
1697:                buf.order(ByteOrder.BIG_ENDIAN);
1698:            }
1699:
1700:            public void testPutIntint() {
1701:                if (buf.isReadOnly()) {
1702:                    try {
1703:                        buf.putInt(0, (int) 1);
1704:                        fail("Should throw Exception"); //$NON-NLS-1$
1705:                    } catch (ReadOnlyBufferException e) {
1706:                        // expected
1707:                    }
1708:                    return;
1709:                }
1710:
1711:                int nbytes = 4;
1712:                byte bytes[] = new byte[nbytes];
1713:                int value = 0;
1714:                buf.clear();
1715:                for (int i = 0; i <= buf.limit() - nbytes; i++) {
1716:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1717:                            : ByteOrder.LITTLE_ENDIAN);
1718:                    value = (int) i;
1719:                    buf.position(i);
1720:                    buf.putInt(i, value);
1721:                    assertEquals(i, buf.position());
1722:                    buf.get(bytes);
1723:                    assertTrue(Arrays.equals(int2bytes(value, buf.order()),
1724:                            bytes));
1725:                }
1726:
1727:                try {
1728:                    buf.putInt(-1, value);
1729:                    fail("Should throw Exception"); //$NON-NLS-1$
1730:                } catch (IndexOutOfBoundsException e) {
1731:                    // expected
1732:                }
1733:                try {
1734:                    buf.putInt(buf.limit() - nbytes + 1, value);
1735:                    fail("Should throw Exception"); //$NON-NLS-1$
1736:                } catch (IndexOutOfBoundsException e) {
1737:                    // expected
1738:                }
1739:
1740:                buf.order(ByteOrder.BIG_ENDIAN);
1741:            }
1742:
1743:            public void testGetLong() {
1744:                int nbytes = 8;
1745:                byte bytes[] = new byte[nbytes];
1746:                long value;
1747:                buf.clear();
1748:                for (int i = 0; buf.remaining() >= nbytes; i++) {
1749:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1750:                            : ByteOrder.LITTLE_ENDIAN);
1751:                    assertEquals(i * nbytes, buf.position());
1752:                    buf.mark();
1753:                    buf.get(bytes);
1754:                    buf.reset();
1755:                    value = buf.getLong();
1756:                    assertEquals(bytes2long(bytes, buf.order()), value);
1757:                }
1758:
1759:                try {
1760:                    buf.getLong();
1761:                    fail("Should throw Exception"); //$NON-NLS-1$
1762:                } catch (BufferUnderflowException e) {
1763:                    // expected
1764:                }
1765:
1766:                buf.order(ByteOrder.BIG_ENDIAN);
1767:            }
1768:
1769:            public void testGetLongint() {
1770:                int nbytes = 8;
1771:                byte bytes[] = new byte[nbytes];
1772:                long value;
1773:                buf.clear();
1774:                for (int i = 0; i <= buf.limit() - nbytes; i++) {
1775:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1776:                            : ByteOrder.LITTLE_ENDIAN);
1777:                    buf.position(i);
1778:                    value = buf.getLong(i);
1779:                    assertEquals(i, buf.position());
1780:                    buf.get(bytes);
1781:                    assertEquals(bytes2long(bytes, buf.order()), value);
1782:                }
1783:
1784:                try {
1785:                    buf.getLong(-1);
1786:                    fail("Should throw Exception"); //$NON-NLS-1$
1787:                } catch (IndexOutOfBoundsException e) {
1788:                    // expected
1789:                }
1790:                try {
1791:                    buf.getLong(buf.limit() - nbytes + 1);
1792:                    fail("Should throw Exception"); //$NON-NLS-1$
1793:                } catch (IndexOutOfBoundsException e) {
1794:                    // expected
1795:                }
1796:
1797:                buf.order(ByteOrder.BIG_ENDIAN);
1798:            }
1799:
1800:            public void testPutLong() {
1801:                if (buf.isReadOnly()) {
1802:                    try {
1803:                        buf.clear();
1804:                        buf.putLong((long) 1);
1805:                        fail("Should throw Exception"); //$NON-NLS-1$
1806:                    } catch (ReadOnlyBufferException e) {
1807:                        // expected
1808:                    }
1809:                    return;
1810:                }
1811:
1812:                int nbytes = 8;
1813:                byte bytes[] = new byte[nbytes];
1814:                long value = 0;
1815:                buf.clear();
1816:                for (int i = 0; buf.remaining() >= nbytes; i++) {
1817:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1818:                            : ByteOrder.LITTLE_ENDIAN);
1819:                    value = (long) i;
1820:                    buf.mark();
1821:                    buf.putLong(value);
1822:                    assertEquals((i + 1) * nbytes, buf.position());
1823:                    buf.reset();
1824:                    buf.get(bytes);
1825:                    assertTrue(Arrays.equals(long2bytes(value, buf.order()),
1826:                            bytes));
1827:                }
1828:
1829:                try {
1830:                    buf.putLong(value);
1831:                    fail("Should throw Exception"); //$NON-NLS-1$
1832:                } catch (BufferOverflowException e) {
1833:                    // expected
1834:                }
1835:
1836:                buf.order(ByteOrder.BIG_ENDIAN);
1837:            }
1838:
1839:            public void testPutLongint() {
1840:                if (buf.isReadOnly()) {
1841:                    try {
1842:                        buf.putLong(0, (long) 1);
1843:                        fail("Should throw Exception"); //$NON-NLS-1$
1844:                    } catch (ReadOnlyBufferException e) {
1845:                        // expected
1846:                    }
1847:                    return;
1848:                }
1849:
1850:                int nbytes = 8;
1851:                byte bytes[] = new byte[nbytes];
1852:                long value = 0;
1853:                buf.clear();
1854:                for (int i = 0; i <= buf.limit() - nbytes; i++) {
1855:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1856:                            : ByteOrder.LITTLE_ENDIAN);
1857:                    value = (long) i;
1858:                    buf.position(i);
1859:                    buf.putLong(i, value);
1860:                    assertEquals(i, buf.position());
1861:                    buf.get(bytes);
1862:                    assertTrue(Arrays.equals(long2bytes(value, buf.order()),
1863:                            bytes));
1864:                }
1865:
1866:                try {
1867:                    buf.putLong(-1, value);
1868:                    fail("Should throw Exception"); //$NON-NLS-1$
1869:                } catch (IndexOutOfBoundsException e) {
1870:                    // expected
1871:                }
1872:                try {
1873:                    buf.putLong(buf.limit() - nbytes + 1, value);
1874:                    fail("Should throw Exception"); //$NON-NLS-1$
1875:                } catch (IndexOutOfBoundsException e) {
1876:                    // expected
1877:                }
1878:
1879:                buf.order(ByteOrder.BIG_ENDIAN);
1880:            }
1881:
1882:            public void testGetShort() {
1883:                int nbytes = 2;
1884:                byte bytes[] = new byte[nbytes];
1885:                short value;
1886:                buf.clear();
1887:                for (int i = 0; buf.remaining() >= nbytes; i++) {
1888:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1889:                            : ByteOrder.LITTLE_ENDIAN);
1890:                    assertEquals(i * nbytes, buf.position());
1891:                    buf.mark();
1892:                    buf.get(bytes);
1893:                    buf.reset();
1894:                    value = buf.getShort();
1895:                    assertEquals(bytes2short(bytes, buf.order()), value);
1896:                }
1897:
1898:                try {
1899:                    buf.getShort();
1900:                    fail("Should throw Exception"); //$NON-NLS-1$
1901:                } catch (BufferUnderflowException e) {
1902:                    // expected
1903:                }
1904:
1905:                buf.order(ByteOrder.BIG_ENDIAN);
1906:            }
1907:
1908:            public void testGetShortint() {
1909:                int nbytes = 2;
1910:                byte bytes[] = new byte[nbytes];
1911:                short value;
1912:                buf.clear();
1913:                for (int i = 0; i <= buf.limit() - nbytes; i++) {
1914:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1915:                            : ByteOrder.LITTLE_ENDIAN);
1916:                    buf.position(i);
1917:                    value = buf.getShort(i);
1918:                    assertEquals(i, buf.position());
1919:                    buf.get(bytes);
1920:                    assertEquals(bytes2short(bytes, buf.order()), value);
1921:                }
1922:
1923:                try {
1924:                    buf.getShort(-1);
1925:                    fail("Should throw Exception"); //$NON-NLS-1$
1926:                } catch (IndexOutOfBoundsException e) {
1927:                    // expected
1928:                }
1929:                try {
1930:                    buf.getShort(buf.limit() - nbytes + 1);
1931:                    fail("Should throw Exception"); //$NON-NLS-1$
1932:                } catch (IndexOutOfBoundsException e) {
1933:                    // expected
1934:                }
1935:
1936:                buf.order(ByteOrder.BIG_ENDIAN);
1937:            }
1938:
1939:            public void testPutShort() {
1940:                if (buf.isReadOnly()) {
1941:                    try {
1942:                        buf.clear();
1943:                        buf.putShort((short) 1);
1944:                        fail("Should throw Exception"); //$NON-NLS-1$
1945:                    } catch (ReadOnlyBufferException e) {
1946:                        // expected
1947:                    }
1948:                    return;
1949:                }
1950:
1951:                int nbytes = 2;
1952:                byte bytes[] = new byte[nbytes];
1953:                short value = 0;
1954:                buf.clear();
1955:                for (int i = 0; buf.remaining() >= nbytes; i++) {
1956:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1957:                            : ByteOrder.LITTLE_ENDIAN);
1958:                    value = (short) i;
1959:                    buf.mark();
1960:                    buf.putShort(value);
1961:                    assertEquals((i + 1) * nbytes, buf.position());
1962:                    buf.reset();
1963:                    buf.get(bytes);
1964:                    assertTrue(Arrays.equals(short2bytes(value, buf.order()),
1965:                            bytes));
1966:                }
1967:
1968:                try {
1969:                    buf.putShort(value);
1970:                    fail("Should throw Exception"); //$NON-NLS-1$
1971:                } catch (BufferOverflowException e) {
1972:                    // expected
1973:                }
1974:
1975:                buf.order(ByteOrder.BIG_ENDIAN);
1976:            }
1977:
1978:            public void testPutShortint() {
1979:                if (buf.isReadOnly()) {
1980:                    try {
1981:                        buf.putShort(0, (short) 1);
1982:                        fail("Should throw Exception"); //$NON-NLS-1$
1983:                    } catch (ReadOnlyBufferException e) {
1984:                        // expected
1985:                    }
1986:                    return;
1987:                }
1988:
1989:                int nbytes = 2;
1990:                byte bytes[] = new byte[nbytes];
1991:                short value = 0;
1992:                buf.clear();
1993:                for (int i = 0; i <= buf.limit() - nbytes; i++) {
1994:                    buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
1995:                            : ByteOrder.LITTLE_ENDIAN);
1996:                    value = (short) i;
1997:                    buf.position(i);
1998:                    buf.putShort(i, value);
1999:                    assertEquals(i, buf.position());
2000:                    buf.get(bytes);
2001:                    assertTrue(Arrays.equals(short2bytes(value, buf.order()),
2002:                            bytes));
2003:                }
2004:
2005:                try {
2006:                    buf.putShort(-1, value);
2007:                    fail("Should throw Exception"); //$NON-NLS-1$
2008:                } catch (IndexOutOfBoundsException e) {
2009:                    // expected
2010:                }
2011:                try {
2012:                    buf.putShort(buf.limit() - nbytes + 1, value);
2013:                    fail("Should throw Exception"); //$NON-NLS-1$
2014:                } catch (IndexOutOfBoundsException e) {
2015:                    // expected
2016:                }
2017:
2018:                buf.order(ByteOrder.BIG_ENDIAN);
2019:            }
2020:
2021:            /**
2022:             * @tests java.nio.ByteBuffer.wrap(byte[],int,int)
2023:             */
2024:            public void testWrappedByteBuffer_null_array() {
2025:                // Regression for HARMONY-264
2026:                byte array[] = null;
2027:                try {
2028:                    ByteBuffer.wrap(array, -1, 0);
2029:                    fail("Should throw NPE"); //$NON-NLS-1$
2030:                } catch (NullPointerException e) {
2031:                }
2032:                try {
2033:                    ByteBuffer.wrap(new byte[10], Integer.MAX_VALUE, 2);
2034:                    fail("Should throw IndexOutOfBoundsException"); //$NON-NLS-1$
2035:                } catch (IndexOutOfBoundsException e) {
2036:                }
2037:            }
2038:
2039:            private void loadTestData1(byte array[], int offset, int length) {
2040:                for (int i = 0; i < length; i++) {
2041:                    array[offset + i] = (byte) i;
2042:                }
2043:            }
2044:
2045:            private void loadTestData2(byte array[], int offset, int length) {
2046:                for (int i = 0; i < length; i++) {
2047:                    array[offset + i] = (byte) (length - i);
2048:                }
2049:            }
2050:
2051:            private void loadTestData1(ByteBuffer buf) {
2052:                buf.clear();
2053:                for (int i = 0; i < buf.capacity(); i++) {
2054:                    buf.put(i, (byte) i);
2055:                }
2056:            }
2057:
2058:            private void loadTestData2(ByteBuffer buf) {
2059:                buf.clear();
2060:                for (int i = 0; i < buf.capacity(); i++) {
2061:                    buf.put(i, (byte) (buf.capacity() - i));
2062:                }
2063:            }
2064:
2065:            private void assertContentEquals(ByteBuffer buf, byte array[],
2066:                    int offset, int length) {
2067:                for (int i = 0; i < length; i++) {
2068:                    assertEquals(buf.get(i), array[offset + i]);
2069:                }
2070:            }
2071:
2072:            private void assertContentEquals(ByteBuffer buf, ByteBuffer other) {
2073:                assertEquals(buf.capacity(), other.capacity());
2074:                for (int i = 0; i < buf.capacity(); i++) {
2075:                    assertEquals(buf.get(i), other.get(i));
2076:                }
2077:            }
2078:
2079:            private void assertContentLikeTestData1(ByteBuffer buf,
2080:                    int startIndex, byte startValue, int length) {
2081:                byte value = startValue;
2082:                for (int i = 0; i < length; i++) {
2083:                    assertEquals(buf.get(startIndex + i), value);
2084:                    value = (byte) (value + 1);
2085:                }
2086:            }
2087:
2088:            private int bytes2int(byte bytes[], ByteOrder order) {
2089:                int nbytes = 4, bigHead, step;
2090:                if (order == ByteOrder.BIG_ENDIAN) {
2091:                    bigHead = 0;
2092:                    step = 1;
2093:                } else {
2094:                    bigHead = nbytes - 1;
2095:                    step = -1;
2096:                }
2097:                int result = 0;
2098:                int p = bigHead;
2099:                for (int i = 0; i < nbytes; i++) {
2100:                    result = result << 8;
2101:                    result = result | (bytes[p] & 0xff);
2102:                    p += step;
2103:                }
2104:                return result;
2105:            }
2106:
2107:            private long bytes2long(byte bytes[], ByteOrder order) {
2108:                int nbytes = 8, bigHead, step;
2109:                if (order == ByteOrder.BIG_ENDIAN) {
2110:                    bigHead = 0;
2111:                    step = 1;
2112:                } else {
2113:                    bigHead = nbytes - 1;
2114:                    step = -1;
2115:                }
2116:                long result = 0;
2117:                int p = bigHead;
2118:                for (int i = 0; i < nbytes; i++) {
2119:                    result = result << 8;
2120:                    result = result | (bytes[p] & 0xff);
2121:                    p += step;
2122:                }
2123:                return result;
2124:            }
2125:
2126:            private short bytes2short(byte bytes[], ByteOrder order) {
2127:                int nbytes = 2, bigHead, step;
2128:                if (order == ByteOrder.BIG_ENDIAN) {
2129:                    bigHead = 0;
2130:                    step = 1;
2131:                } else {
2132:                    bigHead = nbytes - 1;
2133:                    step = -1;
2134:                }
2135:                short result = 0;
2136:                int p = bigHead;
2137:                for (int i = 0; i < nbytes; i++) {
2138:                    result = (short) (result << 8);
2139:                    result = (short) (result | (bytes[p] & 0xff));
2140:                    p += step;
2141:                }
2142:                return result;
2143:            }
2144:
2145:            private char bytes2char(byte bytes[], ByteOrder order) {
2146:                return (char) bytes2short(bytes, order);
2147:            }
2148:
2149:            private float bytes2float(byte bytes[], ByteOrder order) {
2150:                return Float.intBitsToFloat(bytes2int(bytes, order));
2151:            }
2152:
2153:            private double bytes2double(byte bytes[], ByteOrder order) {
2154:                return Double.longBitsToDouble(bytes2long(bytes, order));
2155:            }
2156:
2157:            private byte[] int2bytes(int value, ByteOrder order) {
2158:                int nbytes = 4, smallHead, step;
2159:                if (order == ByteOrder.BIG_ENDIAN) {
2160:                    smallHead = nbytes - 1;
2161:                    step = -1;
2162:                } else {
2163:                    smallHead = 0;
2164:                    step = 1;
2165:                }
2166:                byte bytes[] = new byte[nbytes];
2167:                int p = smallHead;
2168:                for (int i = 0; i < nbytes; i++) {
2169:                    bytes[p] = (byte) (value & 0xff);
2170:                    value = value >> 8;
2171:                    p += step;
2172:                }
2173:                return bytes;
2174:            }
2175:
2176:            private byte[] long2bytes(long value, ByteOrder order) {
2177:                int nbytes = 8, smallHead, step;
2178:                if (order == ByteOrder.BIG_ENDIAN) {
2179:                    smallHead = nbytes - 1;
2180:                    step = -1;
2181:                } else {
2182:                    smallHead = 0;
2183:                    step = 1;
2184:                }
2185:                byte bytes[] = new byte[nbytes];
2186:                int p = smallHead;
2187:                for (int i = 0; i < nbytes; i++) {
2188:                    bytes[p] = (byte) (value & 0xff);
2189:                    value = value >> 8;
2190:                    p += step;
2191:                }
2192:                return bytes;
2193:            }
2194:
2195:            private byte[] short2bytes(short value, ByteOrder order) {
2196:                int nbytes = 2, smallHead, step;
2197:                if (order == ByteOrder.BIG_ENDIAN) {
2198:                    smallHead = nbytes - 1;
2199:                    step = -1;
2200:                } else {
2201:                    smallHead = 0;
2202:                    step = 1;
2203:                }
2204:                byte bytes[] = new byte[nbytes];
2205:                int p = smallHead;
2206:                for (int i = 0; i < nbytes; i++) {
2207:                    bytes[p] = (byte) (value & 0xff);
2208:                    value = (short) (value >> 8);
2209:                    p += step;
2210:                }
2211:                return bytes;
2212:            }
2213:
2214:            private byte[] char2bytes(char value, ByteOrder order) {
2215:                return short2bytes((short) value, order);
2216:            }
2217:
2218:            private byte[] float2bytes(float value, ByteOrder order) {
2219:                return int2bytes(Float.floatToRawIntBits(value), order);
2220:            }
2221:
2222:            private byte[] double2bytes(double value, ByteOrder order) {
2223:                return long2bytes(Double.doubleToRawLongBits(value), order);
2224:            }
2225:        }
w__w_w_.__j__a__v__a___2_s_._c_om_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.