Search in sources :

Example 36 with Adler32

use of java.util.zip.Adler32 in project lucene-solr by apache.

the class ReplicationHandler method getConfFileInfoFromCache.

/**
   * For configuration files, checksum of the file is included because, unlike index files, they may have same content
   * but different timestamps.
   * <p/>
   * The local conf files information is cached so that everytime it does not have to compute the checksum. The cache is
   * refreshed only if the lastModified of the file changes
   */
List<Map<String, Object>> getConfFileInfoFromCache(NamedList<String> nameAndAlias, final Map<String, FileInfo> confFileInfoCache) {
    List<Map<String, Object>> confFiles = new ArrayList<>();
    synchronized (confFileInfoCache) {
        File confDir = new File(core.getResourceLoader().getConfigDir());
        Checksum checksum = null;
        for (int i = 0; i < nameAndAlias.size(); i++) {
            String cf = nameAndAlias.getName(i);
            File f = new File(confDir, cf);
            //must not happen
            if (!f.exists() || f.isDirectory())
                continue;
            FileInfo info = confFileInfoCache.get(cf);
            if (info == null || info.lastmodified != f.lastModified() || info.size != f.length()) {
                if (checksum == null)
                    checksum = new Adler32();
                info = new FileInfo(f.lastModified(), cf, f.length(), getCheckSum(checksum, f));
                confFileInfoCache.put(cf, info);
            }
            Map<String, Object> m = info.getAsMap();
            if (nameAndAlias.getVal(i) != null)
                m.put(ALIAS, nameAndAlias.getVal(i));
            confFiles.add(m);
        }
    }
    return confFiles;
}
Also used : Checksum(java.util.zip.Checksum) ArrayList(java.util.ArrayList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) HashMap(java.util.HashMap) MetricsMap(org.apache.solr.metrics.MetricsMap) File(java.io.File) Adler32(java.util.zip.Adler32)

Example 37 with Adler32

use of java.util.zip.Adler32 in project iris by chicc999.

the class Message method getBodyCRC.

public long getBodyCRC() {
    if (bodyCRC == 0) {
        if (body != null && body.length > 0) {
            Checksum checksum = new Adler32();
            checksum.update(body, 0, body.length);
            bodyCRC = checksum.getValue();
        }
    }
    return bodyCRC;
}
Also used : Checksum(java.util.zip.Checksum) Adler32(java.util.zip.Adler32)

Example 38 with Adler32

use of java.util.zip.Adler32 in project zookeeper by apache.

the class CRCTest method getCheckSum.

/**
 * 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);
}
Also used : CheckedInputStream(java.util.zip.CheckedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) Adler32(java.util.zip.Adler32) BufferedInputStream(java.io.BufferedInputStream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 39 with Adler32

use of java.util.zip.Adler32 in project zookeeper by apache.

the class FileSnap method deserialize.

/**
 * deserialize a data tree from the most recent snapshot
 * @return the zxid of the snapshot
 */
public long deserialize(DataTree dt, Map<Long, Integer> sessions) throws IOException {
    // we run through 100 snapshots (not all of them)
    // if we cannot get it running within 100 snapshots
    // we should  give up
    List<File> snapList = findNValidSnapshots(100);
    if (snapList.size() == 0) {
        return -1L;
    }
    File snap = null;
    boolean foundValid = false;
    for (int i = 0, snapListSize = snapList.size(); i < snapListSize; i++) {
        snap = snapList.get(i);
        LOG.info("Reading snapshot " + snap);
        try (InputStream snapIS = new BufferedInputStream(new FileInputStream(snap));
            CheckedInputStream crcIn = new CheckedInputStream(snapIS, new Adler32())) {
            InputArchive ia = BinaryInputArchive.getArchive(crcIn);
            deserialize(dt, sessions, ia);
            long checkSum = crcIn.getChecksum().getValue();
            long val = ia.readLong("val");
            if (val != checkSum) {
                throw new IOException("CRC corruption in snapshot :  " + snap);
            }
            foundValid = true;
            break;
        } catch (IOException e) {
            LOG.warn("problem reading snap file " + snap, e);
        }
    }
    if (!foundValid) {
        throw new IOException("Not able to find valid snapshots in " + snapDir);
    }
    dt.lastProcessedZxid = Util.getZxidFromName(snap.getName(), SNAPSHOT_FILE_PREFIX);
    return dt.lastProcessedZxid;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) Adler32(java.util.zip.Adler32)

Example 40 with Adler32

use of java.util.zip.Adler32 in project processdash by dtuma.

the class PackageLaunchProfile method calculateContentToken.

private String calculateContentToken() throws IOException {
    List<File> files = new ArrayList<File>();
    for (FileSet fs : filesets) {
        DirectoryScanner ds = fs.getDirectoryScanner(getProject());
        for (String name : ds.getIncludedFiles()) files.add(new File(ds.getBasedir(), name));
    }
    if (files.isEmpty())
        throw new BuildException("You must designate at least one file " + "to include in the launch profile.");
    Collections.sort(files, FILENAME_SORTER);
    Checksum ck = new Adler32();
    for (File f : files) calcChecksum(f, ck);
    return Long.toString(Math.abs(ck.getValue()), Character.MAX_RADIX);
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) Checksum(java.util.zip.Checksum) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) ArrayList(java.util.ArrayList) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) Adler32(java.util.zip.Adler32)

Aggregations

Adler32 (java.util.zip.Adler32)45 IOException (java.io.IOException)11 ByteBuffer (java.nio.ByteBuffer)9 Checksum (java.util.zip.Checksum)9 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)6 CheckedInputStream (java.util.zip.CheckedInputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 File (java.io.File)5 BinaryInputArchive (org.apache.jute.BinaryInputArchive)5 CheckedOutputStream (java.util.zip.CheckedOutputStream)4 InputArchive (org.apache.jute.InputArchive)4 BufferedOutputStream (java.io.BufferedOutputStream)3 EOFException (java.io.EOFException)3 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 ArrayList (java.util.ArrayList)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Date (java.util.Date)2