Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is the ByteBuffer arrayOffset() method in Java?

Syed Muhammad Ali Mustafa

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.

Answers Code

The java.nio.ByteBuffer is a class we can use to store a buffer of bytes. The arrayOffset() method of this class returns the offset of the buffer’s first element inside the buffer’s backing array.

Declaration

The ByteBuffer.arrayOffset() method is declared as follows:

buff.arrayOffset()
  • buff: The ByteBuffer whose first element offset is required.

Return value

The ByteBuffer.arrayOffest() method returns an int that is the offset of the first element of buff.

  • If the buffer is not backed by an accessible array, UnsupportedOperationException is thrown.
  • If the buffer is backed by an array but the array is read-only, ReadOnlyBufferException is thrown.

Code

Example 1

Consider the code snippet below, which demonstrates the use of the ByteBuffer.arrayOffset() method.

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 5;
try {
ByteBuffer buff1 = ByteBuffer.allocate(n1);
buff1.put((byte)1);
buff1.put((byte)4);
System.out.println("buff1: " + Arrays.toString(buff1.array()));
int foo = buff1.arrayOffset();
System.out.println("\nbuff1 array offset: " + foo);
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
} catch (UnsupportedOperationException e) {
System.out.println("Error!!! UnsupportedOperationException");
}
}
}

Explanation

  • An ByteBuffer buff1 is declared in line 7 with capacity n1 = 5.
  • Two elements are added to buff1 using the put() method in lines 8-9.
  • The ByteBuffer.arrayOffset() method is used in line 12 to get the offset of the first element of buff1 inside its backing array.

Example 2

Consider another example of the ByteBuffer.arrayOffset() method in which ReadOnlyBufferException is thrown.

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 5;
try {
ByteBuffer buff1 = ByteBuffer.allocate(n1);
buff1.put((byte)1);
buff1.put((byte)4);
System.out.println("buff1: " + Arrays.toString(buff1.array()));
ByteBuffer buff2 = buff1.asReadOnlyBuffer();
int foo = buff2.arrayOffset();
System.out.println("\nbuff2 array offset: " + foo);
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
} catch (UnsupportedOperationException e) {
System.out.println("Error!!! UnsupportedOperationException");
}
}
}

Explanation

  • An ByteBuffer buff1 is declared in line 7 with capacity n1 = 5.
  • Two elements are added to buff1 using the put() method in lines 8-9.
  • An ByteBuffer buff2 is declared in line 12 that is the read-only copy of buff1.
  • The ByteBuffer.arrayOffset() method is used in line 14 to get the offset of the first element of buff2 inside its backing array. The ReadOnlyBufferException is thrown because buff2 is read-only.

RELATED TAGS

java

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved

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.

Answers Code
Keep Exploring