Skip to content

Commit 5641707

Browse files
committed
[Java] Fix issue with encoding IR containing char constants of length 1. Issue #469.
1 parent d80ec49 commit 5641707

File tree

4 files changed

+122
-12
lines changed

4 files changed

+122
-12
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/PrimitiveValue.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,19 @@ public class PrimitiveValue
3030
{
3131
public enum Representation
3232
{
33+
/**
34+
* Value is stored in a long value.
35+
*/
3336
LONG,
37+
38+
/**
39+
* Value is stored in a double value.
40+
*/
3441
DOUBLE,
42+
43+
/**
44+
* Value is stored in a byte[].
45+
*/
3546
BYTE_ARRAY
3647
}
3748

@@ -154,6 +165,16 @@ public PrimitiveValue(final byte[] value, final String characterEncoding, final
154165
this.size = size;
155166
}
156167

168+
/**
169+
* Get the {@link Representation} of the value.
170+
*
171+
* @return the {@link Representation} of the value.
172+
*/
173+
public Representation representation()
174+
{
175+
return representation;
176+
}
177+
157178
/**
158179
* Parse constant value string and set representation based on type
159180
*
@@ -274,7 +295,8 @@ public long longValue()
274295
{
275296
if (representation != Representation.LONG)
276297
{
277-
throw new IllegalStateException("PrimitiveValue is not a long representation");
298+
throw new IllegalStateException(
299+
"Not a long representation: representation=" + representation + " value=" + toString());
278300
}
279301

280302
return longValue;
@@ -290,7 +312,8 @@ public double doubleValue()
290312
{
291313
if (representation != Representation.DOUBLE)
292314
{
293-
throw new IllegalStateException("PrimitiveValue is not a double representation");
315+
throw new IllegalStateException(
316+
"Not a double representation: representation=" + representation + " value=" + toString());
294317
}
295318

296319
return doubleValue;
@@ -306,7 +329,8 @@ public byte[] byteArrayValue()
306329
{
307330
if (representation != Representation.BYTE_ARRAY)
308331
{
309-
throw new IllegalStateException("PrimitiveValue is not a byte[] representation");
332+
throw new IllegalStateException(
333+
"Not a byte[] representation: representation=" + representation + " value=" + toString());
310334
}
311335

312336
return byteArrayValue;

sbe-tool/src/main/java/uk/co/real_logic/sbe/ir/IrUtil.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,14 @@ public static int put(final MutableDirectBuffer buffer, final PrimitiveValue val
273273
case CHAR:
274274
if (value.size() == 1)
275275
{
276-
buffer.putByte(0, (byte)value.longValue());
276+
if (value.representation() == PrimitiveValue.Representation.LONG)
277+
{
278+
buffer.putByte(0, (byte)value.longValue());
279+
}
280+
else
281+
{
282+
buffer.putByte(0, value.byteArrayValue()[0]);
283+
}
277284
return 1;
278285
}
279286
else

sbe-tool/src/test/java/uk/co/real_logic/sbe/generation/java/GenerateFixBinaryTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@
2323
import uk.co.real_logic.sbe.SbeTool;
2424
import uk.co.real_logic.sbe.TestUtil;
2525
import uk.co.real_logic.sbe.ir.Ir;
26+
import uk.co.real_logic.sbe.ir.IrDecoder;
27+
import uk.co.real_logic.sbe.ir.IrEncoder;
2628
import uk.co.real_logic.sbe.xml.IrGenerator;
2729
import uk.co.real_logic.sbe.xml.MessageSchema;
2830
import uk.co.real_logic.sbe.xml.ParserOptions;
2931

32+
import java.nio.ByteBuffer;
3033
import java.util.Map;
3134

35+
import static org.junit.Assert.assertEquals;
3236
import static org.junit.Assert.assertNotNull;
3337
import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse;
3438

@@ -66,4 +70,31 @@ public void shouldGenerateValidJava() throws Exception
6670

6771
assertNotNull(aClass);
6872
}
73+
74+
@Test
75+
public void shouldGenerateAndEncodeIr() throws Exception
76+
{
77+
System.setProperty(SbeTool.KEYWORD_APPEND_TOKEN, "_");
78+
79+
final ParserOptions options = ParserOptions.builder().stopOnError(true).build();
80+
final MessageSchema schema = parse(TestUtil.getLocalResource("FixBinary.xml"), options);
81+
final IrGenerator irg = new IrGenerator();
82+
final Ir ir = irg.generate(schema);
83+
final ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
84+
85+
final IrEncoder irEncoder = new IrEncoder(buffer, ir);
86+
irEncoder.encode();
87+
88+
buffer.flip();
89+
final IrDecoder irDecoder = new IrDecoder(buffer);
90+
final Ir decodedIr = irDecoder.decode();
91+
92+
assertEquals(ir.id(), decodedIr.id());
93+
assertEquals(ir.version(), decodedIr.version());
94+
assertEquals(ir.byteOrder(), decodedIr.byteOrder());
95+
assertEquals(ir.applicableNamespace(), decodedIr.applicableNamespace());
96+
assertEquals(ir.packageName(), decodedIr.packageName());
97+
assertEquals(ir.types().size(), decodedIr.types().size());
98+
assertEquals(ir.messages().size(), decodedIr.messages().size());
99+
}
69100
}

0 commit comments

Comments
 (0)