The documentation for the ByteBuffer class specifies that the getInt() method reads the next four bytes, so if you are only passing two bytes to the call to wrap, you will get a BufferUnderflowException:

Throws: BufferUnderflowException - If there are fewer than four bytes remaining in this buffer

Answer from andersschuller on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java io › guide to bytebuffer
Guide to ByteBuffer | Baeldung
November 28, 2024 - Conceptually, the ByteBuffer class is a byte array wrapped inside an object. It provides many convenient methods to facilitate reading or writing operations from/to underlying data.
🌐
GeeksforGeeks
geeksforgeeks.org › java › bytebuffer-wrap-method-in-java-with-examples
ByteBuffer wrap() method in Java with Examples - GeeksforGeeks
November 1, 2019 - The wrap() method of java.nio.ByteBuffer Class is used to wraps a byte array into a buffer. The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array to be modified and vice versa.
🌐
Java
download.java.net › java › early_access › panama › docs › api › java.base › java › nio › ByteBuffer.html
ByteBuffer (Java SE 19 & JDK 19 [build 1])
public static ByteBuffer wrap · (byte[] array, int offset, int length) Wraps a byte array into a buffer. The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity will be array.length, ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › bytebuffer-wrap-methods-in-java-with-examples
ByteBuffer wrap() methods in Java with Examples - GeeksforGeeks
July 12, 2025 - The wrap() method of java.nio.ByteBuffer Class is used to wraps a byte array into a buffer. The new buffer will be backed by the given byte array, i.e., modifications to the buffer will cause the array to be modified and vice versa.
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.nio.bytebuffer.wrap
ByteBuffer.Wrap Method (Java.Nio) | Microsoft Learn
Microsoft makes no warranties, express or implied, with respect to the information provided here. Wraps a byte array into a buffer. [Android.Runtime.Register("wrap", "([BII)Ljava/nio/ByteBuffer;", "")] public static Java.Nio.ByteBuffer Wrap(byte[] ...
🌐
Android Developers
developer.android.com › api reference › bytebuffer
ByteBuffer | API reference | Android Developers
August 3, 2026 - Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
Classpath
developer.classpath.org › doc › java › nio › ByteBuffer-source.html
Source for java.nio.ByteBuffer (GNU Classpath 0.95 Documentation)
96: */ 97: public static final ByteBuffer wrap (byte[] array) 98: { 99: return wrap (array, 0, array.length); 100: } 101: 102: /** 103: * This method transfers <code>byte</code>s from this buffer into the given 104: * destination array. Before the transfer, it checks if there are fewer than 105: * length <code>byte</code>s remaining in this buffer.
🌐
Baeldung
baeldung.com › home › java › java string › convert a bytebuffer to string in java
Convert a ByteBuffer to String in Java | Baeldung
August 15, 2025 - @Test public void convertUsingNewStringFromBufferArray_thenOK() { String content = "baeldung"; ByteBuffer byteBuffer = ByteBuffer.wrap(content.getBytes()); if (byteBuffer.hasArray()) { String newContent = new String(byteBuffer.array(), charset); assertEquals(content, newContent); } } In Java, we can use new String(bytes, charset) to convert a byte[] to a String.
🌐
Cmu
edelstein.pebbles.cs.cmu.edu › jadeite › main.php
ByteBuffer (Java Platform SE 6)
public static ByteBuffer wrap(byte[] array, int offset, int length) Wraps a byte array into a buffer. The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity will be array.length, ...
🌐
GitHub
github.com › ZenOfAutumn › jdk8 › blob › master › java › nio › ByteBuffer.java
jdk8/java/nio/ByteBuffer.java at master · ZenOfAutumn/jdk8
public static ByteBuffer wrap(byte[] array, int offset, int length) { try { return new HeapByteBuffer(array, offset, length); } catch (IllegalArgumentException x) { throw new IndexOutOfBoundsException(); } } ·
Author: ZenOfAutumn
🌐
(mod :world :scode)
worldmodscode.wordpress.com › 2012 › 12 › 14 › the-java-bytebuffer-a-crash-course
The Java ByteBuffer – a crash course | (mod :world :scode)
December 15, 2012 - For example, here is a method that ... ByteBuffer.wrap(), which is used here, creates a ByteBuffer that wraps the specified byte array, as opposed to copy the contents of it into a new ByteBuffer):...