Skip to content

Commit d919f5b

Browse files
committed
added auxiliary method makeStrWriter into JBBPTextWriter
1 parent 6d3dd8c commit d919f5b

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

jbbp/src/main/java/com/igormaznitsa/jbbp/utils/JBBPTextWriter.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,17 @@ public JBBPTextWriter(
321321
Radix(radix);
322322
}
323323

324+
/**
325+
* Auxiliary method allows to build writer over StringWriter with system-depended next line and hex radix.
326+
* The Method allows fast instance create.
327+
*
328+
* @since 1.4.0
329+
*/
330+
public static JBBPTextWriter makeStrWriter() {
331+
final String lineSeparator = System.setProperty("line.separator", "\n");
332+
return new JBBPTextWriter(new StringWriter(), JBBPByteOrder.BIG_ENDIAN, lineSeparator, 16, "0x", ".", ";", "~", ",");
333+
}
334+
324335
protected static String makeFieldComment(final JBBPAbstractField field) {
325336
final String path = field.getFieldPath();
326337
final StringBuilder result = new StringBuilder(128);
@@ -1421,10 +1432,10 @@ protected void printAbstractFieldObject(final String postText, final JBBPAbstrac
14211432
}
14221433
Comment(" " + makeFieldComment(field) + postfix);
14231434
} else if (field instanceof JBBPFieldString) {
1424-
final String value = ((JBBPFieldString)field).getAsString();
1425-
Str(value == null ? "<NULL>" : '\"'+value+'\"');
1435+
final String value = ((JBBPFieldString) field).getAsString();
1436+
Str(value == null ? "<NULL>" : '\"' + value + '\"');
14261437
Comment(" " + makeFieldComment(field) + postfix);
1427-
} else {
1438+
} else {
14281439
throw new Error("Unexpected field [" + field.getClass() + ']');
14291440
}
14301441
}
@@ -1898,7 +1909,7 @@ protected void onFieldString(final Object obj, final Field field, final Bin anno
18981909
ensureValueMode();
18991910
final String prefix = prefixValue;
19001911
prefixValue = "";
1901-
printValueString(value == null ? "<NULL>" : '\"'+value+'\"');
1912+
printValueString(value == null ? "<NULL>" : '\"' + value + '\"');
19021913
prefixValue = prefix;
19031914
if (this.arrayCounter == 0) {
19041915
Comment(makeFieldDescription(field, annotation));

jbbp/src/test/java/com/igormaznitsa/jbbp/utils/JBBPTextWriterTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ private static JBBPTextWriter makeWriter() {
4141
return new JBBPTextWriter(new StringWriter(), JBBPByteOrder.BIG_ENDIAN, "\n", 16, "0x", ".", ";", "~", ",");
4242
}
4343

44+
@Test
45+
public void testMakeStrWriter() throws Exception {
46+
final String generated = JBBPTextWriter.makeStrWriter().Int(12).Byte(34).BR().Comment("Huzzaaa").Close().toString();
47+
assertEquals(String.format(".0x0000000C,0x22%n;Huzzaaa"),generated);
48+
}
49+
4450
@Test
4551
public void testConstructor_Default() {
4652
final JBBPTextWriter writer = new JBBPTextWriter();
@@ -729,7 +735,7 @@ class SomeClass {
729735
@Bin(outOrder = 6, comment = "some string")
730736
String str = "Hello String";
731737
@Bin(outOrder = 7, comment = "some string array")
732-
String [] strs = new String[] {"Hello", null, "World"};
738+
String[] strs = new String[] {"Hello", null, "World"};
733739
}
734740

735741
final SomeClass cl = new SomeClass();
@@ -860,6 +866,19 @@ public void testBin_AllEasyTypes_NonMappedRawStruct() throws Exception {
860866
assertFile("txtwrtrjbbpobj1.txt", text);
861867
}
862868

869+
@Test
870+
public void testBin_ValField() throws Exception {
871+
final JBBPParser parser = JBBPParser.prepare("val:123 a;");
872+
final String text = makeWriter().SetMaxValuesPerLine(16).Bin(parser.parse(new byte[0])).Close().toString();
873+
assertEquals("~--------------------------------------------------------------------------------\n" +
874+
"; Start {} \n" +
875+
"~--------------------------------------------------------------------------------\n" +
876+
" .0x0000007B; int a\n" +
877+
"~--------------------------------------------------------------------------------\n" +
878+
"; End {} \n" +
879+
"~--------------------------------------------------------------------------------\n", text);
880+
}
881+
863882
@Test
864883
public void testBin_AllEasyTypes_Anonymous_NonMappedRawStruct() throws Exception {
865884
final JBBPParser parser = JBBPParser.prepare("bit:2; bit:6; byte; ubyte; short; ushort; int; long; bool; stringj;");

0 commit comments

Comments
 (0)