A developer compares two Java ByteBuffer reads of the same four bytes, showing the little-endian parent and its big-endian slice.

Why a Java ByteBuffer Slice Reads the Right Bytes in the Wrong Order

Fri, Dec 4, 2026

I am reviewing this refactor as a binary-interface problem, not as a transport or performance problem. The complete ten-byte frame is already in one heap-backed ByteBuffer, on one thread. The first two bytes are the header CA FE; bytes 2..5 are the little-endian representation 78 56 34 12, which the wire contract says means 0x12345678; bytes 6..9 are 04 03 02 01, meaning 0x01020304. The release question is whether the decoder still produces those two numbers after its final Java NIO view has been constructed.

The important failure is reproducible: a parent configured with LITTLE_ENDIAN reads the first payload field as 0x12345678, while a fresh slice(), duplicate(), or asReadOnlyBuffer() reads the same four bytes as 0x78563412 until the resulting byte view is explicitly configured. This is not an implementation accident. In the Java SE 21 ByteBuffer contract, those byte-buffer view-producing methods give the new buffer BIG_ENDIAN byte order; asIntBuffer() instead captures the source byte buffer's order at typed-view creation time.

I executed the probe in this guide on OpenJDK 21.0.12.1, Debian build 21.0.12.1+1-1-deb13u1-Debian, on 4 December 2026. The results agree with a separate observation on OpenJDK 21.0.13.1. The current candidate's decision is therefore FIX VIEW CONSTRUCTION: the wire bytes and field addresses are correct, but the final decoder-facing view can carry the wrong byte order.

Write down the wire contract

I start with the contract because byte order is not something the JVM gets to infer from the machine. ByteOrder.LITTLE_ENDIAN means that the least-significant byte of a multibyte value comes first; BIG_ENDIAN means the most-significant byte comes first. ByteOrder.nativeOrder() reports the platform's native order, but that is a platform property, not a substitute for a protocol declaration. Java SE 21 documents those meanings explicitly.

For this review, the frame contract is deliberately small:

Frame offset

Width

Meaning

Raw bytes

Contract value

0

2 bytes

Header

CA FE

literal header

2

4 bytes

Field A

78 56 34 12

little-endian 0x12345678 = 305419896

6

4 bytes

Field B

04 03 02 01

little-endian 0x01020304 = 16909060

The payload origin is byte offset 2. Both fields are signed Java int values, but these test values are positive, so their decimal renderings do not introduce signedness ambiguity.

The schema owner owns those offsets and the little-endian declaration. The ingestion boundary owns only the proposition that this exact ten-byte frame is complete and available. This investigation begins after checking that all input bytes arrived. I do not reopen InputStream.available(), EOF handling, partial reads, network framing, or acquisition timing here.

That distinction matters operationally. If the literal frame is already fixed in memory, then three questions can be tested independently: did I choose the right raw bytes, did I choose the right local index in the current view, and did that view interpret the four selected bytes in the contract's byte order?

The Java Buffer abstraction supports that isolation because position, limit, and capacity describe the valid region and cursor state, while absolute operations use an explicit index and do not advance the position.

Build a ten-byte frame with an independent oracle

The fixture is one array created from one literal. ByteBuffer.wrap(byte[]) creates a buffer backed by that array and initially uses BIG_ENDIAN; I configure order only where the test requires it. Java SE 21 specifies both the shared backing-array relationship and the initial order of a wrapped byte buffer.

I executed the probe with:

openjdk 21.0.12.1 2026-08-18
OpenJDK Runtime Environment (build 21.0.12.1+1-1-deb13u1-Debian)
OpenJDK 64-Bit Server VM (build 21.0.12.1+1-1-deb13u1-Debian, mixed mode, sharing)

The exact source-launcher commands are:

java --version
java ByteOrderProbe.java

Here is the complete single-file probe. It uses explicit AssertionError checks, so no -ea switch is required.

import java.nio.*;
import java.util.HexFormat;

public class ByteOrderProbe {
    static final byte[] FRAME =
            HexFormat.of().parseHex("cafe7856341204030201");

    static final int FIELD_A = 0x12345678;
    static final int FIELD_B = 0x01020304;

    static ByteBuffer parent() {
        return ByteBuffer.wrap(FRAME)
                .order(ByteOrder.LITTLE_ENDIAN)
                .position(2)
                .limit(10);
    }

    static void eq(String name, int expected, int actual) {
        if (expected != actual) {
            throw new AssertionError(
                    name + ": expected " + hex(expected)
                    + ", got " + hex(actual));
        }
    }

    static void neq(String name, int forbidden, int actual) {
        if (forbidden == actual) {
            throw new AssertionError(
                    name + ": unexpectedly matched " + hex(actual));
        }
    }

    static String hex(int v) {
        return String.format("0x%08X", v);
    }

    static String raw(ByteBuffer b, int i) {
        return HexFormat.ofDelimiter(" ").withUpperCase()
                .formatHex(new byte[] {
                        b.get(i), b.get(i + 1),
                        b.get(i + 2), b.get(i + 3)
                });
    }

    static int manualLE(int i) {
        return (FRAME[i] & 0xff)
                | ((FRAME[i + 1] & 0xff) << 8)
                | ((FRAME[i + 2] & 0xff) << 16)
                | ((FRAME[i + 3] & 0xff) << 24);
    }

    static void byteRow(
            String name, ByteBuffer b, int i, int expected) {
        int before = b.position();
        int actual = b.getInt(i);

        System.out.printf(
                "%-27s idx=%d raw=%s order=%-13s "
                + "expected=%s/%d observed=%s/%d "
                + "pos=%d ro=%s%n",
                name, i, raw(b, i), b.order(),
                hex(expected), expected,
                hex(actual), actual,
                before, b.isReadOnly());

        if (b.position() != before) {
            throw new AssertionError(name + ": indexed get moved position");
        }
    }

    static void intRow(
            String name, IntBuffer b, int i, int expected) {
        int before = b.position();
        int actual = b.get(i);

        System.out.printf(
                "%-27s idx=%d order=%-13s "
                + "expected=%s/%d observed=%s/%d "
                + "pos=%d ro=%s%n",
                name, i, b.order(),
                hex(expected), expected,
                hex(actual), actual,
                before, b.isReadOnly());

        if (b.position() != before) {
            throw new AssertionError(name + ": indexed get moved position");
        }
    }

    public static void main(String[] args) {
        eq("manual A", FIELD_A, manualLE(2));
        eq("manual B", FIELD_B, manualLE(6));

        ByteBuffer p = parent();
        byteRow("parent", p, 2, FIELD_A);
        byteRow("parent", p, 6, FIELD_B);
        eq("parent A", FIELD_A, p.getInt(2));
        eq("parent B", FIELD_B, p.getInt(6));

        ByteBuffer s = parent().slice();
        byteRow("fresh slice", s, 0, FIELD_A);
        byteRow("fresh slice", s, 4, FIELD_B);
        neq("slice A wrong order", FIELD_A, s.getInt(0));
        neq("slice B wrong order", FIELD_B, s.getInt(4));

        ByteBuffer d = parent().duplicate();
        byteRow("fresh duplicate", d, 2, FIELD_A);
        byteRow("fresh duplicate", d, 6, FIELD_B);
        neq("duplicate A wrong order", FIELD_A, d.getInt(2));

        ByteBuffer ro = parent().asReadOnlyBuffer();
        byteRow("fresh readonly", ro, 2, FIELD_A);
        byteRow("fresh readonly", ro, 6, FIELD_B);
        neq("readonly A wrong order", FIELD_A, ro.getInt(2));

        ByteBuffer bad =
                parent().slice()
                        .order(ByteOrder.LITTLE_ENDIAN)
                        .asReadOnlyBuffer();

        byteRow("slice LE then readonly", bad, 0, FIELD_A);
        neq("wrapper reset order", FIELD_A, bad.getInt(0));

        ByteBuffer good =
                parent().slice()
                        .asReadOnlyBuffer()
                        .order(ByteOrder.LITTLE_ENDIAN);

        byteRow("slice readonly then LE", good, 0, FIELD_A);
        byteRow("slice readonly then LE", good, 4, FIELD_B);
        eq("good A", FIELD_A, good.getInt(0));
        eq("good B", FIELD_B, good.getInt(4));

        ByteBuffer typedSource = parent().slice();
        IntBuffer before = typedSource.asIntBuffer();

        typedSource.order(ByteOrder.LITTLE_ENDIAN);
        IntBuffer after = typedSource.asIntBuffer();

        typedSource.order(ByteOrder.BIG_ENDIAN);

        intRow("IntBuffer before LE", before, 0, FIELD_A);
        intRow("IntBuffer before LE", before, 1, FIELD_B);
        neq("typed before wrong", FIELD_A, before.get(0));

        intRow("IntBuffer after LE", after, 0, FIELD_A);
        intRow("IntBuffer after LE", after, 1, FIELD_B);
        eq("typed after A", FIELD_A, after.get(0));
        eq("typed after B", FIELD_B, after.get(1));

        // Deliberately weak control. Separate bytes; FRAME is not mutated.
        byte[] palindrome =
                HexFormat.of().parseHex("11222211");

        ByteBuffer pbe = ByteBuffer.wrap(palindrome)
                .order(ByteOrder.BIG_ENDIAN);
        ByteBuffer ple = ByteBuffer.wrap(palindrome)
                .order(ByteOrder.LITTLE_ENDIAN);

        eq("palindrome hides order", pbe.getInt(0), ple.getInt(0));

        System.out.printf(
                "palindrome raw=11 22 22 11 BE=%s LE=%s%n",
                hex(pbe.getInt(0)), hex(ple.getInt(0)));

        System.out.println("PASS: all assertions completed");
    }
}

Derive the two expected integers by hand

The oracle must not depend on the same ByteBuffer behavior under investigation.

For Field A, bytes 78 56 34 12 at parent offsets 2..5 are interpreted little-endian:

0x78
| (0x56 << 8)
| (0x34 << 16)
| (0x12 << 24)
= 0x12345678
= 305419896

Field B is the same construction:

0x04
| (0x03 << 8)
| (0x02 << 16)
| (0x01 << 24)
= 0x01020304
= 16909060

The code masks each byte with 0xff before shifting so Java's signed byte promotion cannot contaminate the oracle.

Establish a passing parent read

The passing control is the parent itself:

ByteBuffer p = ByteBuffer.wrap(FRAME)
        .order(ByteOrder.LITTLE_ENDIAN)
        .position(2)
        .limit(10);

int a = p.getInt(2);
int b = p.getInt(6);

Java SE 21 specifies that getInt(int) reads four bytes beginning at the supplied byte index and composes them according to the byte buffer's current order. The generic Buffer contract separately states that absolute operations do not affect position.

The executed results were:

parent idx=2 raw=78 56 34 12 order=LITTLE_ENDIAN
expected=0x12345678/305419896
observed=0x12345678/305419896 pos=2 ro=false

parent idx=6 raw=04 03 02 01 order=LITTLE_ENDIAN
expected=0x01020304/16909060
observed=0x01020304/16909060 pos=2 ro=false

Position remained 2 across both indexed reads.

This control proves something narrow and useful: with the supplied bytes, those parent indices, and explicit LITTLE_ENDIAN, Java's four-byte reads agree with the independent oracle. It does not prove that a child view returned by a refactored decoder factory will inherit that order. The next cases deliberately test that assumption.

Reproduce the failure on a slice

With parent position 2 and limit 10, slice() creates a view beginning at the parent's current position. Its position is 0. Its limit and capacity both equal the eight bytes remaining in the parent, and the two views share their content. Crucially, Java SE 21 specifies the new ByteBuffer slice's byte order as BIG_ENDIAN.

So this apparently reasonable sequence contains the bug:

ByteBuffer parent = parent(); // LITTLE_ENDIAN, position 2
ByteBuffer payload = parent.slice();

System.out.println(payload.order()); // BIG_ENDIAN
int a = payload.getInt(0);

The executed result was:

fresh slice idx=0 raw=78 56 34 12 order=BIG_ENDIAN
expected=0x12345678/305419896
observed=0x78563412/2018915346 pos=0 ro=false

Nothing happened to the bytes. 78 56 34 12 are still exactly the bytes intended for Field A. Nothing happened to the address either: local index 0 correctly denotes the first byte of the payload. Only interpretation changed.

Field B gives the same diagnostic:

fresh slice idx=4 raw=04 03 02 01 order=BIG_ENDIAN
expected=0x01020304/16909060
observed=0x04030201/67305985 pos=0 ro=false

The right storage and local field address can produce the wrong integer because a freshly constructed byte view has its own order.

The scope is intentionally different from the consequences of borrowing mutable buffers in Go. Here the backing bytes do not change after construction. Shared storage is useful evidence because it removes copying and mutation from the causal chain.

Trace slice index zero back to the parent

The index translation is mechanical:

Parent index

Slice index

Byte

2

0

78

3

1

56

4

2

34

5

3

12

6

4

04

7

5

03

8

6

02

9

7

01

java-bytebuffer-img-first    java-bytebuffer-img-second

This is why slice.getInt(2) would not be a second test of Field A. It would begin at parent byte 4 and consume 34 12 04 03, crossing the contractual field boundary.

I give that failure a different diagnostic: address mismatch. An order mismatch preserves the expected four raw bytes at the chosen address but changes their numeric interpretation. An address mismatch changes the four-byte window itself.

Compare a duplicate and a read-only view

duplicate() differs from slice() in indexing. The duplicate shares the parent's entire content and receives the parent's capacity, limit, position, and mark values, so the field indices remain 2 and 6. Yet Java SE 21 still specifies BIG_ENDIAN as the duplicate byte buffer's initial order.

Executed:

fresh duplicate idx=2 raw=78 56 34 12 order=BIG_ENDIAN
expected=0x12345678/305419896
observed=0x78563412/2018915346 pos=2 ro=false

fresh duplicate idx=6 raw=04 03 02 01 order=BIG_ENDIAN
expected=0x01020304/16909060
observed=0x04030201/67305985 pos=2 ro=false

asReadOnlyBuffer() likewise preserves capacity, limit, position, and mark, shares content, prevents mutation through the returned view, and specifies BIG_ENDIAN for the new byte buffer.

Executed:

fresh readonly idx=2 raw=78 56 34 12 order=BIG_ENDIAN
expected=0x12345678/305419896
observed=0x78563412/2018915346 pos=2 ro=true

fresh readonly idx=6 raw=04 03 02 01 order=BIG_ENDIAN
expected=0x01020304/16909060
observed=0x04030201/67305985 pos=2 ro=true

The duplicate and read-only views retain parent-style indices, unlike a slice, but they do not make parent byte order part of their interpretation contract.

The read-only flag is orthogonal to endianness. isReadOnly() == true answers whether writes through that view are allowed. It does not mean “same decoding configuration as the mutable source.”

Find the last byte view in the call chain

A common refactor is to configure a payload and then wrap it defensively:

return parent.slice()
        .order(ByteOrder.LITTLE_ENDIAN)
        .asReadOnlyBuffer();

That sequence looks appealing because the order-setting call is visible. It is nevertheless wrong for this contract. asReadOnlyBuffer() constructs another byte-buffer view, and Java SE 21 specifies that newly created byte view as BIG_ENDIAN.

The executed negative control was:

slice LE then readonly idx=0 raw=78 56 34 12 order=BIG_ENDIAN
expected=0x12345678/305419896
observed=0x78563412/2018915346 pos=0 ro=true

Move order configuration to the final byte view instead:

return parent.slice()
        .asReadOnlyBuffer()
        .order(ByteOrder.LITTLE_ENDIAN);

Executed:

slice readonly then LE idx=0 raw=78 56 34 12 order=LITTLE_ENDIAN
expected=0x12345678/305419896
observed=0x12345678/305419896 pos=0 ro=true

slice readonly then LE idx=4 raw=04 03 02 01 order=LITTLE_ENDIAN
expected=0x01020304/16909060
observed=0x01020304/16909060 pos=0 ro=true

This is still one thread and one shared frame. I am not testing handoff semantics such as ownership when transferring a browser buffer. The relevant ownership boundary here is the Java factory boundary: which view object does the decoder actually receive?

Check where configuration belongs

I want a factory test that inspects the returned object, not an intermediate:

static ByteBuffer decoderPayload(ByteBuffer parent) {
    return parent.slice()
            .asReadOnlyBuffer()
            .order(ByteOrder.LITTLE_ENDIAN);
}

ByteBuffer payload = decoderPayload(parent());

if (payload.order() != ByteOrder.LITTLE_ENDIAN) {
    throw new AssertionError("final payload order changed");
}

eq("A", FIELD_A, payload.getInt(0));
eq("B", FIELD_B, payload.getInt(4));

The corresponding negative control configures only the parent:

ByteBuffer parent = parent();    // LITTLE_ENDIAN
ByteBuffer payload = parent.slice();

if (payload.order() != ByteOrder.BIG_ENDIAN) {
    throw new AssertionError("unexpected slice order");
}

That control prevents a future reviewer from concluding that setting the parent once is sufficient.

The practical rule is: audit construction from the final consumer backward. Any later slice(), duplicate(), or asReadOnlyBuffer() is a new Java ByteBuffer interpretation boundary under the Java SE 21 contract.

Create an IntBuffer at two different moments

Typed views have a related but different rule. ByteBuffer.asIntBuffer() creates an IntBuffer beginning at the byte buffer's current position; its capacity is the remaining byte count divided by four; and its byte order is the source byte buffer's order at the moment the typed view is created.

That timing deserves a dedicated regression test.

Start from a fresh slice. The slice begins in BIG_ENDIAN:

ByteBuffer source = parent().slice();

IntBuffer before = source.asIntBuffer();

source.order(ByteOrder.LITTLE_ENDIAN);
IntBuffer after = source.asIntBuffer();

source.order(ByteOrder.BIG_ENDIAN);

Now inspect both typed views. Java SE 21's IntBuffer.order() reports the byte order used by that typed buffer; an indexed get(int) addresses an integer element rather than a byte index.

Executed:

IntBuffer before LE idx=0 order=BIG_ENDIAN
expected=0x12345678/305419896
observed=0x78563412/2018915346 pos=0 ro=false

IntBuffer before LE idx=1 order=BIG_ENDIAN
expected=0x01020304/16909060
observed=0x04030201/67305985 pos=0 ro=false

IntBuffer after LE idx=0 order=LITTLE_ENDIAN
expected=0x12345678/305419896
observed=0x12345678/305419896 pos=0 ro=false

IntBuffer after LE idx=1 order=LITTLE_ENDIAN
expected=0x01020304/16909060
observed=0x01020304/16909060 pos=0 ro=false

Changing source back to BIG_ENDIAN after constructing after did not change after.order() or its results. That matches the documented creation-time capture rule.

A separate observation on OpenJDK 21.0.13.1 showed the same result. An IntBuffer created after configuring the slice read both contractual values and retained that typed order after the slice itself was changed back. The results shown here are from 21.0.12.1.

Translate an integer index into a byte offset

Typed indexing changes units.

For the eight-byte payload:

IntBuffer element 0 -> slice bytes 0..3 -> parent bytes 2..5
IntBuffer element 1 -> slice bytes 4..7 -> parent bytes 6..9

Read the second element with this indexed call:

after.get(1)

This call does not mean “read at byte offset one.” It means “read the second four-byte integer element.”

The typed view also owns its own position, measured in integer elements. Absolute IntBuffer.get(int) reads the specified element without moving that position, consistent with the general absolute-operation rule in Buffer.

For this fixture, both typed-view elements must be checked against the same manual oracle used for byte-buffer reads. That keeps the expectation independent from Java NIO view construction.

Identify controls that can hide the defect

A byte-order regression can evade tests that never perform a multibyte interpretation.

For example:

byte b0 = payload.get(0);
byte b1 = payload.get(1);
byte b2 = payload.get(2);
byte b3 = payload.get(3);

Those byte-at-a-time reads return 78, 56, 34, and 12 regardless of the byte buffer's byte order because each operation transfers one byte. Byte order matters when the buffer composes multiple bytes into a primitive value such as an int. Java SE 21 describes getInt specifically in terms of composing four bytes according to current byte order.

That makes byte equality a necessary diagnostic but not a sufficient decoder acceptance test.

A second weak control is a byte palindrome. I use separate test bytes so the shared ten-byte frame remains untouched:

11 22 22 11

Interpreted either way, the integer is:

0x11222211

The executed probe confirmed:

palindrome raw=11 22 22 11
BE=0x11222211
LE=0x11222211

Such a test can falsely reassure a reviewer because the numeric output is invariant under reversal. It is useful only as a demonstration of test weakness, not as release evidence.

The asymmetric contract fields 78 56 34 12 and 04 03 02 01 remain the release witnesses.

This is also why I keep the scope away from streaming UTF-8 across chunk boundaries. Character decoder state has different failure modes. Here every accepted value is a fixed-width four-byte integer from a complete frame.

Build the evidence table for every view

The table below records the executed OpenJDK 21.0.12.1 probe. Rows with the same construction sequence are separate per-field observations.

Java build

Construction sequence

View type

Local index

Raw four bytes

Current order

Expected value (hex/dec)

Observed value (hex/dec)

Cursor/position

isReadOnly

Verdict

21.0.12.1+1 Debian

wrap → LE → pos(2)

ByteBuffer parent

2

78 56 34 12

LE

0x12345678 / 305419896

0x12345678 / 305419896

2

false

PASS

21.0.12.1+1 Debian

wrap → LE → pos(2)

ByteBuffer parent

6

04 03 02 01

LE

0x01020304 / 16909060

0x01020304 / 16909060

2

false

PASS

21.0.12.1+1 Debian

parent → slice

ByteBuffer

0

78 56 34 12

BE

0x12345678 / 305419896

0x78563412 / 2018915346

0

false

FAIL: order

21.0.12.1+1 Debian

parent → slice

ByteBuffer

4

04 03 02 01

BE

0x01020304 / 16909060

0x04030201 / 67305985

0

false

FAIL: order

21.0.12.1+1 Debian

parent → duplicate

ByteBuffer

2

78 56 34 12

BE

0x12345678 / 305419896

0x78563412 / 2018915346

2

false

FAIL: order

21.0.12.1+1 Debian

parent → duplicate

ByteBuffer

6

04 03 02 01

BE

0x01020304 / 16909060

0x04030201 / 67305985

2

false

FAIL: order

21.0.12.1+1 Debian

parent → asReadOnlyBuffer

ByteBuffer

2

78 56 34 12

BE

0x12345678 / 305419896

0x78563412 / 2018915346

2

true

FAIL: order

21.0.12.1+1 Debian

parent → asReadOnlyBuffer

ByteBuffer

6

04 03 02 01

BE

0x01020304 / 16909060

0x04030201 / 67305985

2

true

FAIL: order

21.0.12.1+1 Debian

parent → slice → LE → readOnly

ByteBuffer

0

78 56 34 12

BE

0x12345678 / 305419896

0x78563412 / 2018915346

0

true

FAIL: wrapper reset

21.0.12.1+1 Debian

parent → slice → readOnly → LE

ByteBuffer

0

78 56 34 12

LE

0x12345678 / 305419896

0x12345678 / 305419896

0

true

PASS

21.0.12.1+1 Debian

parent → slice → readOnly → LE

ByteBuffer

4

04 03 02 01

LE

0x01020304 / 16909060

0x01020304 / 16909060

0

true

PASS

21.0.12.1+1 Debian

slice(BE) → asIntBuffer

IntBuffer

0

78 56 34 12

BE

0x12345678 / 305419896

0x78563412 / 2018915346

0 elements

false

FAIL: captured BE

21.0.12.1+1 Debian

slice(BE) → asIntBuffer

IntBuffer

1

04 03 02 01

BE

0x01020304 / 16909060

0x04030201 / 67305985

0 elements

false

FAIL: captured BE

21.0.12.1+1 Debian

slice → LE → asIntBuffer

IntBuffer

0

78 56 34 12

LE

0x12345678 / 305419896

0x12345678 / 305419896

0 elements

false

PASS

21.0.12.1+1 Debian

slice → LE → asIntBuffer

IntBuffer

1

04 03 02 01

LE

0x01020304 / 16909060

0x01020304 / 16909060

0 elements

false

PASS

The Java SE 21 documented behaviors behind the table are: byte-buffer slice(), duplicate(), and asReadOnlyBuffer() create new byte views with BIG_ENDIAN; getInt(int) uses the byte view's current order; and asIntBuffer() captures source order when created.

Diagnose bytes, address and order independently

My triage sequence is deliberately compact:

  1. Capture the four raw bytes at the index actually used. If they do not equal the frame contract, investigate field addressing first; if the complete input itself differs, investigate acquisition/provenance outside this lab.

  2. Translate local view index to frame offset. A slice changes the local origin; duplicate and read-only byte views preserve the parent's index space at construction. Java documents those position/capacity differences explicitly.

  3. If the four bytes match, inspect ByteBuffer.order() on the final object. A matching four-byte window plus reversed numeric value is strong evidence of interpretation-order mismatch.

  4. For typed views, inspect both source order at construction and IntBuffer.order(). Do not assume a later change to the byte view reconfigures an existing typed view.

  5. Keep cursor movement out of the diagnosis. Prefer indexed reads so position remains observable and unchanged.

Repair the decoder factory

The smallest maintainable repair is not “make every buffer little-endian.” It is: apply the wire contract's declared order to the final ByteBuffer that performs multibyte reads or to the source immediately before constructing a typed view.

For a decoder that should expose a read-only payload:

static ByteBuffer payloadView(ByteBuffer frame) {
    frame.position(2).limit(10);

    return frame.slice()
            .asReadOnlyBuffer()
            .order(ByteOrder.LITTLE_ENDIAN);
}

The ordering of those calls is intentional.

This is unsafe:

return frame.slice()
        .order(ByteOrder.LITTLE_ENDIAN)
        .asReadOnlyBuffer();       // new byte view: BIG_ENDIAN

This is correct for the declared contract:

return frame.slice()
        .asReadOnlyBuffer()
        .order(ByteOrder.LITTLE_ENDIAN);

For an IntBuffer:

ByteBuffer payload = frame.slice()
        .asReadOnlyBuffer()
        .order(ByteOrder.LITTLE_ENDIAN);

IntBuffer fields = payload.asIntBuffer();

The typed view then captures the little-endian order at construction as specified by Java SE 21.

I would not replace that schema-specific declaration with:

.order(ByteOrder.nativeOrder())

nativeOrder() reports the running platform's hardware order; the frame contract, not the host, supplies the meaning of these bytes.

The code-review rule is therefore explicit:

Repair rule: after the last byte-buffer view-producing wrapper and before any getInt or asIntBuffer, set the final decoder-facing ByteBuffer to the order declared by the frame schema.

That rule is narrow enough to review mechanically.

Rerun every affected field

A one-field spot check is insufficient because the refactor may also have shifted an address or changed typed-view positioning.

I rerun these acceptance cases against the unchanged literal oracle:

  • parent Field A at byte index 2;

  • parent Field B at byte index 6;

  • slice Field A at local byte index 0;

  • slice Field B at local byte index 4;

  • final read-only payload at local indices 0 and 4;

  • IntBuffer elements 0 and 1;

  • the deliberately failing unconfigured slice;

  • the deliberately failing “configure then wrap read-only” case;

  • the palindrome only as a weak-test demonstration.

The observed passing final-view rows were:

slice readonly then LE idx=0 raw=78 56 34 12
order=LITTLE_ENDIAN
observed=0x12345678/305419896

slice readonly then LE idx=4 raw=04 03 02 01
order=LITTLE_ENDIAN
observed=0x01020304/16909060

IntBuffer after LE idx=0 order=LITTLE_ENDIAN
observed=0x12345678/305419896

IntBuffer after LE idx=1 order=LITTLE_ENDIAN
observed=0x01020304/16909060

PASS: all assertions completed

That is release evidence for the probe, not a blanket statement about every production entry point. A service may have several factory methods that construct fresh views after a common helper returns. Each wrapper entry point that can introduce slice(), duplicate(), asReadOnlyBuffer(), or typed-view construction must be represented in the regression fixture because those operations define interpretation boundaries under the API contract.

The original literal frame remains unchanged throughout. The negative palindrome uses its own four-byte array and therefore does not weaken the “one fixed ten-byte frame” evidence for the actual decoder cases.

Record a rollback trigger

For rollout, I would compare candidate and known-good decoder results on the same complete frames and preserve the raw field windows.

A rollback trigger is concrete:

If the candidate changes the decoded value of any documented field while the same frame, field address, and schema version are accepted by the known-good decoder, route that parser version back to the prior implementation and retain the frame bytes, local index, view-construction trace, observed order, expected value, actual value, and runtime build.

The trigger should not depend on whether the number merely “looks plausible.” 0x78563412 is a perfectly valid positive integer. The contract says it is wrong.

Rollback also does not settle the root cause by itself. If candidate and old decoder disagree because the frame version is unknown, the right decision is not automatically “old code wins”; that becomes a missing-contract investigation.

Choose whether to release the parser

The current evidence supports FIX VIEW CONSTRUCTION for the refactor as tested. We have a complete wire contract, the parent reads both fields correctly, raw field windows remain correct in the failing views, and the failure follows the documented Java SE 21 view-order behavior.

The decision matrix I would use is:

Evidence state

Release decision

Owner

Required action

Contract bytes, addresses and values all match after the final view is constructed

APPROVE DECODER

Decoder owner

Merge with regression fixture and runtime evidence

Raw bytes and local address match contract, but final byte/typed view has wrong order and numeric value

FIX VIEW CONSTRUCTION

Decoder/factory owner

Set schema order on final byte view or before typed-view creation; rerun all rows

Frame/version identifier is unknown or schema does not define field byte order

HOLD FOR MISSING WIRE CONTRACT

Interface/schema owner

Establish authoritative layout and order before altering decoder interpretation

Frame provenance is missing, so literal bytes cannot be tied to an accepted schema/version

HOLD FOR MISSING WIRE CONTRACT

Ingestion/interface owner

Recover provenance and version evidence; do not infer order from output plausibility

java-bytebuffer-img

For this concrete candidate, the release gate is not ambiguous: do not approve the uncorrected factory. Apply the final-view repair, rerun the fixture, and approve only the repaired construction path if all required rows pass.

The separate OpenJDK 21.0.13.1 observation and my independent OpenJDK 21.0.12.1 run agree on the view order. The Java SE 21 API contract defines the required behavior.

Hand the regression fixture to the interface owner

The fixture belongs beside the binary interface definition, not buried in an incidental decoder unit test.

I would store four things together:

binary-contract/
  frame-v1/
    contract.md
    cafe7856341204030201.hex
    ByteOrderProbe.java
    expected-results.md

contract.md should identify the schema or frame version, header CA FE, payload origin 2, little-endian field declaration, Field A offset/width/value, Field B offset/width/value, and the interface owner.

expected-results.md should capture the JDK build, command, pass/fail output, and the per-view evidence rows. If a CI environment later runs a different Java 21 maintenance build, record that separately rather than overwriting the provenance of this execution.

The factory regression should assert the object the production decoder actually receives:

ByteBuffer v = payloadView(
        ByteBuffer.wrap(FRAME)
                .order(ByteOrder.LITTLE_ENDIAN));

if (v.order() != ByteOrder.LITTLE_ENDIAN) {
    throw new AssertionError("decoder-facing order");
}

eq("field A", 0x12345678, v.getInt(0));
eq("field B", 0x01020304, v.getInt(4));

Review triggers include adding a read-only wrapper, inserting slice() or duplicate(), changing payload origin, converting to IntBuffer, or changing the schema version's declared byte order. Those are meaningful because the Java SE 21 APIs assign new state to the created views rather than making all interpretation properties implicitly transitive.

The broader discipline is the same one used when testing field survival across Protobuf relays: preserve a known artifact, define semantic expectations independently, and test the object after the transformation boundary rather than assuming an earlier valid state survives every wrapper.

Practice reviewing backend data contracts

This exercise is small enough to run from one source file, but the review habit scales: write the interface contract first, construct an independent oracle, hold acquisition constant, instrument the final representation seen by the consumer, and release from evidence rather than from a convenient intermediate state.

For readers building those backend integration-review skills through project work, Refonte Learning's Backend Developer Program currently lists a three-month format at 10–12 hours per week and includes backend work with Node.js/Express plus MongoDB and SQL. The official program page is the source to consult for current eligibility and curriculum details.

For the decoder reviewed here, the evidence is complete enough to act. The unchanged wire bytes select the right fields; the independent oracle supplies 0x12345678 and 0x01020304; the parent proves the declared little-endian interpretation; and the failing child views preserve the same bytes while reverting to BIG_ENDIAN exactly where Java SE 21 documents that behavior. The release disposition for the uncorrected refactor is therefore FIX VIEW CONSTRUCTION. Configure the final decoder-facing ByteBuffer to the frame's declared byte order before getInt, or configure the byte source before asIntBuffer() creates and captures its typed-view order; then rerun the complete regression fixture before approval.