ByteBuffer

Use ByteBuffer to store Strings

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: March 19th, 2019
0 489 1 minute read

This is an example of how to store Strings using a ByteBuffer in Java. In order to use a ByteBuffer to store Strings in Java we have to :

  • Allocate a newByteBuffer and set its size to a number large enough in order to avoid buffer to overflow when putting bytes to it
  • Use theasCharBuffer() API method so as to be able to put characters directly into the byte buffer
  • Using theput(String) API method we can put a String directly to the byte buffer
  • ThetoString() API method returns the string representation of the ByteBuffer’s contents. Do not forget toflip() the ByteBuffer since thetoString() API method displays ByteBuffer’s contents from the current buffer’s position on-wards
  • as shown in the code snippet below.

 

package com.javacodegeeks.snippets.core;import java.nio.ByteBuffer;import java.nio.CharBuffer;public class UseByteBufferToStoreStrings {public static void main(String[] args) {// Allocate a new non-direct byte buffer with a 50 byte capacity    // set this to a big value to avoid BufferOverflowExceptionByteBuffer buf = ByteBuffer.allocate(50); // Creates a view of this byte buffer as a char bufferCharBuffer cbuf = buf.asCharBuffer();// Write a string to char buffercbuf.put("Java Code Geeks");// Flips this buffer.  The limit is set to the current position and then// the position is set to zero.  If the mark is defined then it is discardedcbuf.flip();String s = cbuf.toString();  // a stringSystem.out.println(s);}}

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.

Java Code Geeks

This was an example of how to use a ByteBuffer to store Strings 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 Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: March 19th, 2019
0 489 1 minute read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He 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.