view com/hjg/pngj/ChunkSeqSkipping.java @ 4:3f418d4451d6 pngj

add pngj
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 15 Apr 2016 09:52:58 +0200
parents
children
line wrap: on
line source

package ar.com.hjg.pngj;

import java.util.ArrayList;
import java.util.List;

import ar.com.hjg.pngj.ChunkReader.ChunkReaderMode;
import ar.com.hjg.pngj.chunks.ChunkRaw;

/**
 * This simple reader skips all chunks contents and stores the chunkRaw in a list. Useful to read chunks structure.
 * 
 * Optionally the contents might be processed. This doesn't distinguish IDAT chunks
 */
public class ChunkSeqSkipping extends ChunkSeqReader {

  private List<ChunkRaw> chunks = new ArrayList<ChunkRaw>();
  private boolean skip = true;

  /**
   * @param skipAll if true, contents will be truly skipped, and CRC will not be computed
   */
  public ChunkSeqSkipping(boolean skipAll) {
    super(true);
    skip = skipAll;
  }

  public ChunkSeqSkipping() {
    this(true);
  }

  protected ChunkReader createChunkReaderForNewChunk(String id, int len, long offset, boolean skip) {
    return new ChunkReader(len, id, offset, skip ? ChunkReaderMode.SKIP : ChunkReaderMode.PROCESS) {
      @Override
      protected void chunkDone() {
        postProcessChunk(this);
      }

      @Override
      protected void processData(int offsetinChhunk, byte[] buf, int off, int len) {
        processChunkContent(getChunkRaw(), offsetinChhunk, buf, off, len);
      }
    };
  }

  protected void processChunkContent(ChunkRaw chunkRaw, int offsetinChhunk, byte[] buf, int off,
      int len) {
    // does nothing
  }

  @Override
  protected void postProcessChunk(ChunkReader chunkR) {
    super.postProcessChunk(chunkR);
    chunks.add(chunkR.getChunkRaw());
  }

  @Override
  protected boolean shouldSkipContent(int len, String id) {
    return skip;
  }

  @Override
  protected boolean isIdatKind(String id) {
    return false;
  }

  public List<ChunkRaw> getChunks() {
    return chunks;
  }

}