Java Code Examples for com.igormaznitsa.jbbp.io.JBBPBitInputStream#getCounter()
The following examples show how to use
com.igormaznitsa.jbbp.io.JBBPBitInputStream#getCounter() .
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: JBBPParser.java From java-binary-block-parser with Apache License 2.0 | 6 votes |
/** * Parse am input stream with defined external value provider. * * @param in an input stream which content will be parsed, it must not be null * @param varFieldProcessor a var field processor, it may be null if there is * not any var field in a script, otherwise NPE will be thrown during parsing * @param externalValueProvider an external value provider, it can be null but * only if the script doesn't have fields desired the provider * @return the parsed content as the root structure * @throws IOException it will be thrown for transport errors */ public JBBPFieldStruct parse(final InputStream in, final JBBPVarFieldProcessor varFieldProcessor, final JBBPExternalValueProvider externalValueProvider) throws IOException { final JBBPBitInputStream bitInStream = in instanceof JBBPBitInputStream ? (JBBPBitInputStream) in : new JBBPBitInputStream(in, bitOrder); this.finalStreamByteCounter = bitInStream.getCounter(); final JBBPNamedNumericFieldMap fieldMap; if (this.compiledBlock.hasEvaluatedSizeArrays() || this.compiledBlock.hasVarFields()) { fieldMap = new JBBPNamedNumericFieldMap(externalValueProvider); } else { fieldMap = null; } if (this.compiledBlock.hasVarFields()) { JBBPUtils.assertNotNull(varFieldProcessor, "The Script contains VAR fields, a var field processor must be provided"); } try { return new JBBPFieldStruct(new JBBPNamedFieldInfo("", "", -1), parseStruct(bitInStream, new JBBPIntCounter(), varFieldProcessor, fieldMap, new JBBPIntCounter(), new JBBPIntCounter(), false)); } finally { this.finalStreamByteCounter = bitInStream.getCounter(); } }
Example 2
Source File: JBBPOnlyFieldEvaluator.java From java-binary-block-parser with Apache License 2.0 | 6 votes |
@Override public int eval(final JBBPBitInputStream inStream, final int currentCompiledBlockOffset, final JBBPCompiledBlock block, final JBBPNamedNumericFieldMap fieldMap) { final int result; if (this.externalFieldName == null) { final JBBPNamedFieldInfo namedField = block.getNamedFields()[this.namedFieldIndex]; final JBBPNumericField numericField = fieldMap.get(namedField); if (numericField == null) { throw new java.lang.ArithmeticException("Can't find field '" + namedField.getFieldName() + "' among numeric fields"); } else { result = numericField.getAsInt(); } } else { result = this.externalFieldName.equals("$") ? (int) inStream.getCounter() : fieldMap.getExternalFieldValue(this.externalFieldName, block, this); } return result; }
Example 3
Source File: SCLPlugin.java From zxpoly with GNU General Public License v3.0 | 5 votes |
@Override public ReadResult readFrom(final File file, final int index) throws IOException { final List<SCLCatalogItem> list = new ArrayList<>(); final JBBPBitInputStream in = new JBBPBitInputStream(new FileInputStream(file)); try { final long id = in.readLong(JBBPByteOrder.BIG_ENDIAN); if (id == 0x53494E434C414952L) { // it's scl final int fileNumber = in.readByte(); for (int i = 0; i < fileNumber; i++) { final SCLCatalogItem item = CATALOG_PARSER.parse(in).mapTo(new SCLCatalogItem()); list.add(item); } final SCLCatalogItem itemToRead = list.get(index); for (int i = 0; i < index; i++) { final int len = list.get(i).sectors * 256; if (len != in.skip(len)) { throw new IllegalStateException("Can't skip bytes:" + list.get(i).length); } } final long offset = in.getCounter(); return new ReadResult(new ZXPolyData( new Info(itemToRead.name, itemToRead.type, itemToRead.start, itemToRead.length, (int) offset), this, in.readByteArray(itemToRead.sectors * 256)), null); } else { throw new IllegalArgumentException("It's not a SCl file: " + file); } } finally { JBBPUtils.closeQuietly(in); } }
Example 4
Source File: TRDPlugin.java From zxpoly with GNU General Public License v3.0 | 5 votes |
@Override public ReadResult readFrom(final File file, final int index) throws IOException { final JBBPBitInputStream inStream = new JBBPBitInputStream(new FileInputStream(file), JBBPBitOrder.LSB0); try { final List<TRDosCatalogItem> list = new ArrayList<>(); for (int i = 0; i < 128; i++) { final TRDosCatalogItem item = CATALOG_PARSER.parse(inStream).mapTo(new TRDosCatalogItem()); if (item.name.charAt(0) > 1) { list.add(item); } } final TRDosCatalogItem info = list.get(index); final int offsetToFile = ((info.track << 4) + info.firstSector) * 256; final long toskip = offsetToFile - inStream.getCounter(); final long skept = inStream.skip(toskip); if (skept != toskip) { throw new IllegalStateException("Can't skip needed byte number [" + toskip + ']'); } return new ReadResult( new ZXPolyData(new Info(info.name, info.type, info.start, info.length, offsetToFile), this, inStream.readByteArray(info.sectors << 8)), null); } finally { JBBPUtils.closeQuietly(inStream); } }
Example 5
Source File: TAPPlugin.java From zxpoly with GNU General Public License v3.0 | 4 votes |
@Override public ReadResult readFrom(final File file, final int index) throws IOException { JBBPBitInputStream in = new JBBPBitInputStream(new FileInputStream(file)); try { int curindex = 0; while (in.hasAvailableData()) { final int length = in.readUnsignedShort(JBBPByteOrder.LITTLE_ENDIAN); final int flag = in.readByte(); final int offset = (int) in.getCounter(); final Info info; if (flag == 0) { // standard rom final int standardflag = in.readByte(); final byte[] data = in.readByteArray(length - 2); final int datalen = extractDataLengthField(data); final int address = extractStartAddressField(data); switch (standardflag) { case 0: { // program header info = new Info(extractHeaderName(data), 'B', address, datalen, offset); } break; case 1: { // numeric data array header info = new Info(extractHeaderName(data), 'N', address, datalen, offset); } break; case 2: { // alphanumeric data array header info = new Info(extractHeaderName(data), 'S', address, datalen, offset); } break; case 3: { // code block info = new Info(extractHeaderName(data), 'C', address, datalen, offset); } break; default: { // unknown info = new Info("<Unknown>", 'U', address, length, offset); } break; } if (curindex < index) { curindex++; } else { throw new IllegalArgumentException("Selected item is not a data block but a header"); } } else { if (flag == 0xFF) { // data block info = new Info("<Code>", 'D', -1, length - 2, offset); } else { // custom info = new Info("<Unknown>", 'U', -1, length, offset); } if (curindex < index) { curindex++; in.skip(length - 1); } else { return new ReadResult(new ZXPolyData(info, this, in.readByteArray(length - 1)), null); } } } throw new IllegalArgumentException("Can't find file for index " + index); } finally { JBBPUtils.closeQuietly(in); } }