Skip to content

[Java] Add new method to decoders to pass in Appendable to write ascii encoded strings to #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,37 @@ private void generateDataDecodeMethods(
sizeOfLengthField,
generateGet(lengthType, "limit", byteOrderStr),
characterEncoding));

if (characterEncoding.contains("ASCII"))
{
sb.append(String.format("\n" +
indent + " public void get%1$s(final Appendable value)\n" +
indent + " {\n" +
"%2$s" +
indent + " final int headerLength = %3$d;\n" +
indent + " final int limit = parentMessage.limit();\n" +
indent + " final int dataLength = (int)%4$s;\n" +
indent + " final int dataOffset = limit + headerLength;\n" +
indent + " parentMessage.limit(dataOffset + dataLength);\n" +
indent + " for (int i = 0; i < dataLength; ++i)\n" +
indent + " {\n" +
indent + " try\n" +
indent + " {\n" +
indent + " final int c = buffer.getByte(dataOffset + i) & 0xFF;\n" +
indent + " value.append(c > 127 ? '?' : (char)c);\n" +
indent + " }\n" +
indent + " catch (final java.io.IOException e)\n" +
indent + " {\n" +
indent + " throw new java.io.UncheckedIOException(e);\n" +
indent + " }\n" +
indent + " }\n" +
indent + " }\n",
Generators.toUpperFirstChar(propertyName),
generateStringNotPresentCondition(token.version(), indent),
sizeOfLengthField,
generateGet(lengthType, "limit", byteOrderStr),
byteOrderStr));
}
}
}

Expand Down Expand Up @@ -1813,6 +1844,34 @@ private CharSequence generatePrimitiveArrayPropertyDecode(
fieldLength, offset,
fieldLength, fieldLength,
charset(encoding.characterEncoding())));

if (encoding.characterEncoding().contains("ASCII"))
{
sb.append(String.format("\n" +
indent + " public void get%s(final Appendable value)\n" +
indent + " {\n" +
"%s" +
indent + " for (int i = 0; i < %d ; ++i)\n" +
indent + " {\n" +
indent + " final int c = buffer.getByte(this.offset + %d + i) & 0xFF;\n" +
indent + " if (c == 0)\n" +
indent + " {\n" +
indent + " break;\n" +
indent + " }\n" +
indent + " try\n" +
indent + " {\n" +
indent + " value.append(c > 127 ? '?' : (char)c);\n" +
indent + " }\n" +
indent + " catch (final java.io.IOException e)\n" +
indent + " {\n" +
indent + " throw new java.io.UncheckedIOException(e);\n" +
indent + " }\n" +
indent + " }\n" +
indent + " }\n\n",
Generators.toUpperFirstChar(propertyName),
generateStringNotPresentCondition(propertyToken.version(), indent),
fieldLength, offset));
}
}

return sb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,55 @@ public void shouldGenerateGetString() throws Exception
assertThat(get(decoder, "vehicleCode"), is("R11R12"));
}

@Test
public void shouldGenerateGetFixedLengthStringUsingAppendable() throws Exception
{
final UnsafeBuffer buffer = new UnsafeBuffer(new byte[4096]);
final StringBuilder result = new StringBuilder();
generator().generate();

final Object encoder = wrap(buffer, compileCarEncoder().newInstance());
final Object decoder = getCarDecoder(buffer, encoder);

set(encoder, "vehicleCode", String.class, "R11");
get(decoder, "vehicleCode", result);
assertThat(result.toString(), is("R11"));

result.setLength(0);
set(encoder, "vehicleCode", String.class, "");
get(decoder, "vehicleCode", result);
assertThat(result.toString(), is(""));

result.setLength(0);
set(encoder, "vehicleCode", String.class, "R11R12");
get(decoder, "vehicleCode", result);
assertThat(result.toString(), is("R11R12"));
}

@Test
public void shouldGenerateGetVariableStringUsingAppendable() throws Exception
{
final UnsafeBuffer buffer = new UnsafeBuffer(new byte[4096]);
final StringBuilder result = new StringBuilder();
generator().generate();

final Object encoder = wrap(buffer, compileCarEncoder().newInstance());
final Object decoder = getCarDecoder(buffer, encoder);
set(encoder, "color", String.class, "Red");
get(decoder, "color", result);
assertThat(result.toString(), is("Red"));

result.setLength(0);
set(encoder, "color", String.class, "");
get(decoder, "color", result);
assertThat(result.toString(), is(""));

result.setLength(0);
set(encoder, "color", String.class, "Red and Blue");
get(decoder, "color", result);
assertThat(result.toString(), is("Red and Blue"));
}

@Test
public void shouldGeneratePutCharSequence() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package uk.co.real_logic.sbe.generation.java;

import uk.co.real_logic.sbe.generation.Generators;

public final class ReflectionUtil
{
static int getSbeSchemaVersion(final Object encoder) throws Exception
Expand All @@ -37,6 +39,12 @@ static Object get(final Object object, final String fieldName) throws Exception
return object.getClass().getMethod(fieldName).invoke(object);
}

static void get(final Object object, final String fieldName, final Appendable arg) throws Exception
{
final String methodName = "get" + Generators.toUpperFirstChar(fieldName);
object.getClass().getMethod(methodName, Appendable.class).invoke(object, arg);
}

static String getManufacturer(final Object decoder) throws Exception
{
return (String)get(decoder, "manufacturer");
Expand Down
5 changes: 5 additions & 0 deletions sbe-tool/src/test/resources/code-generation-schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<type name="length" primitiveType="uint16"/>
<type name="varData" primitiveType="uint8" length="0" characterEncoding="UTF-8"/>
</composite>
<composite name="varAsciiStringEncoding">
<type name="length" primitiveType="uint32" maxValue="1073741824"/>
<type name="varData" primitiveType="uint8" length="0" characterEncoding="ASCII"/>
</composite>
</types>
<types>
<type name="ModelYear" primitiveType="uint16"/>
Expand Down Expand Up @@ -84,5 +88,6 @@
<data name="manufacturer" id="18" type="varStringEncoding"/>
<data name="model" id="19" type="varStringEncoding"/>
<data name="activationCode" id="20" type="varStringEncoding"/>
<data name="color" id="21" type="varAsciiStringEncoding"/>
</sbe:message>
</sbe:messageSchema>