Skip to content

Commit a6c04fd

Browse files
committed
[Java] Allow a length to be set that is greater than provided constant and NUL pad remainder. Issue aeron-io#463.
1 parent 30f0265 commit a6c04fd

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,18 @@ public static PrimitiveValue parse(
250250
public static PrimitiveValue parse(
251251
final String value, final int length, final String characterEncoding)
252252
{
253-
if (value.length() != length)
253+
if (value.length() > length)
254254
{
255-
throw new IllegalStateException("value.length=" + value.length() + " not equal to length=" + length);
255+
throw new IllegalStateException("value.length=" + value.length() + " greater than length=" + length);
256256
}
257257

258-
return new PrimitiveValue(value.getBytes(forName(characterEncoding)), characterEncoding, length);
258+
byte[] bytes = value.getBytes(forName(characterEncoding));
259+
if (bytes.length < length)
260+
{
261+
bytes = Arrays.copyOf(bytes, length);
262+
}
263+
264+
return new PrimitiveValue(bytes, characterEncoding, length);
259265
}
260266

261267
/**

sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/EncodedDataType.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public EncodedDataType(final Node node, final String givenName)
6464
super(node, givenName, null);
6565

6666
primitiveType = PrimitiveType.get(getAttributeValue(node, "primitiveType"));
67-
length = Integer.parseInt(getAttributeValue(node, "length", "1"));
67+
final String lengthAttr = getAttributeValueOrNull(node, "length");
68+
length = Integer.parseInt(null == lengthAttr ? "1" : lengthAttr);
6869
varLen = Boolean.parseBoolean(getAttributeValue(node, "variableLength", "false"));
6970

7071
if (PrimitiveType.CHAR == primitiveType)
@@ -89,13 +90,34 @@ public EncodedDataType(final Node node, final String givenName)
8990
final String nodeValue = node.getFirstChild().getNodeValue();
9091
if (PrimitiveType.CHAR == primitiveType)
9192
{
92-
if (nodeValue.length() == 1)
93+
final int valueLength = nodeValue.length();
94+
95+
if (null != lengthAttr && length < valueLength)
96+
{
97+
handleError(node, "length of " + length + " is less than provided value: " + nodeValue);
98+
}
99+
100+
if (valueLength == 1)
93101
{
94-
constValue = PrimitiveValue.parse(nodeValue, primitiveType, characterEncoding);
102+
if (null == lengthAttr)
103+
{
104+
constValue = PrimitiveValue.parse(nodeValue, primitiveType, characterEncoding);
105+
}
106+
else
107+
{
108+
constValue = PrimitiveValue.parse(nodeValue, length, characterEncoding);
109+
}
95110
}
96111
else
97112
{
98-
constValue = PrimitiveValue.parse(nodeValue, nodeValue.length(), characterEncoding);
113+
if (null == lengthAttr)
114+
{
115+
constValue = PrimitiveValue.parse(nodeValue, valueLength, characterEncoding);
116+
}
117+
else
118+
{
119+
constValue = PrimitiveValue.parse(nodeValue, length, characterEncoding);
120+
}
99121
}
100122
}
101123
else

0 commit comments

Comments
 (0)