Example usage for java.util.zip CRC32 CRC32

List of usage examples for java.util.zip CRC32 CRC32

Introduction

In this page you can find the example usage for java.util.zip CRC32 CRC32.

Prototype

public CRC32() 

Source Link

Document

Creates a new CRC32 object.

Usage

From source file:PngEncoder.java

/**
 * Encodes the image./*w w  w . j  av a 2 s. co  m*/
 *
 * @param out an OutputStream to which the encoded image will be
 *            written
 * @throws IOException if a problem is encountered writing the output
 */
public synchronized void encode(OutputStream out) throws IOException {
    Checksum csum = new CRC32();
    out = new CheckedOutputStream(out, csum);

    out.write(SIGNATURE);

    writeIhdrChunk(out, csum);

    if (outputBpp == 1) {
        writePlteChunk(out, csum);
    }

    writeIdatChunks(out, csum);

    writeIendChunk(out, csum);
}

From source file:org.kalypso.commons.java.util.zip.ZipUtilities.java

private static void processFiles(final File packDir, final File file, final ZipOutputStream out,
        final IFileFilter filter) throws IOException {
    if (!filter.accept(file))
        return;/* w ww . ja v  a 2s .  c om*/

    if (file.isDirectory()) {
        final ZipEntry e = new ZipEntry(convertFileName(packDir, file) + "/"); //$NON-NLS-1$
        out.putNextEntry(e);
        out.closeEntry();

        final File[] files = file.listFiles();
        for (final File f : files) {
            processFiles(packDir, f, out, filter);
        }
    } else {

        final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

        final ZipEntry e = new ZipEntry(convertFileName(packDir, file));
        final CRC32 crc = new CRC32();

        out.putNextEntry(e);

        final byte[] buf = new byte[4096];
        int len = 0;

        while ((len = bis.read(buf)) > 0) {
            out.write(buf, 0, len);
            crc.update(buf, 0, len);
        }
        e.setCrc(crc.getValue());

        out.closeEntry();
        bis.close();

    }
}

From source file:org.apache.jackrabbit.oak.plugins.segment.file.TarReader.java

/**
 * Scans through the tar file, looking for all segment entries.
 *
 * @throws IOException if the tar file could not be read
 *///from  ww w .j av a 2  s  . c  om
private static void recoverEntries(File file, RandomAccessFile access, LinkedHashMap<UUID, byte[]> entries)
        throws IOException {
    byte[] header = new byte[BLOCK_SIZE];
    while (access.getFilePointer() + BLOCK_SIZE <= access.length()) {
        // read the tar header block
        access.readFully(header);

        // compute the header checksum
        int sum = 0;
        for (int i = 0; i < BLOCK_SIZE; i++) {
            sum += header[i] & 0xff;
        }

        // identify possible zero block
        if (sum == 0 && access.getFilePointer() + 2 * BLOCK_SIZE == access.length()) {
            return; // found the zero blocks at the end of the file
        }

        // replace the actual stored checksum with spaces for comparison
        for (int i = 148; i < 148 + 8; i++) {
            sum -= header[i] & 0xff;
            sum += ' ';
        }

        byte[] checkbytes = String.format("%06o\0 ", sum).getBytes(UTF_8);
        for (int i = 0; i < checkbytes.length; i++) {
            if (checkbytes[i] != header[148 + i]) {
                log.warn("Invalid entry checksum at offset {} in tar file {}, skipping...",
                        access.getFilePointer() - BLOCK_SIZE, file);
            }
        }

        // The header checksum passes, so read the entry name and size
        ByteBuffer buffer = ByteBuffer.wrap(header);
        String name = readString(buffer, 100);
        buffer.position(124);
        int size = readNumber(buffer, 12);
        if (access.getFilePointer() + size > access.length()) {
            // checksum was correct, so the size field should be accurate
            log.warn("Partial entry {} in tar file {}, ignoring...", name, file);
            return;
        }

        Matcher matcher = NAME_PATTERN.matcher(name);
        if (matcher.matches()) {
            UUID id = UUID.fromString(matcher.group(1));

            String checksum = matcher.group(3);
            if (checksum != null || !entries.containsKey(id)) {
                byte[] data = new byte[size];
                access.readFully(data);

                // skip possible padding to stay at block boundaries
                long position = access.getFilePointer();
                long remainder = position % BLOCK_SIZE;
                if (remainder != 0) {
                    access.seek(position + (BLOCK_SIZE - remainder));
                }

                if (checksum != null) {
                    CRC32 crc = new CRC32();
                    crc.update(data);
                    if (crc.getValue() != Long.parseLong(checksum, 16)) {
                        log.warn("Checksum mismatch in entry {} of tar file {}, skipping...", name, file);
                        continue;
                    }
                }

                entries.put(id, data);
            }
        } else if (!name.equals(file.getName() + ".idx")) {
            log.warn("Unexpected entry {} in tar file {}, skipping...", name, file);
            long position = access.getFilePointer() + size;
            long remainder = position % BLOCK_SIZE;
            if (remainder != 0) {
                position += BLOCK_SIZE - remainder;
            }
            access.seek(position);
        }
    }
}

From source file:org.apache.jackrabbit.oak.segment.file.TarReader.java

/**
 * Scans through the tar file, looking for all segment entries.
 *
 * @throws IOException if the tar file could not be read
 *//*from w w  w  .ja  v  a2  s . c  om*/
private static void recoverEntries(File file, RandomAccessFile access, LinkedHashMap<UUID, byte[]> entries)
        throws IOException {
    byte[] header = new byte[BLOCK_SIZE];
    while (access.getFilePointer() + BLOCK_SIZE <= access.length()) {
        // read the tar header block
        access.readFully(header);

        // compute the header checksum
        int sum = 0;
        for (int i = 0; i < BLOCK_SIZE; i++) {
            sum += header[i] & 0xff;
        }

        // identify possible zero block
        if (sum == 0 && access.getFilePointer() + 2 * BLOCK_SIZE == access.length()) {
            return; // found the zero blocks at the end of the file
        }

        // replace the actual stored checksum with spaces for comparison
        for (int i = 148; i < 148 + 8; i++) {
            sum -= header[i] & 0xff;
            sum += ' ';
        }

        byte[] checkbytes = String.format("%06o\0 ", sum).getBytes(UTF_8);
        for (int i = 0; i < checkbytes.length; i++) {
            if (checkbytes[i] != header[148 + i]) {
                log.warn("Invalid entry checksum at offset {} in tar file {}, skipping...",
                        access.getFilePointer() - BLOCK_SIZE, file);
            }
        }

        // The header checksum passes, so read the entry name and size
        ByteBuffer buffer = wrap(header);
        String name = readString(buffer, 100);
        buffer.position(124);
        int size = readNumber(buffer, 12);
        if (access.getFilePointer() + size > access.length()) {
            // checksum was correct, so the size field should be accurate
            log.warn("Partial entry {} in tar file {}, ignoring...", name, file);
            return;
        }

        Matcher matcher = NAME_PATTERN.matcher(name);
        if (matcher.matches()) {
            UUID id = UUID.fromString(matcher.group(1));

            String checksum = matcher.group(3);
            if (checksum != null || !entries.containsKey(id)) {
                byte[] data = new byte[size];
                access.readFully(data);

                // skip possible padding to stay at block boundaries
                long position = access.getFilePointer();
                long remainder = position % BLOCK_SIZE;
                if (remainder != 0) {
                    access.seek(position + (BLOCK_SIZE - remainder));
                }

                if (checksum != null) {
                    CRC32 crc = new CRC32();
                    crc.update(data);
                    if (crc.getValue() != Long.parseLong(checksum, 16)) {
                        log.warn("Checksum mismatch in entry {} of tar file {}, skipping...", name, file);
                        continue;
                    }
                }

                entries.put(id, data);
            }
        } else if (!name.equals(file.getName() + ".idx")) {
            log.warn("Unexpected entry {} in tar file {}, skipping...", name, file);
            long position = access.getFilePointer() + size;
            long remainder = position % BLOCK_SIZE;
            if (remainder != 0) {
                position += BLOCK_SIZE - remainder;
            }
            access.seek(position);
        }
    }
}

From source file:com.blackducksoftware.tools.commonframework.core.config.ConfigurationFileTest.java

/**
 * We don't handle this scenario// w  ww  .java2s  .c  o m
 *
 * @throws Exception
 */
@Test
public void testLegacyPasswordBase64IsplaintextNotSet() throws Exception {
    final File sourceConfigFile = new File("src/test/resources/psw_encryption/legacy_base64_notset.properties");
    final File configFile = File.createTempFile(
            "com.blackducksoftware.tools.commonframework.core.config.ConfigurationFileTest", "test3");
    filesToDelete.add(configFile);
    configFile.deleteOnExit();
    FileUtils.copyFile(sourceConfigFile, configFile);
    final ConfigurationFile cf = new ConfigurationFile(configFile.getAbsolutePath());

    List<String> updatedLines = null;
    if (cf.isInNeedOfUpdate()) {
        updatedLines = cf.saveWithEncryptedPasswords();
    }

    // This warning has been disabled to avoid calling isBase64, which
    // causes problems for RGT
    // System.out.println("There should be 3 warnings in the log about possibly-base64-encoded passwords");

    assertTrue(updatedLines.size() > 0);
    final Iterator<String> updatedLinesIter = updatedLines.iterator();
    while (updatedLinesIter.hasNext()) {
        String updatedLine = updatedLinesIter.next();

        // make sure obsolete properties have been removed
        assertFalse(updatedLine.matches("^.*\\.password\\.isplaintext=.*$"));

        // If this is a password, verify that it was encoded, and that the
        // isencrypted=true was inserted after it
        if (updatedLine.startsWith("cc.password=")) {
            // There's not much point in checking the value of password,
            // since it is wrong
            updatedLine = updatedLinesIter.next();
            assertEquals("cc.password.isencrypted=true", updatedLine);
        } else if (updatedLine.startsWith("protex.password=")) {
            // There's not much point in checking the value of password,
            // since it is wrong
            updatedLine = updatedLinesIter.next();
            assertEquals("protex.password.isencrypted=true", updatedLine);
        } else if (updatedLine.startsWith("connector.0.password=")) {
            // There's not much point in checking the value of password,
            // since it is wrong
            updatedLine = updatedLinesIter.next();
            assertEquals("connector.0.password.isencrypted=true", updatedLine);
        }
    }

    final File testGeneratedUpdatedFile = File.createTempFile(
            "com.blackducksoftware.tools.commonframework.core.config.ConfigurationFileTest",
            "test3_testGeneratedUpdatedFile");
    filesToDelete.add(testGeneratedUpdatedFile);
    testGeneratedUpdatedFile.deleteOnExit();
    FileUtils.writeLines(testGeneratedUpdatedFile, updatedLines);
    final long csumTestGeneratedFile = FileUtils.checksum(testGeneratedUpdatedFile, new CRC32()).getValue();
    final long csumActualFile = FileUtils.checksum(configFile, new CRC32()).getValue();
    assertEquals(csumTestGeneratedFile, csumActualFile);
}

From source file:org.apache.hadoop.raid.TestBlockCopier.java

static void validateFileCopy(FileSystem fs, Path path, long size, long[] blockCrcs, boolean twiceThrough)
        throws IOException {

    final int timesThrough = (twiceThrough ? 2 : 1);
    final int numBlocks = (int) Math.ceil((double) size / BLOCK_SIZE);

    // Check all the blocks timesThrough times
    FSDataInputStream in = fs.open(path);
    CRC32 crc = new CRC32();

    for (int i = 0; i < timesThrough; i++) {
        for (int b = 0; b < numBlocks; b++) {
            int chunkSize = (int) Math.min(BLOCK_SIZE, (size - (b * BLOCK_SIZE)));
            byte[] buf = new byte[chunkSize];

            in.read(buf);/*from w  ww  . j a  v a  2  s  .  c o  m*/
            crc.reset();
            crc.update(buf);

            assertEquals(("Block crc " + b + " did not match on iteration " + i), blockCrcs[b], crc.getValue());
        }
        assert in.getPos() == size : "Did not read to end of file";
        if (i < (timesThrough - 1)) {
            in.seekToNewSource(0);
        }
    }
}

From source file:com.autoupdateapk.AutoUpdateApk.java

private static int crc32(String str) {
    byte bytes[] = str.getBytes();
    Checksum checksum = new CRC32();
    checksum.update(bytes, 0, bytes.length);
    return (int) checksum.getValue();
}

From source file:de.mpg.escidoc.services.dataacquisition.DataHandlerBean.java

/**
 * fetch data from a given url.//w ww.  ja  v a2s  .  co m
 * 
 * @param url
 * @return byte[]
 * @throws SourceNotAvailableException
 * @throws RuntimeException
 * @throws AccessException
 */
public byte[] fetchMetadatafromURL(URL url)
        throws SourceNotAvailableException, RuntimeException, AccessException {
    byte[] input = null;
    URLConnection conn = null;
    Date retryAfter = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    try {
        conn = ProxyHelper.openConnection(url);
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        int responseCode = httpConn.getResponseCode();
        switch (responseCode) {
        case 503:
            String retryAfterHeader = conn.getHeaderField("Retry-After");
            if (retryAfterHeader != null) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
                retryAfter = dateFormat.parse(retryAfterHeader);
                this.logger.debug("Source responded with 503, retry after " + retryAfter + ".");
                throw new SourceNotAvailableException(retryAfter);
            }
            break;
        case 302:
            String alternativeLocation = conn.getHeaderField("Location");
            return fetchMetadatafromURL(new URL(alternativeLocation));
        case 200:
            this.logger.info("Source responded with 200.");
            // Fetch file
            GetMethod method = new GetMethod(url.toString());
            HttpClient client = new HttpClient();
            ProxyHelper.executeMethod(client, method);
            input = method.getResponseBody();
            httpConn.disconnect();
            // Create zip file with fetched file
            ZipEntry ze = new ZipEntry("unapi");
            ze.setSize(input.length);
            ze.setTime(this.currentDate());
            CRC32 crc321 = new CRC32();
            crc321.update(input);
            ze.setCrc(crc321.getValue());
            zos.putNextEntry(ze);
            zos.write(input);
            zos.flush();
            zos.closeEntry();
            zos.close();
            this.setContentType("application/zip");
            this.setFileEnding(".zip");
            break;
        case 403:
            throw new AccessException("Access to url " + url + " is restricted.");
        default:
            throw new RuntimeException("An error occurred during importing from external system: "
                    + responseCode + ": " + httpConn.getResponseMessage() + ".");
        }
    } catch (AccessException e) {
        this.logger.error("Access denied.", e);
        throw new AccessException(url.toString());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return baos.toByteArray();
}

From source file:org.apache.hadoop.raid.TestStatisticsCollector.java

private static long createFile(FileSystem fileSys, Path name, int repl, int numBlocks, long blocksize)
        throws IOException {
    CRC32 crc = new CRC32();
    int bufSize = fileSys.getConf().getInt("io.file.buffer.size", 4096);
    FSDataOutputStream stm = fileSys.create(name, true, bufSize, (short) repl, blocksize);
    // fill random data into file
    byte[] b = new byte[(int) blocksize];
    for (int i = 0; i < numBlocks; i++) {
        rand.nextBytes(b);//from   www. j  av  a 2 s.co m
        stm.write(b);
        crc.update(b);
    }
    stm.close();
    return crc.getValue();
}

From source file:org.apache.isis.objectstore.nosql.db.file.server.FileServer.java

private void syncConnection(final Socket connection, final int readTimeout) {
    try {//from   w  w  w .j  a  v  a2 s . c om
        final CRC32 crc32 = new CRC32();
        final DataOutput output = new DataOutputStream(connection.getOutputStream());
        final DataInput input = new DataInputStream(new CheckedInputStream(connection.getInputStream(), crc32));

        if (input.readByte() != INIT) {
            return;
        }

        final LogRange logFileRange = Util.logFileRange();
        final long lastId = logFileRange.noLogFile() ? -1 : logFileRange.getLast();
        output.writeLong(lastId);
        do {
            if (input.readByte() != RECOVERY_LOG) {
                return;
            }
            crc32.reset();
            final long logId = input.readLong();
            final File file = Util.tmpLogFile(logId);
            LOG.info("syncing recovery file: " + file.getName());
            final BufferedOutputStream fileOutput = new BufferedOutputStream(new FileOutputStream(file));

            final byte[] buffer = new byte[8092];
            int length;
            while ((length = input.readInt()) > 0) {
                input.readFully(buffer, 0, length);
                fileOutput.write(buffer, 0, length);
            }
            fileOutput.close();

            final long calculatedChecksum = crc32.getValue();
            final long sentChecksum = input.readLong();
            if (calculatedChecksum != sentChecksum) {
                throw new NoSqlStoreException("Checksum didn't match during download of " + file.getName());
            }

            recover(file);
            final File renameTo = Util.logFile(logId);
            file.renameTo(renameTo);
        } while (true);
    } catch (final NoSqlStoreException e) {
        LOG.error("file server failure", e);
    } catch (final IOException e) {
        LOG.error("networking failure", e);
    } catch (final RuntimeException e) {
        LOG.error("request failure", e);
    } finally {
        try {
            connection.close();
        } catch (final IOException e) {
            LOG.warn("failure to close connection", e);
        }
    }

    // TODO restart
}