MyCATApache/Mycat-Server · error · java.lang.UnsupportedOperationException

Heap buffer

Error message

Heap buffer

What it means

MyCatMemoryAllocator is a Netty ByteBufAllocator wrapper dedicated to off-heap (direct) memory management. It deliberately refuses to allocate on-heap buffers: heapBuffer() unconditionally throws UnsupportedOperationException("Heap buffer") instead of delegating to the wrapped allocator. The error means the caller asked for a Java-heap-backed buffer in an allocator that only supports direct buffers.

Solutions

  1. Replace the heapBuffer() call with ioBuffer(), directBuffer(), or compositeDirectBuffer() — the allocator's supported APIs.
  2. If a heap buffer is genuinely required, use a different allocator (e.g. new DefaultByteBufAllocator(...).heapBuffer()) for that code path instead of MyCatMemoryAllocator.
  3. Audit call sites with a ByteBufAllocator parameter and make buffer-type selection explicit rather than relying on heap defaults.
  4. If heap support is intentionally disabled, catch UnsupportedOperationException and fall back to a direct buffer.

Example fix

// before
ByteBuf buf = allocator.heapBuffer();
// after
ByteBuf buf = allocator.directBuffer(); // or allocator.ioBuffer()
Defensive patterns

Strategy: fallback

Validate before calling

if (allocator instanceof MyCatMemoryAllocator) {
    buf = allocator.directBuffer();
} else {
    buf = allocator.heapBuffer();
}

Type guard

boolean supportsHeapBuffers(ByteBufAllocator a) {
    return !(a instanceof MyCatMemoryAllocator);
}

Try / catch

ByteBuf buf;
try {
    buf = allocator.heapBuffer();
} catch (UnsupportedOperationException e) {
    buf = allocator.directBuffer();
}

Prevention

When it happens

Trigger: Calling alloc.heapBuffer() (the no-arg overload at line 145) on a MyCatMemoryAllocator instance, typically wherever code is written against the generic ByteBufAllocator interface and defaults to heap allocation.

Common situations: Frameworks or utility code (e.g. codecs, serialization helpers) that call heapBuffer() by default when handed any ByteBufAllocator; swapping MyCatMemoryAllocator in as a drop-in replacement for UnpooledByteBufAllocator/DefaultByteBufAllocator without auditing heap-buffer usage.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/9a43ec74ab25146a. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/buffer/MyCatMemoryAllocator.java:145

    @Override
    public ByteBuf ioBuffer() {
        return alloc.ioBuffer();
    }

    @Override
    public ByteBuf ioBuffer(int initialCapacity) {
        return alloc.ioBuffer(initialCapacity);
    }

    @Override
    public ByteBuf ioBuffer(int initialCapacity, int maxCapacity) {
        return alloc.ioBuffer(initialCapacity, maxCapacity);
    }

    @Override
    public ByteBuf heapBuffer() {
        throw new UnsupportedOperationException("Heap buffer");
    }

    @Override
    public ByteBuf heapBuffer(int initialCapacity) {
        throw new UnsupportedOperationException("Heap buffer");
    }

    @Override
    public ByteBuf heapBuffer(int initialCapacity, int maxCapacity) {
        throw new UnsupportedOperationException("Heap buffer");
    }

    @Override
    public ByteBuf directBuffer() {
        return alloc.directBuffer();
    }

    @Override

View on GitHub (pinned to 65f8d8beb7)