Home » bytebuffer

Tag Archives: bytebuffer

Java Nio BufferOverflowException Example

Exceptions are the unwanted or the unexpected events that occur during the execution of programs that disrupt the normal flow of the instructions. In this tutorial, we will learn about the BufferOverflowException which is very common in the Java Nio package. But before moving ahead let’s take a look and understand the basics of the Java Nio package.     ...

Read More »

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 »

Java write to File Example

In this post, we feature a comprehensive example of how to write to File in Java using FileWriter, FileOutputStream, and NIO. We are going to investigate several methods to write a File in Java. We will list some of the older, before Java 7 and NIO, methods to write to a text or a binary file as well as some ...

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 ...

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 »