snapIS.close(); if (crcIn != null) crcIn.close();
/** return if checksum matches for a snapshot **/ private boolean getCheckSum(FileSnap snap, File snapFile) throws IOException { DataTree dt = new DataTree(); Map<Long, Integer> sessions = new ConcurrentHashMap<Long, Integer>(); InputStream snapIS = new BufferedInputStream(new FileInputStream( snapFile)); CheckedInputStream crcIn = new CheckedInputStream(snapIS, new Adler32()); InputArchive ia = BinaryInputArchive.getArchive(crcIn); try { snap.deserialize(dt, sessions, ia); } catch (IOException ie) { // we failed on the most recent snapshot // must be incomplete // try reading the next one // after corrupting snapIS.close(); crcIn.close(); throw ie; } long checksum = crcIn.getChecksum().getValue(); long val = ia.readLong("val"); snapIS.close(); crcIn.close(); return (val != checksum); }
checkedInputStream.close();
private long computeChecksum(File file) throws IOException { CheckedInputStream checkedInput = new CheckedInputStream(new FileInputStream(file), new CRC32()); try { while (checkedInput.read() > -1); } finally { checkedInput.close(); } return checkedInput.getChecksum().getValue(); }
private long computeChecksum(File file) throws IOException { CheckedInputStream checkedInput = new CheckedInputStream(new FileInputStream(file), new CRC32()); try { while (checkedInput.read() > -1); } finally { checkedInput.close(); } return checkedInput.getChecksum().getValue(); }
@Override public void close() throws IOException { Checksum cs = getChecksum(); ENTROPY.setEvent((int) cs.getValue()); super.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; }
private static long getCRCFromStream(final InputStream in) { long check = 0; if (in == null) return 0; CRC32 crc = new CRC32(); try (CheckedInputStream cis = new CheckedInputStream(in, crc)) { byte[] buffer = new byte[4096]; while (cis.read(buffer) != -1) { /* Read until the end of the stream is reached. */ } check = crc.getValue(); cis.close(); } catch (IOException e) { throw new RuntimeException(e); } return check; }
private static void validateFileWithChecksum(FileSystem fs, Path filePath, BackupFileInfo backupFileInfo) throws IOException { final CheckedInputStream cin = new CheckedInputStream(fs.open(filePath), new CRC32()); final BufferedReader reader = new BufferedReader(new InputStreamReader(cin)); final ObjectMapper objectMapper = new ObjectMapper(); String line; long records = 0; // parse records just to make sure formatting is correct while ((line = reader.readLine()) != null) { objectMapper.readValue(line, BackupRecord.class); ++records; } cin.close(); long found = cin.getChecksum().getValue(); if (backupFileInfo.getChecksum() != found) { throw new IOException(format("Corrupt backup data file %s. Expected checksum %x, found %x", filePath, backupFileInfo.getChecksum(), found)); } if (backupFileInfo.getRecords() != records) { throw new IOException(format("Corrupt backup data file %s. Expected records %x, found %x", filePath, backupFileInfo.getRecords(), records)); } }
snapIS.close(); if (crcIn != null) crcIn.close();
public void close() throws IOException { try { while (skip(Long.MAX_VALUE) > 0) // process CRC-32 until EOF - this version makes FindBugs happy! ; } finally { super.close(); } final long expectedCrc = entry.getCrc(); final long actualCrc = getChecksum().getValue(); if (expectedCrc != actualCrc) throw new CRC32Exception( entry.getName(), expectedCrc, actualCrc); } } // class CheckedInputStream
public void close() throws IOException { try { while (skip(Long.MAX_VALUE) > 0) // process CRC-32 until EOF - this version makes FindBugs happy! ; } finally { super.close(); } final long expectedCrc = entry.getCrc(); final long actualCrc = getChecksum().getValue(); if (expectedCrc != actualCrc) throw new CRC32Exception( entry.getName(), expectedCrc, actualCrc); } } // class CheckedInputStream
@Override public void close() throws IOException { if (!closed) { while (skip(Long.MAX_VALUE) > 0) { // process CRC-32 until EOF } } super.close(); closed = true; final long expected = entry.getCrc(); final long computed = getChecksum().getValue(); if (expected != computed) throw new CRC32Exception(entry.getName(), expected, computed); } }
checkedInputStream.close();