com.igormaznitsa.jbbp.JBBPParser Java Examples
The following examples show how to use
com.igormaznitsa.jbbp.JBBPParser.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JBBPToJavaConverterReadWriteTest.java From java-binary-block-parser with Apache License 2.0 | 6 votes |
@Test public void testReaWrite_StructMappedToInterface_NotArray_GettersSettersOn() throws Exception { final JBBPParser parser = JBBPParser.prepare("z { x { y { byte a;}}}"); final Map<String, String> interfaceMap = new HashMap<>(); interfaceMap.put("z.x.y", ByteTestInterface.class.getCanonicalName()); final String text = JBBPToJavaConverter.makeBuilder(parser).setMainClassName(CLASS_NAME).setMainClassPackage(PACKAGE_NAME).setAddGettersSetters(true).setMapSubClassesInterfaces(interfaceMap).build().convert(); final String fullClassName = PACKAGE_NAME + '.' + CLASS_NAME; final ClassLoader classLoader = saveAndCompile(new JavaClassContent(fullClassName, text)); final Object instance = ReflectUtils.newInstance(classLoader.loadClass(fullClassName)); callRead(instance, new byte[] {42}); final ByteTestInterface data = getFieldThroughGetters(instance, "z.x.y", ByteTestInterface.class); assertEquals(42, data.getA()); assertArrayEquals(new byte[] {42}, callWrite(instance)); }
Example #2
Source File: GenAnnotationsTest.java From java-binary-block-parser with Apache License 2.0 | 6 votes |
@Test void testReadWrite() throws IOException { final byte[] testData = new byte[] {4, (byte) 0x12, (byte) 0x34, 3, 5, 6, 7}; final GenAnnotations result = new GenAnnotations().read(new JBBPBitInputStream(new ByteArrayInputStream(testData))); assertEquals(4, result.getLEN()); assertEquals(3, result.getSOME1().getSOME2().getFIELD().length); final String script = "ubyte len;" + "some1 {" + " bit:4 [len] someField;" + " ubyte len;" + " some2 {" + " byte [len] field;" + " }" + "}"; final GenAnnotations instance = JBBPParser.prepare(script).parse(testData).mapTo(new GenAnnotations()); assertEquals(result.getLEN(), instance.getLEN()); assertEquals(result.getSOME1().getLEN(), instance.getSOME1().getLEN()); assertArrayEquals(result.getSOME1().getSOMEFIELD(), instance.getSOME1().getSOMEFIELD()); assertArrayEquals(result.getSOME1().getSOME2().getFIELD(), instance.getSOME1().getSOME2().getFIELD()); }
Example #3
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 6 votes |
@Test void testMapSubstructToSuperclassesInterface() throws Exception { final JBBPParser parser = JBBPParser.prepare("a { b { c [_] { byte d;}} }"); final String text = JBBPToJavaConverter.makeBuilder(parser) .setMainClassName(CLASS_NAME) .setAddGettersSetters(true) .setMapSubClassesSuperclasses( makeMap( "a.b", "com.igormaznitsa.Impl", "a.b.c", "com.igormaznitsa.Impl2" ) ) .build() .convert(); assertTrue(text.contains("public static class B extends com.igormaznitsa.Impl")); assertTrue(text.contains("public static class C extends com.igormaznitsa.Impl2")); assertTrue(text.contains("public B getB() { return this.b;}")); assertTrue(text.contains("public C [] getC() { return this.c;}")); System.out.println(text); }
Example #4
Source File: BasedOnQuestionsAndCasesTest.java From java-binary-block-parser with Apache License 2.0 | 6 votes |
/** * Case 13-aug-2015 * <p> * 3DF8 = 0011 1101 1111 1000 where data are stored from left to right : * 6 bits : 0011 11 for year; * 4 bits : 01 11 for month * 5 bits : 11 100 for day, * * @throws Exception for any error */ @Test public void testParseDayMonthYearFromBytePairInMSB0AndPackThemBack() throws Exception { class YearMonthDay { @Bin(type = BinType.BIT, bitNumber = JBBPBitNumber.BITS_6, order = 1, bitOrder = JBBPBitOrder.MSB0) byte year; @Bin(type = BinType.BIT, bitNumber = JBBPBitNumber.BITS_4, order = 2, bitOrder = JBBPBitOrder.MSB0) byte month; @Bin(type = BinType.BIT, bitNumber = JBBPBitNumber.BITS_5, order = 3, bitOrder = JBBPBitOrder.MSB0) byte day; } final YearMonthDay parsed = JBBPParser.prepare("bit:6 year; bit:4 month; bit:5 day;", JBBPBitOrder.MSB0).parse(new byte[] {(byte) 0x3D, (byte) 0xF8}).mapTo(new YearMonthDay()); assertEquals(0x0F, parsed.year); assertEquals(0x07, parsed.month); assertEquals(0x1C, parsed.day & 0xFF); assertArrayEquals(new byte[] {(byte) 0x3D, (byte) 0xF8}, JBBPOut.BeginBin(JBBPBitOrder.MSB0).Bin(parsed).End().toByteArray()); }
Example #5
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 6 votes |
@Test void testMap_ParsedMarkedTransientField() throws Exception { @Bin class Parsed { @Bin(path = "struct.a") byte num; @Bin(path = "struct.b", type = BinType.BYTE_ARRAY) String str; @Bin(path = "struct.c", type = BinType.BYTE_ARRAY) transient String trans; } final Parsed parsed = JBBPParser.prepare("int start; struct { byte a; byte [3] b; byte [3] c; } byte end;").parse(new byte[] {1, 2, 3, 4, 5, (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', 9}).mapTo(new Parsed()); assertEquals(0x05, parsed.num); assertEquals("abc", parsed.str); assertEquals("def", parsed.trans); }
Example #6
Source File: ConvertToJSONTest.java From java-binary-block-parser with Apache License 2.0 | 6 votes |
@Test public void testConvertToJSON() throws Exception { try (InputStream pngStream = getResourceAsInputStream("picture.png")) { final JBBPParser pngParser = JBBPParser.prepare( "long header;" + "// chunks\n" + "chunk [_]{" + " int length; " + " int type; " + " byte[length] data; " + " int crc;" + "}" ); final JSONObject json = convertToJSon(null, pngParser.parse(pngStream)); final String jsonText = json.toJSONString(JSONStyle.MAX_COMPRESS); assertTrue(jsonText.length() == 13917); assertTrue(jsonText.contains("header:")); assertTrue(jsonText.contains("chunk:{")); assertTrue(jsonText.contains("length:")); assertTrue(jsonText.contains("type:")); assertTrue(jsonText.contains("data:")); assertTrue(jsonText.contains("crc:")); } }
Example #7
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 6 votes |
@Test void testMap_customMappingFields_Class() throws Exception { final class Mapped { @Bin int a; @Bin(custom = true, paramExpr = "TEST_TEXT") String b; @Bin int c; } final Mapped mapped = JBBPParser.prepare("int a; int b; int c;").parse(new byte[] {1, 2, 3, 4, 0x4A, 0x46, 0x49, 0x46, 5, 6, 7, 8}).mapTo(new Mapped(), (parsedBlock, annotation, field) -> { if ("b".equals(field.getName()) && "TEST_TEXT".equals(annotation.paramExpr())) { final int bvalue = parsedBlock.findFieldForNameAndType("b", JBBPFieldInt.class).getAsInt(); return String.valueOf((char) ((bvalue >>> 24) & 0xFF)) + (char) ((bvalue >>> 16) & 0xFF) + (char) ((bvalue >>> 8) & 0xFF) + (char) (bvalue & 0xFF); } else { fail("Unexpected state" + field); return null; } }); assertEquals(0x01020304, mapped.a); assertEquals("JFIF", mapped.b); assertEquals(0x05060708, mapped.c); }
Example #8
Source File: JBBPTextWriterTest.java From java-binary-block-parser with Apache License 2.0 | 6 votes |
@Test public void testBin_ParsedPng_NonMappedRawStruct() throws Exception { final InputStream pngStream = getResourceAsInputStream("picture.png"); try { final JBBPParser pngParser = JBBPParser.prepare( "long header;" + "// chunks\n" + "chunks [_]{" + " int length; " + " int type; " + " byte[length] data; " + " int crc;" + "}" ); final String text = makeWriter().SetMaxValuesPerLine(16).Bin(pngParser.parse(pngStream)).Close().toString(); System.out.println(text); assertFileContent("testwriterbin2b.txt", text); } finally { JBBPUtils.closeQuietly(pngStream); } }
Example #9
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 6 votes |
private static String makeSources( final JBBPParser parser, final String classComment, final boolean useSetterGetter, final boolean addBinAnnotations, boolean nonStaticInnerClasses) { final JBBPToJavaConverter.Builder result = JBBPToJavaConverter.makeBuilder(parser) .setMainClassPackage(PACKAGE_NAME) .setMainClassName(CLASS_NAME) .setHeadComment(classComment) .setAddGettersSetters(useSetterGetter); if (nonStaticInnerClasses) { result.doInternalClassesNonStatic(); } if (addBinAnnotations) { result.addBinAnnotations().addNewInstanceMethods(); } return result.build() .convert(); }
Example #10
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_String() throws Exception { class Mapped { @Bin String a; @Bin String b; } final Mapped mapped = JBBPParser.prepare("stringj a; stringj b;").parse(new byte[] {3, 65, 66, 67, 2, 68, 69}).mapTo(new Mapped()); assertEquals("ABC", mapped.a); assertEquals("DE", mapped.b); }
Example #11
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_FieldWithDefinedBitNumberToBitField_FieldPresentedWithDifferentBitNumber() throws Exception { class Parsed { @Bin(bitNumber = JBBPBitNumber.BITS_5) byte field; } assertThrows(JBBPMapperException.class, () -> JBBPParser.prepare("int fieldint; bit:6 field;").parse(new byte[] {1, 2, 3, 4, 0x35}).mapTo(new Parsed())); }
Example #12
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_MapIntArrayToFloatArray() throws Exception { class Mapped { @Bin(type = BinType.INT_ARRAY) float[] a; } final byte[] max = JBBPOut.BeginBin().Float(Float.MAX_VALUE, Float.MIN_VALUE).End().toByteArray(); final Mapped result = JBBPParser.prepare("int [_] a;").parse(max).mapTo(new Mapped()); assertEquals(2, result.a.length); assertEquals(Float.MAX_VALUE, result.a[0], TestUtils.FLOAT_DELTA); assertEquals(Float.MIN_VALUE, result.a[1], TestUtils.FLOAT_DELTA); }
Example #13
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_MapIntToFloat() throws Exception { class Mapped { @Bin(type = BinType.INT) float a; } final byte[] max = JBBPOut.BeginBin().Int(Float.floatToIntBits(Float.MAX_VALUE)).End().toByteArray(); assertEquals(Float.MAX_VALUE, JBBPParser.prepare("int a;").parse(max).mapTo(new Mapped()).a, 0.005d); final byte[] min = JBBPOut.BeginBin().Int(Float.floatToIntBits(Float.MIN_VALUE)).End().toByteArray(); assertEquals(Float.MIN_VALUE, JBBPParser.prepare("int a;").parse(min).mapTo(new Mapped()).a, 0.005d); }
Example #14
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_Int() throws Exception { class Mapped { @Bin int a; } assertEquals(0x01020304, JBBPParser.prepare("int a;").parse(new byte[] {1, 2, 3, 4}).mapTo(new Mapped()).a); }
Example #15
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_Bit() throws Exception { class Mapped { @Bin(type = BinType.BIT) byte a; @Bin(type = BinType.BIT) byte b; @Bin(type = BinType.BIT) byte c; } final Mapped mapped = JBBPParser.prepare("bit:3 a; bit:2 b; bit:3 c; ").parse(new byte[] {(byte) 0xDD}).mapTo(new Mapped()); assertEquals(5, mapped.a); assertEquals(3, mapped.b); assertEquals(6, mapped.c); }
Example #16
Source File: JBBPToJavaConverterReadWriteTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test public void testRead_ExpressionResult_OnlyField_NegativeResultAsZero() throws Exception { final Object instance = compileAndMakeInstance("byte len; byte [len] arr;", JBBPParser.FLAG_NEGATIVE_EXPRESSION_RESULT_AS_ZERO); final byte[] etalon = new byte[] {(byte) 0xFE, 1, 2, 3, 4}; callRead(instance, etalon.clone()); assertEquals((byte) 0xFE, getField(instance, "len", Byte.class).byteValue()); assertEquals(0, getField(instance, "arr", byte[].class).length); }
Example #17
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_customMappingFields_ClassInstance() throws Exception { final class Mapped { @Bin int a; @Bin(custom = true, paramExpr = "TEST_TEXT") String b; @Bin int c; } final Mapped mapped = new Mapped(); final Mapped result = JBBPParser.prepare("int a; int b; int c;").parse(new byte[] {1, 2, 3, 4, 0x4A, 0x46, 0x49, 0x46, 5, 6, 7, 8}).mapTo(mapped, (parsedBlock, annotation, field) -> { if ("b".equals(field.getName()) && "TEST_TEXT".equals(annotation.paramExpr())) { final int bvalue = parsedBlock.findFieldForNameAndType("b", JBBPFieldInt.class).getAsInt(); return String.valueOf((char) ((bvalue >>> 24) & 0xFF)) + (char) ((bvalue >>> 16) & 0xFF) + (char) ((bvalue >>> 8) & 0xFF) + (char) (bvalue & 0xFF); } else { fail("Unexpected state" + field); return null; } }); assertSame(mapped, result); assertEquals(0x01020304, mapped.a); assertEquals("JFIF", mapped.b); assertEquals(0x05060708, mapped.c); }
Example #18
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_MapLongArrayToDoubleArray() throws Exception { class Mapped { @Bin(type = BinType.LONG_ARRAY) double[] a; } final byte[] max = JBBPOut.BeginBin().Double(Double.MAX_VALUE, Double.MIN_VALUE).End().toByteArray(); final Mapped result = JBBPParser.prepare("long [_] a;").parse(max).mapTo(new Mapped()); assertEquals(2, result.a.length); assertEquals(Double.MAX_VALUE, result.a[0], TestUtils.FLOAT_DELTA); assertEquals(Double.MIN_VALUE, result.a[1], TestUtils.FLOAT_DELTA); }
Example #19
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_InsideStructAndClass() throws Exception { class Mapped { @Bin byte a; } assertEquals(3, JBBPParser.prepare("byte a; some{struc {byte a;}}").parse(new byte[] {1, 3}).mapTo("some.struc", new Mapped()).a); }
Example #20
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_IgnoreMarkedFieldByDefaultIfTransient() throws Exception { @Bin class Parsed { @Bin(path = "struct.a") byte num; @Bin(path = "struct.b", type = BinType.BYTE_ARRAY) String str; transient String ignored; } final Parsed parsed = JBBPParser.prepare("int start; struct { byte a; byte [3] b; } int end;").parse(new byte[] {1, 2, 3, 4, 5, (byte) 'a', (byte) 'b', (byte) 'c', 6, 7, 8, 9}).mapTo(new Parsed()); assertEquals(0x05, parsed.num); assertEquals("abc", parsed.str); }
Example #21
Source File: CommandFlasher.java From Flashtool with GNU General Public License v3.0 | 5 votes |
public void getUfsInfo() throws IOException,X10FlashException { logger.info("Sending Get-ufs-info"); String command = "Get-ufs-info"; USBFlash.write(command.getBytes()); CommandPacket reply = USBFlash.readCommandReply(true); logger.info(" Get-ufs-info status : "+reply.getResponse()); JBBPParser ufs_parser = JBBPParser.prepare( "byte headerlen;" + "byte[headerlen-1] ufs_header;" + "luns [_] { " + " byte lunlength; " + " byte reserved1; " + " byte lunid; " + " byte[12] reserved2; " + " int length; " + " byte[lunlength-19] lundata; " + "}" ); try { System.out.println(HexDump.toHex(reply.getDataArray())); JBBPBitInputStream stream = new JBBPBitInputStream(new ByteArrayInputStream(reply.getDataArray())); ufs_infos = ufs_parser.parse(stream).mapTo(new UfsInfos()); ufs_infos.setSectorSize(Integer.parseInt(getPhoneProperty("Sector-size"))); try { stream.close(); } catch (Exception streamclose ) {} } catch (Exception e) { System.out.println(e.getMessage()); ufs_infos=null; } }
Example #22
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_MapDoubleToDouble() throws Exception { class Mapped { @Bin double a; } final byte[] max = JBBPOut.BeginBin().Double(-1.2345678912345d).End().toByteArray(); assertEquals(-1.2345678912345d, JBBPParser.prepare("doublej a;").parse(max).mapTo(new Mapped()).a, TestUtils.FLOAT_DELTA); }
Example #23
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_MapDoubleArrayToDoubleArray() throws Exception { class Mapped { @Bin double[] a; } final byte[] max = JBBPOut.BeginBin().Double(-1.2345678912345d, 45.3334d).End().toByteArray(); assertArrayEquals(new double[] {-1.2345678912345d, 45.3334d}, JBBPParser.prepare("doublej [_] a;").parse(max).mapTo(new Mapped()).a, TestUtils.FLOAT_DELTA); }
Example #24
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMakeNewInstanceInLocalStaticClasses() throws Exception { final StaticTop result = JBBPParser.prepare("levelOne { levelTwos[_]{byte a;}}").parse(new byte[] {1, 2, 3}).mapTo(new StaticTop()); assertNotNull(result.levelOne); assertNotNull(result.levelOne.levelTwos); assertEquals(3, result.levelOne.levelTwos.length); }
Example #25
Source File: Bin2Json.java From openhab-core with Eclipse Public License 2.0 | 5 votes |
/** * Constructor. * * @param parserRule Binary data parser rule. * @throws ConversionException if parse rule parsing fails. */ public Bin2Json(String parserRule) throws ConversionException { try { parser = JBBPParser.prepare(parserRule); } catch (JBBPException e) { throw new ConversionException(String.format("Illegal parser rule, reason: %s", e.getMessage(), e)); } }
Example #26
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_ErrorForMappingStructureToPrimitiveField() { class Mapped { @Bin(name = "test", type = BinType.STRUCT) long a; } assertThrows(JBBPMapperException.class, () -> JBBPParser.prepare("test { byte [_] a;}").parse(new byte[] {1, 2, 3, 4}).mapTo(new Mapped())); }
Example #27
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_MapElementsByTheirPaths_ErrorForFieldIncompatibleType() { class Parsed { @Bin(path = "struct.a") byte num; @Bin(path = "struct.b", type = BinType.UBYTE_ARRAY) String str; } assertThrows(JBBPMapperException.class, () -> JBBPParser.prepare("int start; struct { byte a; byte [3] b; } int end;").parse(new byte[] {1, 2, 3, 4, 5, (byte) 'a', (byte) 'b', (byte) 'c', 6, 7, 8, 9}).mapTo(new Parsed())); }
Example #28
Source File: JBBPToJBBPToJavaConverterCompilationTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testPngParsing() throws Exception { final JBBPParser parser = JBBPParser.prepare( "long header;" + "// chunks\n" + "chunk [_]{" + " int length; " + " int type; " + " byte[length] data; " + " int crc;" + "}" ); assertCompilation(makeSources(parser, null, false, false, false)); assertCompilation(makeSources(parser, null, true, false, false)); }
Example #29
Source File: JBBPMapperTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test void testMap_LongArray() throws Exception { class Mapped { @Bin long[] a; } assertArrayEquals(new long[] {0x0102030405060708L, 0x1112131415161718L}, JBBPParser.prepare("long [_] a;").parse(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18}).mapTo(new Mapped()).a); }
Example #30
Source File: BasedOnQuestionsAndCasesTest.java From java-binary-block-parser with Apache License 2.0 | 5 votes |
@Test public void testParseBitsThroughDslBasedScriptAndMapping() throws Exception { class Bits { @Bin(name = "a", type = BinType.BIT_ARRAY, bitNumber = JBBPBitNumber.BITS_1, arraySizeExpr = "_") byte[] bit; } JBBPParser parser = JBBPParser.prepare(JBBPDslBuilder.Begin().AnnotatedClassFields(Bits.class).End()); Bits parsed = parser.parse(new byte[] {73}).mapTo(new Bits()); System.out.println(JBBPTextWriter.makeStrWriter().Bin(parsed).Close().toString()); assertArrayEquals(new byte[] {1, 0, 0, 1, 0, 0, 1, 0}, parsed.bit); }