下面列出了怎么用java.util.zip.CheckedInputStream的API类实例代码及写法,或者点击链接到github查看源代码。
public static void decompress(final String sourceFile, final String outputDir, final Checksum checksum)
throws IOException {
try (final FileInputStream fis = new FileInputStream(sourceFile);
final CheckedInputStream cis = new CheckedInputStream(fis, checksum);
final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(cis))) {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
final String fileName = entry.getName();
final File entryFile = new File(Paths.get(outputDir, fileName).toString());
FileUtils.forceMkdir(entryFile.getParentFile());
try (final FileOutputStream fos = new FileOutputStream(entryFile);
final BufferedOutputStream bos = new BufferedOutputStream(fos)) {
IOUtils.copy(zis, bos);
bos.flush();
fos.getFD().sync();
}
}
// Continue to read all remaining bytes(extra metadata of ZipEntry) directly from the checked stream,
// Otherwise, the checksum value maybe unexpected.
//
// See https://coderanch.com/t/279175/java/ZipInputStream
IOUtils.copy(cis, NullOutputStream.NULL_OUTPUT_STREAM);
}
}
private long calculateChecksum(URL layer) {
if (layer == null) {
return -1;
}
try {
InputStream is = layer.openStream();
try {
CheckedInputStream cis = new CheckedInputStream(is, new CRC32());
// Compute the CRC32 checksum
byte[] buf = new byte[1024];
while (cis.read(buf) >= 0) {
}
cis.close();
return cis.getChecksum().getValue();
} finally {
is.close();
}
} catch (IOException e) {
return -1;
}
}
private static long getApkFileChecksum(Context context) {
String apkPath = context.getPackageCodePath();
Long chksum = null;
try {
// Open the file and build a CRC32 checksum.
FileInputStream fis = new FileInputStream(new File(apkPath));
CRC32 chk = new CRC32();
CheckedInputStream cis = new CheckedInputStream(fis, chk);
byte[] buff = new byte[80];
while (cis.read(buff) >= 0) ;
chksum = chk.getValue();
} catch (Exception e) {
e.printStackTrace();
}
return chksum;
}
/**
* Construct the reader
* @param in The stream to read from.
* @param logVersion The version of the data coming from the stream.
*/
public Reader(DataInputStream in, StreamLimiter limiter, int logVersion) {
this.logVersion = logVersion;
if (NameNodeLayoutVersion.supports(
LayoutVersion.Feature.EDITS_CHESKUM, logVersion)) {
this.checksum = DataChecksum.newCrc32();
} else {
this.checksum = null;
}
// It is possible that the logVersion is actually a future layoutversion
// during the rolling upgrade (e.g., the NN gets upgraded first). We
// assume future layout will also support length of editlog op.
this.supportEditLogLength = NameNodeLayoutVersion.supports(
NameNodeLayoutVersion.Feature.EDITLOG_LENGTH, logVersion)
|| logVersion < NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION;
if (this.checksum != null) {
this.in = new DataInputStream(
new CheckedInputStream(in, this.checksum));
} else {
this.in = in;
}
this.limiter = limiter;
this.cache = new OpInstanceCache();
this.maxOpSize = DFSConfigKeys.DFS_NAMENODE_MAX_OP_SIZE_DEFAULT;
}
@Override
protected void parse(InputStream is) throws IOException {
// don't close CheckedInputStream, as it will close the underlying inputstream
try (UncloseableInputStream uis = new UncloseableInputStream(is);
CheckedInputStream cis = new CheckedInputStream(uis, new CRC16())) {
checkControl(cis, HDTPart.Type.TRIPLES);
checkFormat(cis, FORMAT_BITMAP);
properties = getProperties(cis);
int i = getIntegerProperty(properties, ORDER, "order");
if (i != HDTTriples.Order.SPO.getValue()) {
throw new UnsupportedOperationException(
"Triples section: order " + Integer.toString(i) + ", but only SPO order is supported");
}
checkCRC(cis, is, 2);
}
}
@Override
protected void parse(InputStream is) throws IOException {
super.parse(is);
// don't close CheckedInputStream, as it will close the underlying inputstream
try (UncloseableInputStream uis = new UncloseableInputStream(is);
CheckedInputStream cis = new CheckedInputStream(uis, new CRC32())) {
// read bytes, minimum 1
long bytes = (nrbits * entries + 7) / 8;
if (bytes > Integer.MAX_VALUE) {
throw new UnsupportedOperationException("Maximum number of bytes in array exceeded: " + bytes);
}
buffer = new byte[(int) bytes];
cis.read(buffer);
checkCRC(cis, is, 4);
}
}
@Override
protected void parse(InputStream is) throws IOException {
CRC8 crc8 = new CRC8();
crc8.update(getType());
// don't close CheckedInputStream, as it will close the underlying inputstream
try (UncloseableInputStream uis = new UncloseableInputStream(is);
CheckedInputStream cis = new CheckedInputStream(uis, crc8)) {
nrbits = cis.read();
long l = VByte.decode(cis);
if (l > Integer.MAX_VALUE) {
throw new UnsupportedOperationException("Maximum number of bytes in array exceeded: " + l);
}
entries = (int) l;
checkCRC(cis, is, 1);
}
}
/**
* Compare the calculated checksum to the expected one.
*
* @param cis checked input stream
* @param is (unchecked) input stream
* @param len number of bytes of the checksum
* @throws IOException
*/
protected static void checkCRC(CheckedInputStream cis, InputStream is, int len) throws IOException {
long calc = cis.getChecksum().getValue();
byte[] checksum = new byte[len];
is.read(checksum);
long expect = 0L;
// little-endian to big-endian, e.g. HDT-It stores checksum 7635 as 0x35 0x76 (at least on x86)
for (int i = len - 1; i >= 0; i--) {
expect <<= 8;
expect |= checksum[i] & 0xFF;
}
if (calc != expect) {
throw new IOException("CRC does not match: calculated " +
Long.toHexString(calc) + " instead of " + Long.toHexString(expect));
}
}
/**
* Construct the reader
* @param in The stream to read from.
* @param logVersion The version of the data coming from the stream.
*/
public Reader(DataInputStream in, StreamLimiter limiter, int logVersion) {
this.logVersion = logVersion;
if (NameNodeLayoutVersion.supports(
LayoutVersion.Feature.EDITS_CHESKUM, logVersion)) {
this.checksum = DataChecksum.newCrc32();
} else {
this.checksum = null;
}
// It is possible that the logVersion is actually a future layoutversion
// during the rolling upgrade (e.g., the NN gets upgraded first). We
// assume future layout will also support length of editlog op.
this.supportEditLogLength = NameNodeLayoutVersion.supports(
NameNodeLayoutVersion.Feature.EDITLOG_LENGTH, logVersion)
|| logVersion < NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION;
if (this.checksum != null) {
this.in = new DataInputStream(
new CheckedInputStream(in, this.checksum));
} else {
this.in = in;
}
this.limiter = limiter;
this.cache = new OpInstanceCache();
this.maxOpSize = DFSConfigKeys.DFS_NAMENODE_MAX_OP_SIZE_DEFAULT;
}
private SFVInfo getSFVFile(Slave slave, String path) throws IOException {
BufferedReader reader = null;
CRC32 checksum;
try {
File file = slave.getRoots().getFile(path);
checksum = new CRC32();
reader = new BufferedReader(new InputStreamReader(new CheckedInputStream(new FileInputStream(file), checksum)));
SFVInfo sfvInfo = SFVInfo.importSFVInfoFromFile(reader);
sfvInfo.setSFVFileName(file.getName());
sfvInfo.setChecksum(checksum.getValue());
return sfvInfo;
} finally {
if (reader != null) {
reader.close();
}
}
}
/**
* java.util.zip.CheckedInputStream#skip(long)
*/
public void test_skipJ() throws Exception {
// testing that the return by skip is valid
InputStream checkInput = Support_Resources.getStream("hyts_checkInput.txt");
CheckedInputStream checkIn = new CheckedInputStream(checkInput, new CRC32());
long skipValue = 5;
assertEquals("the value returned by skip(n) is not the same as its parameter",
skipValue, checkIn.skip(skipValue));
checkIn.skip(skipValue);
// ran JDK and found the checkSum value is 2235765342
// System.out.print(checkIn.getChecksum().getValue());
assertEquals("checkSum value is not correct", 2235765342L, checkIn.getChecksum()
.getValue());
assertEquals(0, checkIn.skip(0));
checkInput.close();
}
public void test_read() throws Exception {
// testing that the return by skip is valid
InputStream checkInput = Support_Resources
.getStream("hyts_checkInput.txt");
CheckedInputStream checkIn = new CheckedInputStream(checkInput,
new CRC32());
checkIn.read();
checkIn.close();
try {
checkIn.read();
fail("IOException expected.");
} catch (IOException ee) {
// expected
}
checkInput.close();
}
public void test_read$byteII() throws Exception {
// testing that the return by skip is valid
InputStream checkInput = Support_Resources
.getStream("hyts_checkInput.txt");
CheckedInputStream checkIn = new CheckedInputStream(checkInput,
new CRC32());
byte buff[] = new byte[50];
checkIn.read(buff, 10, 5);
checkIn.close();
try {
checkIn.read(buff, 10, 5);
fail("IOException expected.");
} catch (IOException ee) {
// expected
}
checkInput.close();
}
private Long getCRC(URL zipUrl) {
Long result = -1l;
try {
CRC32 crc = new CRC32();
CheckedInputStream cis = new CheckedInputStream(zipUrl.openStream(), crc);
byte[] buffer = new byte[1024];
int length;
//read the entry from zip file and extract it to disk
while( (length = cis.read(buffer)) > 0);
cis.close();
result = crc.getValue();
} catch (IOException e) {
LOG.warn("Unable to calculate CRC, resource doesn't exist?", e);
}
return result;
}
@Test
public void testChecksumStreams() throws IOException {
byte[] content = new byte[33333];
new Random().nextBytes(content);
Murmur3F murmur3F = new Murmur3F();
murmur3F.update(content);
String hash = murmur3F.getValueHexString();
murmur3F.reset();
CheckedOutputStream out = new CheckedOutputStream(new ByteArrayOutputStream(), murmur3F);
out.write(content);
Assert.assertEquals(hash, murmur3F.getValueHexString());
murmur3F.reset();
CheckedInputStream in = new CheckedInputStream(new ByteArrayInputStream(content), murmur3F);
IoUtils.readAllBytes(in);
Assert.assertEquals(hash, murmur3F.getValueHexString());
}
/**
* Construct the reader
* @param in The stream to read from.
* @param logVersion The version of the data coming from the stream.
*/
@SuppressWarnings("deprecation")
public Reader(DataInputStream in, int logVersion) {
this.logVersion = logVersion;
if (LayoutVersion.supports(Feature.EDITS_CHESKUM, logVersion)) {
this.checksum = FSEditLog.getChecksumForRead();
} else {
this.checksum = null;
}
if (this.checksum != null) {
this.in = new DataInputStream(
new CheckedInputStream(in, this.checksum));
} else {
this.in = in;
}
}
@Override
public Long getFileChecksum(File file) {
long checksum = -1;
byte[] buffer = new byte[512];
try {
FileInputStream fis = new FileInputStream(file);
CheckedInputStream cis = new CheckedInputStream(fis, check);
int read = cis.read(buffer);
while(read != -1){
read = cis.read(buffer);
}
fis.close();
cis.close();
checksum = check.getValue();
} catch (IOException ex) {
logger.error("Error getting file checksum: "+ex);
}
return checksum;
}
@Override
public Long getFileChecksum(File file) {
long checksum = -1;
byte[] buffer = new byte[512];
Checksum check = new Adler32();
try {
FileInputStream fis = new FileInputStream(file);
CheckedInputStream cis = new CheckedInputStream(fis, check);
int read = cis.read(buffer);
while(read != -1){
read = cis.read(buffer);
}
checksum = check.getValue();
} catch (IOException ex) {
logger.error("Error getting file checksum: "+ex);
}
return checksum;
}
/**
* Computes the CRC checksum for the given file.
*
* @param file The file to compute checksum for.
* @return A CRC32 checksum.
* @throws IOException If an I/O error occurs.
*/
private static long computeCrc(Path file) throws IOException {
try (InputStream is = Files.newInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
CheckedInputStream cis = new CheckedInputStream(bis, new CRC32())) {
byte[] buf = new byte[8192];
while ((cis.read(buf)) != -1) {
continue;
}
return cis.getChecksum().getValue();
}
}
/**
* Computes the checksum with the given options.
*
* @param memory The memory to generate the checksum from.
* @param addrSet The addresses over which to generate the checksum.
* @param monitor Cancelable task monitor to cancel the computation.
* @param onesComp True if the checksum should be complemented with a ones complement.
* @param twosComp True if the checksum should be complemented with a twos complement.
* @throws MemoryAccessException If there was a problem reading the memory.
* @throws CancelledException If the user cancels the task.
*/
public void updateChecksum(Memory memory, AddressSetView addrSet, TaskMonitor monitor,
boolean onesComp, boolean twosComp) throws MemoryAccessException, CancelledException {
byte[] bytes = new byte[1024];
long crc;
try (CheckedInputStream cis =
new CheckedInputStream(new MemoryInputStream(memory, addrSet), new CRC32())) {
while (cis.read(bytes) > 0) {
if (monitor.isCancelled()) {
throw new CancelledException();
}
}
crc = cis.getChecksum().getValue();
}
catch (IOException e) {
throw new MemoryAccessException(e.getMessage());
}
if (onesComp) {
crc = ~crc;
}
else if (twosComp) {
crc = -crc;
}
checksum = toArray(crc, 4);
}
/**
* Computes the checksum with the given options.
*
* @param memory The memory to generate the checksum from.
* @param addrSet The addresses over which to generate the checksum.
* @param monitor Cancelable task monitor to cancel the computation.
* @param onesComp True if the checksum should be complemented with a ones complement.
* @param twosComp True if the checksum should be complemented with a twos complement.
* @throws MemoryAccessException If there was a problem accessing the specified memory.
* @throws CancelledException If checksum generation was cancelled.
*/
public void updateChecksum(Memory memory, AddressSetView addrSet, TaskMonitor monitor,
boolean onesComp, boolean twosComp) throws MemoryAccessException, CancelledException {
byte[] bytes = new byte[1024];
long sum;
try (CheckedInputStream cis =
new CheckedInputStream(new MemoryInputStream(memory, addrSet), new Adler32())) {
while (cis.read(bytes) > 0) {
if (monitor.isCancelled()) {
throw new CancelledException();
}
}
sum = cis.getChecksum().getValue();
}
catch (IOException e) {
throw new MemoryAccessException(e.getMessage());
}
if (onesComp) {
sum = ~sum;
}
else if (twosComp) {
sum = -sum;
}
checksum = toArray(sum, 4);
}
/**
* Return the CRC checksum of a given part
*
* @param partIndex
* the index of the part
* @param isAttachment
* true if the part is an attachment
* @return the part checksum
* @throws PackageException
*/
@PublicAtsApi
public long getPartChecksum(
int partIndex,
boolean isAttachment ) throws PackageException {
InputStream partDataStream = getPartData(partIndex, isAttachment);
if (partDataStream != null) {
try {
SeekInputStream seekDataStream = new SeekInputStream(partDataStream);
seekDataStream.seek(0);
// create a new crc and reset it
CRC32 crc = new CRC32();
// use checked stream to get the checksum
CheckedInputStream stream = new CheckedInputStream(seekDataStream, crc);
int bufLen = 4096;
byte[] buffer = new byte[bufLen];
int numBytesRead = bufLen;
while (numBytesRead == bufLen) {
numBytesRead = stream.read(buffer, 0, bufLen);
}
long checksum = stream.getChecksum().getValue();
stream.close();
return checksum;
} catch (IOException ioe) {
throw new PackageException(ioe);
}
} else {
throw new MimePartWithoutContentException("MIME part does not have any content");
}
}
public SpillRecord(Path indexFileName, JobConf job, Checksum crc,
String expectedIndexOwner)
throws IOException {
final FileSystem rfs = FileSystem.getLocal(job).getRaw();
final FSDataInputStream in =
SecureIOUtils.openFSDataInputStream(new File(indexFileName.toUri()
.getRawPath()), expectedIndexOwner, null);
try {
final long length = rfs.getFileStatus(indexFileName).getLen();
final int partitions = (int) length / MAP_OUTPUT_INDEX_RECORD_LENGTH;
final int size = partitions * MAP_OUTPUT_INDEX_RECORD_LENGTH;
buf = ByteBuffer.allocate(size);
if (crc != null) {
crc.reset();
CheckedInputStream chk = new CheckedInputStream(in, crc);
IOUtils.readFully(chk, buf.array(), 0, size);
if (chk.getChecksum().getValue() != in.readLong()) {
throw new ChecksumException("Checksum error reading spill index: " +
indexFileName, -1);
}
} else {
IOUtils.readFully(in, buf.array(), 0, size);
}
entries = buf.asLongBuffer();
} finally {
in.close();
}
}
@Override
protected void parse(InputStream is) throws IOException {
// don't close CheckedInputStream, as it will close the underlying inputstream
try (UncloseableInputStream uis = new UncloseableInputStream(is);
CheckedInputStream cis = new CheckedInputStream(uis, new CRC16())) {
checkControl(cis, HDTPart.Type.GLOBAL);
checkFormat(cis, GLOBAL_FORMAT);
properties = getProperties(cis);
checkCRC(cis, is, 2);
}
}
@Override
protected void parse(InputStream is) throws IOException {
// don't close CheckedInputStream, as it will close the underlying inputstream
try (UncloseableInputStream uis = new UncloseableInputStream(is);
CheckedInputStream cis = new CheckedInputStream(uis, new CRC16())) {
checkControl(cis, HDTPart.Type.DICTIONARY);
checkFormat(cis, DICT_FORMAT);
properties = getProperties(cis);
checkCRC(cis, is, 2);
}
}
@Override
protected void parse(InputStream is) throws IOException {
// don't close CheckedInputStream, as it will close the underlying inputstream
try (UncloseableInputStream uis = new UncloseableInputStream(is);
CheckedInputStream cis = new CheckedInputStream(uis, new CRC16())) {
checkControl(cis, HDTPart.Type.HEADER);
checkFormat(cis, HEADER_FORMAT);
properties = getProperties(cis);
checkCRC(cis, is, 2);
}
int hlen = getIntegerProperty(properties, HEADER_LENGTH, "Header length");
headerData = parseHeaderData(is, hlen);
}
@Override
protected void parse(InputStream is) throws IOException {
long bytes = 0L;
// don't close CheckedInputStream, as it will close the underlying inputstream
try (UncloseableInputStream uis = new UncloseableInputStream(is);
CheckedInputStream cis = new CheckedInputStream(uis, new CRC8())) {
int dtype = cis.read();
if (dtype != BITMAP1) {
throw new UnsupportedOperationException("Bitmap encoding " + Long.toHexString(dtype) +
", but only bitmap v1 is supported");
}
long b = (int) VByte.decode(cis);
if (b > Integer.MAX_VALUE) {
throw new UnsupportedOperationException("Maximum number of entries in bitmap exceeded: " + b);
}
bits = (int) b;
bytes = (bits + 7) / 8;
checkCRC(cis, is, 1);
}
// don't close CheckedInputStream, as it will close the underlying inputstream
try (UncloseableInputStream uis = new UncloseableInputStream(is);
CheckedInputStream cis = new CheckedInputStream(uis, new CRC32())) {
buffer = new byte[(int) bytes];
cis.read(buffer);
checkCRC(cis, is, 4);
}
}
public SpillRecord(Path indexFileName, JobConf job, Checksum crc,
String expectedIndexOwner)
throws IOException {
final FileSystem rfs = FileSystem.getLocal(job).getRaw();
final FSDataInputStream in =
SecureIOUtils.openFSDataInputStream(new File(indexFileName.toUri()
.getRawPath()), expectedIndexOwner, null);
try {
final long length = rfs.getFileStatus(indexFileName).getLen();
final int partitions = (int) length / MAP_OUTPUT_INDEX_RECORD_LENGTH;
final int size = partitions * MAP_OUTPUT_INDEX_RECORD_LENGTH;
buf = ByteBuffer.allocate(size);
if (crc != null) {
crc.reset();
CheckedInputStream chk = new CheckedInputStream(in, crc);
IOUtils.readFully(chk, buf.array(), 0, size);
if (chk.getChecksum().getValue() != in.readLong()) {
throw new ChecksumException("Checksum error reading spill index: " +
indexFileName, -1);
}
} else {
IOUtils.readFully(in, buf.array(), 0, size);
}
entries = buf.asLongBuffer();
} finally {
in.close();
}
}
public long checkSum(PhysicalFile file) throws IOException {
logger.debug("Checksumming: {}", file.getPath());
CRC32 crc32 = new CRC32();
try (CheckedInputStream in = new CheckedInputStream(new BufferedInputStream(new FileInputStream(file)),
crc32)) {
byte[] buf = new byte[16384];
while (in.read(buf) != -1) {
}
return crc32.getValue();
}
}
private void calculateChecksum(Path file) throws IOException {
final byte[] buff = new byte[16384];
try (CheckedInputStream in = new CheckedInputStream(new BufferedInputStream(
Files.newInputStream(file)), new CRC32())) {
while (in.read(buff) != -1) {
// do nothing, we are only checking for crc
}
} catch (IOException e) {
throw new IOException("CRC check failed for " + file);
}
}