Home » Core Java » nio » ByteBuffer

ByteBuffer

Java Direct ByteBuffer Example

This example shows the usage of direct ByteBuffer. ByteBuffer is an abstract class, extends Buffer and implements Comparable<ByteBuffer>. This class is defined in the java.nio package. A buffer is a container for a fixed amount of data of a specific primitive type. There is a buffer class for each non-boolean primitive type. A ByteBuffer is a sub class of Buffer ...

Read More »

Write/Append to File with ByteBuffer

With this is example we are going to demonstrate how to write/append data to a file in Java using a ByteBuffer. Particularly we are going to read data from a source file and append them to the destination file. In short what we do is the following : Create a File object to encapsulate an actual file in the file ...

Read More »

Use ByteBuffer for non-byte Java types buffering

In this example we will demonstrate how to perform non-byte Java types buffering using a ByteBuffer in Java. In particular we are going to show you how to use a ByteBuffer to store the following Java types : char short int long float double In all cases above we are going to use the standard API methods provided by the ...

Read More »

Use ByteBuffer to store Strings

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 new ByteBuffer and set its size to a number large enough in order to avoid buffer to overflow when putting bytes to it Use the asCharBuffer() API method so ...

Read More »

Get byte from ByteBuffer

With this example we are going to demonstrate how to read bytes from a ByteBuffer. Additionally we will show you several of ByteBuffer‘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) ...

Read More »

Put byte into ByteBuffer

This is an example of how to put bytes into a ByteBuffer in Java. Additionally we will demonstrate several of ByteBuffer‘s API methods in order to share some light on how to randomly write data to it.                 package com.javacodegeeks.snippets.core; import java.nio.ByteBuffer; public class PutByteIntoByteBuffer { public static void main(String[] args) { // ...

Read More »

Create direct and non-direct ByteBuffer

In this example we are going to demonstrate several methods of creating a direct (memory-mapped) and non-direct ByteBuffer in Java. Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer’s content to (or from) an intermediate buffer before ...

Read More »

Convert between ByteBuffer and byte array

With this example we are going to demonstrate how to convert between ByteBuffers and byte arrays. In short, to make a conversion between a ByteBuffer and a byte array you should: Create a byte array and wrap it into a ByteBuffer. The buffer’s capacity and limit will be the array’s length and its position will be zero. Retrieve the bytes ...

Read More »

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns