001package net.bramp.ffmpeg.io; 002 003import java.io.FilterInputStream; 004import java.io.IOException; 005import java.io.InputStream; 006import java.util.zip.CRC32; 007 008/** 009 * Calculates the CRC32 for all bytes read through the input stream. Using the java.util.zip.CRC32 010 * class to calculate the checksum. 011 */ 012public class CRC32InputStream extends FilterInputStream { 013 014 final CRC32 crc = new CRC32(); 015 016 public CRC32InputStream(InputStream in) { 017 super(in); 018 } 019 020 public void resetCrc() { 021 crc.reset(); 022 } 023 024 public long getValue() { 025 return crc.getValue(); 026 } 027 028 @Override 029 public int read() throws IOException { 030 int b = in.read(); 031 if (b >= 0) { 032 crc.update(b); 033 } 034 return b; 035 } 036 037 @Override 038 public int read(byte[] b) throws IOException { 039 int len = in.read(b); 040 crc.update(b, 0, len); 041 return len; 042 } 043 044 @Override 045 public int read(byte[] b, int off, int len) throws IOException { 046 int actual = in.read(b, off, len); 047 crc.update(b, off, actual); 048 return actual; 049 } 050 051 @Override 052 public long skip(long n) throws IOException { 053 long i = 0; 054 while (i < n) { 055 read(); 056 i++; 057 } 058 return i; 059 } 060 061 @Override 062 public synchronized void mark(int readlimit) { 063 throw new UnsupportedOperationException("mark not supported"); 064 } 065 066 @Override 067 public synchronized void reset() throws IOException { 068 throw new UnsupportedOperationException("reset not supported"); 069 } 070 071 @Override 072 public boolean markSupported() { 073 return false; 074 } 075}