OnJava8-Examples/newio/ViewBuffers.java

70 lines
2.2 KiB
Java
Raw Normal View History

2015-11-03 12:00:44 -08:00
// newio/ViewBuffers.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
import java.nio.*;
public class ViewBuffers {
public static void main(String[] args) {
ByteBuffer bb = ByteBuffer.wrap(
new byte[]{ 0, 0, 0, 0, 0, 0, 0, 'a' });
bb.rewind();
2015-11-03 12:00:44 -08:00
System.out.print("Byte Buffer ");
2015-06-15 17:47:35 -07:00
while(bb.hasRemaining())
2015-12-02 09:20:27 -08:00
System.out.print(
bb.position()+ " -> " + bb.get() + ", ");
2015-11-03 12:00:44 -08:00
System.out.println();
2015-06-15 17:47:35 -07:00
CharBuffer cb =
((ByteBuffer)bb.rewind()).asCharBuffer();
2015-11-03 12:00:44 -08:00
System.out.print("Char Buffer ");
2015-06-15 17:47:35 -07:00
while(cb.hasRemaining())
2015-12-02 09:20:27 -08:00
System.out.print(
cb.position() + " -> " + cb.get() + ", ");
2015-11-03 12:00:44 -08:00
System.out.println();
2015-06-15 17:47:35 -07:00
FloatBuffer fb =
((ByteBuffer)bb.rewind()).asFloatBuffer();
2015-11-03 12:00:44 -08:00
System.out.print("Float Buffer ");
2015-06-15 17:47:35 -07:00
while(fb.hasRemaining())
2015-12-02 09:20:27 -08:00
System.out.print(
fb.position()+ " -> " + fb.get() + ", ");
2015-11-03 12:00:44 -08:00
System.out.println();
2015-06-15 17:47:35 -07:00
IntBuffer ib =
((ByteBuffer)bb.rewind()).asIntBuffer();
2015-11-03 12:00:44 -08:00
System.out.print("Int Buffer ");
2015-06-15 17:47:35 -07:00
while(ib.hasRemaining())
2015-12-02 09:20:27 -08:00
System.out.print(
ib.position()+ " -> " + ib.get() + ", ");
2015-11-03 12:00:44 -08:00
System.out.println();
2015-06-15 17:47:35 -07:00
LongBuffer lb =
((ByteBuffer)bb.rewind()).asLongBuffer();
2015-11-03 12:00:44 -08:00
System.out.print("Long Buffer ");
2015-06-15 17:47:35 -07:00
while(lb.hasRemaining())
2015-12-02 09:20:27 -08:00
System.out.print(
lb.position()+ " -> " + lb.get() + ", ");
2015-11-03 12:00:44 -08:00
System.out.println();
2015-06-15 17:47:35 -07:00
ShortBuffer sb =
((ByteBuffer)bb.rewind()).asShortBuffer();
2015-11-03 12:00:44 -08:00
System.out.print("Short Buffer ");
2015-06-15 17:47:35 -07:00
while(sb.hasRemaining())
2015-12-02 09:20:27 -08:00
System.out.print(
sb.position()+ " -> " + sb.get() + ", ");
2015-11-03 12:00:44 -08:00
System.out.println();
2015-06-15 17:47:35 -07:00
DoubleBuffer db =
((ByteBuffer)bb.rewind()).asDoubleBuffer();
2015-11-03 12:00:44 -08:00
System.out.print("Double Buffer ");
2015-06-15 17:47:35 -07:00
while(db.hasRemaining())
2015-12-02 09:20:27 -08:00
System.out.print(
db.position()+ " -> " + db.get() + ", ");
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
Byte Buffer 0 -> 0, 1 -> 0, 2 -> 0, 3 -> 0, 4 -> 0, 5 -> 0,
6 -> 0, 7 -> 97,
Char Buffer 0 -> NUL, 1 -> NUL, 2 -> NUL, 3 -> a,
Float Buffer 0 -> 0.0, 1 -> 1.36E-43,
Int Buffer 0 -> 0, 1 -> 97,
Long Buffer 0 -> 97,
Short Buffer 0 -> 0, 1 -> 0, 2 -> 0, 3 -> 97,
Double Buffer 0 -> 4.8E-322,
2015-09-07 11:44:36 -06:00
*/