Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the ByteBuffer hasArray() method in Java?

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

In Java, the hasArray() method of the ByteBuffer class confirms whether or not a ByteBuffer object has an accessible byte array.

The process is illustrated below:

To use the hasArray() method, you will need to import the ByteBuffer class into your program, as shown below:

import java.nio.ByteBuffer

The prototype of the hasArray() method is shown below:

public final boolean hasArray()

Parameters

The hasArray() method does not accept any parameters.

Return value

If the buffer has an accessible byte array and is not set to read-only, then the hasArray() method returns true. Otherwise, it returns false.

Note: If the hasArray() method returns true, then you can safely invoke the array() and arrayOffset() methods.

Example

The code below shows how the hasArray() method can be used in Java:

import java.nio.*;
import java.util.*;
class hasArrayMethod {
public static void main(String[] args) {
// initialize ByteBuffer instance
ByteBuffer bufferOne = ByteBuffer.allocate(5);
byte values[] = {2, 1, 6, 7, 10};
// add values to buffer
for(int i = 0; i < 5; i++)
{
bufferOne.put(values[i]);
}
// create a new read-only buffer
ByteBuffer bufferTwo = bufferOne.asReadOnlyBuffer();
// check if bufferOne has a backing array
if(bufferOne.hasArray())
{
System.out.println("BufferOne has an accessible byte array.");
}
else
{
System.out.println("BufferOne does not have an accessible byte array.");
}
// check if bufferTwo has a backing array
if(bufferTwo.hasArray())
{
System.out.println("BufferTwo has an accessible byte array.");
}
else
{
System.out.println("BufferTwo does not have an accessible byte array.");
}
}
}

Explanation

First, a ByteBuffer object called bufferOne is initialized through the allocate() method. bufferOne has the capacity to store five elements.

Next, a for loop adds values to bufferOne through the put() method.

A second ByteBuffer object called bufferTwo is initialized in line 1919. Since bufferTwo is created through the asReadOnlyBuffer() method, it has the same values as bufferOne, but its read-only attribute is set as true.

The hasArray() method in line 2222 checks if bufferOne has an accessible byte array. Since bufferOne does have a valid byte array and is not set to read-only, the hasArray() method returns true.

Finally, the hasArray() method in line 3232 checks if bufferTwo has an accessible byte array. Since bufferTwo is set to read-only, the hasArray() method returns false.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring