Java Code Examples for com.igormaznitsa.jbbp.io.JBBPBitInputStream#skip()

The following examples show how to use com.igormaznitsa.jbbp.io.JBBPBitInputStream#skip() . 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: SCLPlugin.java    From zxpoly with GNU General Public License v3.0 5 votes vote down vote up
@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 2
Source File: TRDPlugin.java    From zxpoly with GNU General Public License v3.0 5 votes vote down vote up
@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 3
Source File: TARawUnit.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
public void fetchContent(JBBPBitInputStream stream) throws IOException {
	  if (magic==0x3BF8E9C1) {
		  unit = new TAUnit(unitNumber,stream.readByteArray(length));
		  if (length % 4 != 0) {
			  stream.skip(4 - length % 4);
		  }
	  }
}
 
Example 4
Source File: TAPPlugin.java    From zxpoly with GNU General Public License v3.0 4 votes vote down vote up
@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);
  }
}
 
Example 5
Source File: USBRecord.java    From Flashtool with GNU General Public License v3.0 4 votes vote down vote up
public void parse(JBBPBitInputStream usbStream) throws IOException {

		JBBPParser USBHeaderParser = JBBPParser.prepare(
				"<short usb_Length;" +
				"<short usb_Function;" +
				"<int usb_Status;" +
				"<long usb_UsbDeviceHandle;" +
				"<long usb_UsbdFlags;" +
				"<long usb_PipeHandle;" +
				"<int usb_TransferFlags;" +
				"<int usb_TransferBufferLength;" +
				"<long usb_TransferBuffer;" +
				"<long usb_TransferBufferMDL;" +
				"<long usb_UrbLink;" +
				"<long usb_hcdendpoint;" +
				"<long usb_hcdirp;" +
				"<long usb_hcdlistentry;" +
				"<long usb_flink;" +
				"<long usb_blink;" +
				"<long usb_hcdlistentry2;" +
				"<long usb_hcdcurrentflushpointer;" +
				"<long usb_hcdextension;"
				);

		JBBPParser USBHeaderExtended = JBBPParser.prepare(
				"byte brmRequestType;" + 
				"byte bRequest;" + 
				"<short wValue;" + 
				"<short wIndex;" + 
				"<short wLength;"
				);
		if (recordlength>=128) {
			header = USBHeaderParser.parse(usbStream).mapTo(new USBHeader());
		}
		else {
			usbStream.skip(recordlength);
		}
		try {
			if (header.usb_Length!=128) {
				header=null;
				usbStream.skip(recordlength-128);
			}
			else data = usbStream.readByteArray(recordlength-128);
		} catch (NullPointerException npe) {}
	}