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

org.archive.util.io.CRCInputStream Maven / Gradle / Ivy

The newest version!
package org.archive.util.io;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.CRC32;

public class CRCInputStream extends InputStream {
	private InputStream is = null;
	private CRC32 crc = null;
	private long count = 0;
	public CRCInputStream(InputStream is) {
		this(is,new CRC32());
	}
	public CRCInputStream(InputStream is, CRC32 crc) {
		this.is = is;
		this.crc = crc;
		count = 0;
	}
	@Override
	public int read() throws IOException {
		int b = is.read();
		if(b != -1) {
			crc.update(b);
			count++;
		}
		return b;
	}
	public int read(byte[] b) throws IOException {
		return read(b,0,b.length);
	}
	public int read(byte[] b, int off, int len) throws IOException {
		int amt = is.read(b, off, len);
		if(amt > -1) {
			count += amt;
			crc.update(b, off, amt);
		}
		return amt;
	}
	public long getCRCValue() {
		return crc.getValue();
	}
	public long getByteCount() {
		return count;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy