Getting Different Representations from a ByteBuffer

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Getting Different Representations from a ByteBuffer

This is a example of Java program.

[edit] Java Source Code

  • Package: com.bruceeckel
  • File: GetData.java
package com.bruceeckel;
 
//: c12:GetData.java
//Getting different representations from a ByteBuffer
//From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
//www.BruceEckel.com. See copyright notice in CopyRight.txt.
 
import java.nio.ByteBuffer;
 
public class GetData {
    private static final int BSIZE = 1024;
 
    public static void main(String[] args) {
	ByteBuffer bb = ByteBuffer.allocate(BSIZE);
	// Allocation automatically zeroes the ByteBuffer:
	int i = 0;
	while (i++ < bb.limit())
	    if (bb.get() != 0)
		System.out.println("nonzero");
	System.out.println("i = " + i);
	bb.rewind();
	// Store and read a char array:
	bb.asCharBuffer().put("Howdy!");
	char c;
	while ((c = bb.getChar()) != 0)
	    System.out.print(c + " ");
	System.out.println();
	bb.rewind();
	// Store and read a short:
	bb.asShortBuffer().put((short) 471142);
	System.out.println(bb.getShort());
	bb.rewind();
	// Store and read an int:
	bb.asIntBuffer().put(99471142);
	System.out.println(bb.getInt());
	bb.rewind();
	// Store and read a long:
	bb.asLongBuffer().put(99471142);
	System.out.println(bb.getLong());
	bb.rewind();
	// Store and read a float:
	bb.asFloatBuffer().put(99471142);
	System.out.println(bb.getFloat());
	bb.rewind();
	// Store and read a double:
	bb.asDoubleBuffer().put(99471142);
	System.out.println(bb.getDouble());
	bb.rewind();
 
    }
}

[edit] What Result You Can Get

Run the program, you will get:


i = 1025
H o w d y ! 
12390
99471142
99471142
9.9471144E7
9.9471142E7

[edit] Required External Library for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.

Personal tools