All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.emv.qrcode.decoder.cpm.DecodeCpmIterator Maven / Gradle / Ivy

There is a newer version: 0.1.2
Show newest version
package com.emv.qrcode.decoder.cpm;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Consumer;

import com.emv.qrcode.core.utils.BERUtils;

// @formatter:off
final class DecodeCpmIterator implements Iterator {

  private Integer current;

  private final Integer max;

  private final byte[] source;

  public DecodeCpmIterator(final byte[] source) {
    current = 0;
    max = source.length;
    this.source = source;
  }

  @Override
  public boolean hasNext() {

    if (current >= max) {
      return false;
    }

    final Integer countBytesOfTag = BERUtils.countBytesOfTag(source, current);
    final Integer countBytesOfLength = BERUtils.countBytesOfLength(source, current);
    final Integer valueLength = BERUtils.valueOfLength(source, current);

    return current + countBytesOfTag + countBytesOfLength + valueLength <= max;
  }

  @Override
  public void forEachRemaining(final Consumer action) {
    while (hasNext()) {
      action.accept(next());
    }
  }

  @Override
  public byte[] next() {

    if(!hasNext()){
      throw new NoSuchElementException();
    }

    final byte[] value = BERUtils.chunk(source, current);

    current += value.length;

    return value;
  }

}
// @formatter:on




© 2015 - 2024 Weber Informatics LLC | Privacy Policy