| 8 | public class GetData { |
| 9 | private static final int BSIZE = 1024; |
| 10 | public static void main(String[] args) { |
| 11 | ByteBuffer bb = ByteBuffer.allocate(BSIZE); |
| 12 | // Allocation automatically zeroes the ByteBuffer: |
| 13 | int i = 0; |
| 14 | while(i++ < bb.limit()) |
| 15 | if(bb.get() != 0) |
| 16 | System.out.println("nonzero"); |
| 17 | System.out.println("i = " + i); |
| 18 | bb.rewind(); |
| 19 | // Store and read a char array: |
| 20 | bb.asCharBuffer().put("Howdy!"); |
| 21 | char c; |
| 22 | while((c = bb.getChar()) != 0) |
| 23 | System.out.print(c + " "); |
| 24 | System.out.println(); |
| 25 | bb.rewind(); |
| 26 | // Store and read a short: |
| 27 | bb.asShortBuffer().put((short)471142); |
| 28 | System.out.println(bb.getShort()); |
| 29 | bb.rewind(); |
| 30 | // Store and read an int: |
| 31 | bb.asIntBuffer().put(99471142); |
| 32 | System.out.println(bb.getInt()); |
| 33 | bb.rewind(); |
| 34 | // Store and read a long: |
| 35 | bb.asLongBuffer().put(99471142); |
| 36 | System.out.println(bb.getLong()); |
| 37 | bb.rewind(); |
| 38 | // Store and read a float: |
| 39 | bb.asFloatBuffer().put(99471142); |
| 40 | System.out.println(bb.getFloat()); |
| 41 | bb.rewind(); |
| 42 | // Store and read a double: |
| 43 | bb.asDoubleBuffer().put(99471142); |
| 44 | System.out.println(bb.getDouble()); |
| 45 | bb.rewind(); |
| 46 | } |
| 47 | } |
| 48 | /* Output: |
| 49 | i = 1025 |