|
| 1 | +package org.javacore.nio; |
| 2 | + |
| 3 | +import java.nio.ByteBuffer; |
| 4 | +import java.nio.CharBuffer; |
| 5 | +import java.nio.DoubleBuffer; |
| 6 | +import java.nio.FloatBuffer; |
| 7 | +import java.nio.IntBuffer; |
| 8 | +import java.nio.LongBuffer; |
| 9 | +import java.nio.ShortBuffer; |
| 10 | + |
| 11 | +/* |
| 12 | + * Copyright [2015] [Jeff Lee] |
| 13 | + * |
| 14 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 15 | + * you may not use this file except in compliance with the License. |
| 16 | + * You may obtain a copy of the License at |
| 17 | + * |
| 18 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | + * |
| 20 | + * Unless required by applicable law or agreed to in writing, software |
| 21 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 22 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 23 | + * See the License for the specific language governing permissions and |
| 24 | + * limitations under the License. |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Jeff Lee |
| 29 | + * @since 2015-10-13 20:40:35 |
| 30 | + * 不同视图下的缓冲区 |
| 31 | + */ |
| 32 | +public class ViewBuffers { |
| 33 | + public static void main(String[] args) { |
| 34 | + ByteBuffer bb = ByteBuffer.wrap(new byte[]{0,0,0,0,0,0,0,'a'}); |
| 35 | + bb.rewind(); |
| 36 | + System.out.print("Byte Buffer "); |
| 37 | + while (bb.hasRemaining()) |
| 38 | + System.out.print(bb.position() + " -> " + bb.get() + ", "); |
| 39 | + System.out.println(); |
| 40 | + |
| 41 | + CharBuffer cb = ((ByteBuffer)bb.rewind()).asCharBuffer(); |
| 42 | + System.out.print("Char Buffer "); |
| 43 | + while (cb.hasRemaining()) |
| 44 | + System.out.print(cb.position() + " -> " + cb.get() + ", "); |
| 45 | + System.out.println(); |
| 46 | + |
| 47 | + ShortBuffer sb = ((ByteBuffer)bb.rewind()).asShortBuffer(); |
| 48 | + System.out.print("Short Buffer "); |
| 49 | + while (sb.hasRemaining()) |
| 50 | + System.out.print(sb.position() + " -> " + sb.get() + ", "); |
| 51 | + System.out.println(); |
| 52 | + |
| 53 | + IntBuffer ib = ((ByteBuffer)bb.rewind()).asIntBuffer(); |
| 54 | + System.out.print("Int Buffer "); |
| 55 | + while (ib.hasRemaining()) |
| 56 | + System.out.print(ib.position() + " -> " + ib.get()); |
| 57 | + System.out.println(); |
| 58 | + |
| 59 | + FloatBuffer fb = ((ByteBuffer)bb.rewind()).asFloatBuffer(); |
| 60 | + System.out.print("Float Buffer "); |
| 61 | + while (fb.hasRemaining()) |
| 62 | + System.out.print(fb.position() + " -> " + fb.get() + ", "); |
| 63 | + System.out.println(); |
| 64 | + |
| 65 | + LongBuffer lb = ((ByteBuffer)bb.rewind()).asLongBuffer(); |
| 66 | + System.out.print("Long Buffer "); |
| 67 | + while (lb.hasRemaining()) |
| 68 | + System.out.print(lb.position() + " -> " + lb.get() + ", "); |
| 69 | + System.out.println(); |
| 70 | + |
| 71 | + DoubleBuffer db = ((ByteBuffer)bb.rewind()).asDoubleBuffer(); |
| 72 | + System.out.print("Double Buffer "); |
| 73 | + while (db.hasRemaining()) |
| 74 | + System.out.print(db.position() + " -> " + db.get() + ", "); |
| 75 | + System.out.println(); |
| 76 | + } |
| 77 | +} |
0 commit comments