Skip to content

Commit

Permalink
[Java] Preserve byte order throughout IR transformations.
Browse files Browse the repository at this point in the history
Previously, the IrDecoder looked at the message header's BEGIN_COMPOSITE
token to determine byte order; however, IrEncoder does not write the
schema's byte order on tokens like these. Now, the IrDecoder finds the
first real ENCODING token and reads the byte order from that.
  • Loading branch information
ZachBray committed Nov 27, 2024
1 parent 7079133 commit 8af808c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
11 changes: 10 additions & 1 deletion sbe-tool/src/main/java/uk/co/real_logic/sbe/ir/IrDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,16 @@ public Ir decode()
i = captureHeader(tokens);
}

final ByteOrder byteOrder = !tokens.isEmpty() ? tokens.get(0).encoding().byteOrder() : null;
ByteOrder byteOrder = null;
for (int j = 0; j < tokens.size(); j++)
{
if (tokens.get(j).signal() == Signal.ENCODING)
{
byteOrder = tokens.get(j).encoding().byteOrder();
break;
}
}

final Ir ir = new Ir(
irPackageName, irNamespaceName, irId, irVersion, null, semanticVersion, byteOrder, irHeader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.co.real_logic.sbe.ir;

import org.junit.jupiter.api.Test;
import uk.co.real_logic.sbe.PrimitiveType;
import uk.co.real_logic.sbe.xml.IrGenerator;
import uk.co.real_logic.sbe.xml.MessageSchema;
import uk.co.real_logic.sbe.xml.ParserOptions;
import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;

Expand Down Expand Up @@ -308,4 +310,30 @@ void shouldGenerateIrForMessageWithVariableLengthFieldWithEmbeddedLength() throw
assertThat(tokens.get(dataEncIdx).encoding().primitiveType(), is(PrimitiveType.CHAR));
}
}

@Test
void shouldRegenerateIrWithSameByteOrder() throws Exception
{
try (InputStream in = getLocalResource("example-bigendian-test-schema.xml"))
{
final MessageSchema schema = parse(in, ParserOptions.DEFAULT);
final IrGenerator generator = new IrGenerator();
final Ir firstIr = generator.generate(schema);
final ByteBuffer firstIrOutputBuffer = ByteBuffer.allocate(8 * 1024);

final int length;
try (IrEncoder firstIrEncoder = new IrEncoder(firstIrOutputBuffer, firstIr))
{
length = firstIrEncoder.encode();
}

final Ir secondIr;
try (IrDecoder firstIrDecoder = new IrDecoder(firstIrOutputBuffer.slice(0, length)))
{
secondIr = firstIrDecoder.decode();
}

assertThat(secondIr.byteOrder(), is(firstIr.byteOrder()));
}
}
}

0 comments on commit 8af808c

Please sign in to comment.