ByteBuffer

Get byte from ByteBuffer

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: March 19th, 2019
0 165 1 minute read

With this example we are going to demonstrate how to read bytes from a ByteBuffer. Additionally we will show you several ofByteBuffer‘s API methods in order to share some light on how to randomly ready data from it.
 
 
 
 
 
 
 

package com.javacodegeeks.snippets.core;import java.nio.ByteBuffer;public class GetByteFromByteBuffer {public static void main(String[] args) {// Create a byte arraybyte[] bytes = { 0x00, 0x01, 0x02, 0x03, 0x04};// Wrap a byte array into a bufferByteBuffer buf = ByteBuffer.wrap(bytes);// Get the buffer's capacityint capacity = buf.capacity();// Get the buffer's limitint limit = buf.limit();// Get the buffer's positionint position = buf.position();System.out.println("Buffer capacity: " + capacity);System.out.println("Buffer limit: " + limit);System.out.println("Buffer position: " + position);// Absolute get method. Reads byte at given index (does not affect the position)byte b = buf.get(2);System.out.println("byte: " + b);// Set the positionbuf.position(3);// Relative get method. Reads byte at buffer's current position and increments position.b = buf.get();System.out.println("byte: " + b);// Get remaining byte countint remainingBytes = buf.remaining();System.out.println("Buffer remaining bytes: " + remainingBytes);// Rewinds this buffer. The position is set to zero and the mark is discardedbuf.rewind();remainingBytes = buf.remaining();System.out.println("Buffer remaining bytes: " + remainingBytes);b = buf.get();System.out.println("byte: " + b);}}

Output:

Want to be a Java NIO Master ?
Subscribe to our newsletter and download the JDBCUltimateGuideright now!
In order to help you master Java NIO Library, we have compiled a kick-ass guide with all the major Java NIO features and use cases! Besides studying them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

Buffer capacity: 5Buffer limit: 5Buffer position: 0byte: 2byte: 3Buffer remaining bytes: 1Buffer remaining bytes: 5byte: 0

This was an example of how to read bytes from a ByteBuffer in Java.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: March 19th, 2019
0 165 1 minute read
Photo of Byron Kiourtzoglou

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor atJava Code Geeks.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.