libgdx/libgdx · error · UnsupportedOperationException
UnsupportedOperationException
Error message
UnsupportedOperationException
What it means
DirectReadWriteShortBufferAdapter — the ShortBuffer view of a GWT emulated direct ByteBuffer (asShortBuffer()) — throws UnsupportedOperationException from protectedArray() because a direct view has no short[] backing array. The emulated ShortBuffer.array() delegates to this method, so any array() call on the view throws. protectedHasArray() returns false, so hasArray() is the correct pre-check.
Source
Thrown at backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/java/nio/DirectReadWriteShortBufferAdapter.java:116
@Override
public boolean isDirect () {
return true;
}
@Override
public boolean isReadOnly () {
return false;
}
@Override
public ByteOrder order () {
return byteBuffer.order();
}
@Override
protected short[] protectedArray () {
throw new UnsupportedOperationException();
}
@Override
protected int protectedArrayOffset () {
throw new UnsupportedOperationException();
}
@Override
protected boolean protectedHasArray () {
return false;
}
@Override
public ShortBuffer put (short c) {
// if (position == limit) {
// throw new BufferOverflowException();
// }
shortArray.set(position++, c);View on GitHub (pinned to 97f4086187)
Solutions
- Check sb.hasArray() first; for direct views, bulk-copy with sb.get(short[]) or use relative/absolute puts.
- Build data in a short[] then ShortBuffer.wrap(arr) or use put(short[]) instead of exposing the internal array.
- Use libGDX's ShortArray or number-utils array helpers on GWT instead of nio array access.
Example fix
// before short[] verts = sb.array(); // UnsupportedOperationException // after short[] verts = new short[sb.remaining()]; sb.get(verts);
Defensive patterns
Strategy: type-guard
Validate before calling
if (sb.hasArray()) {
short[] arr = sb.array();
} else {
short[] arr = new short[sb.remaining()];
sb.get(arr);
} Type guard
boolean isHeapBacked(ShortBuffer sb) {
return sb != null && sb.hasArray();
} Try / catch
try {
short[] a = sb.array();
} catch (UnsupportedOperationException e) {
short[] a = new short[sb.remaining()];
sb.get(a);
} Prevention
- Guard every array() call with hasArray() in cross-platform code.
- Use put(short[]) to fill buffers instead of exposing internals.
- Test mesh/vertex builders under the GWT backend where views are direct.
When it happens
Trigger: Calling shortBuffer.array() on a ShortBuffer from ByteBuffer.asShortBuffer() in a GWT build; typical in vertex-data builders that memcpy from the raw array, or audio code that assumed a heap buffer.
Common situations: Mesh construction via FloatBuffer/ShortBuffer views that worked on desktop JVM direct buffers with array() hacks; libraries calling array() without checking hasArray().
Related errors
- UnsupportedOperationException
- UnsupportedOperationException
- BufferOverflowException
- ReadOnlyBufferException
- BufferUnderflowException
AI-assisted analysis of libgdx/libgdx@97f4086187 (2026-08-14).
Data as JSON: /api/errors/be923eee7cadffc2.
Report an issue: GitHub.