Home » Core Java » nio (page 6)

nio

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 »