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:divconq.ctp.stream.UngzipStream.java

public UngzipStream() {
    this.inflater = new Inflater(true);
    this.crc = new CRC32();
}

From source file:org.tangram.components.CodeExporter.java

@LinkAction("/codes.zip")
public TargetDescriptor codes(HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (!request.getRequestURI().endsWith(".zip")) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return null;
    } // if/* w  w w  . ja  v a2  s  .c om*/
    if (request.getAttribute(Constants.ATTRIBUTE_ADMIN_USER) == null) {
        throw new IOException("User may not execute action");
    } // if

    long now = System.currentTimeMillis();

    response.setContentType("application/x-zip-compressed");

    CRC32 crc = new CRC32();

    ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
    zos.setComment("Tangram Repository Codes");
    zos.setLevel(9);
    Collection<CodeResource> codes = codeResourceCache.getCodes();
    for (CodeResource code : codes) {
        if (StringUtils.isNotBlank(code.getAnnotation())) {
            String mimeType = CodeHelper.getNormalizedMimeType(code.getMimeType());
            String folder = CodeHelper.getFolder(mimeType);
            String extension = CodeHelper.getExtension(mimeType);
            if (mimeType.startsWith("text/")) {
                byte[] bytes = code.getCodeText().getBytes("UTF-8");
                ZipEntry ze = new ZipEntry(folder + "/" + getFilename(code) + extension);
                ze.setTime(now);
                crc.reset();
                crc.update(bytes);
                ze.setCrc(crc.getValue());
                zos.putNextEntry(ze);
                zos.write(bytes);
                zos.closeEntry();
            } // if
        } // if
    } // for
    zos.finish();
    zos.close();

    return TargetDescriptor.DONE;
}

From source file:org.bdval.util.ShortHash.java

/**
 * Return a short hash (String of 5 chars, A-Z) of the contents of toHash.
 * @param toHash the content to hash/*from  w ww  .j a  v  a 2s .  com*/
 * @return the short hash
 */
public static String shortHash(final String toHash) {
    if (StringUtils.isBlank(toHash)) {
        return null;
    }

    // Get the CRC32 checksum of the string (CRC will clash less often than the Adler checksum for short strings)
    final CRC32 crc32 = new CRC32();
    crc32.update(toHash.getBytes());
    // Map it from a long to an int with mod
    final int checksum = (int) (crc32.getValue() % Integer.MAX_VALUE);

    final StringBuilder output = new StringBuilder();
    for (int i = 0; i < MASKS.length; i++) {
        // Mask the value, shift it to the right, and mod it to the output-able characters
        final int partial = ((checksum & MASKS[i]) >> MASK_SHIFTS[i]) % HASH_CHARS.length;
        final char asChar = HASH_CHARS[partial];
        output.append(asChar);
    }
    LOG.debug(String.format("hash=%s for string=%s", output.toString(), toHash));
    return output.toString();
}

From source file:org.trellisldp.file.FileUtils.java

/**
 * Get a directory for a given resource identifier.
 * @param baseDirectory the base directory
 * @param identifier a resource identifier
 * @return a directory//from w w  w.  j  a  va 2 s  .c o  m
 */
public static File getResourceDirectory(final File baseDirectory, final IRI identifier) {
    requireNonNull(baseDirectory, "The baseDirectory may not be null!");
    requireNonNull(identifier, "The identifier may not be null!");
    final String id = identifier.getIRIString();
    final StringJoiner joiner = new StringJoiner(separator);
    final CRC32 hasher = new CRC32();
    hasher.update(id.getBytes(UTF_8));
    final String intermediate = Long.toHexString(hasher.getValue());

    range(0, intermediate.length() / LENGTH).limit(MAX)
            .forEach(i -> joiner.add(intermediate.substring(i * LENGTH, (i + 1) * LENGTH)));

    joiner.add(md5Hex(id));
    return new File(baseDirectory, joiner.toString());
}

From source file:MainClass.java

public CRC() {
    super("CRC");
    crc = new CRC32();
}

From source file:com.petercho.Encoder.java

public static long checksumCRC32(String text) {
    CRC32 crc = new CRC32();
    try (InputStream is = new ByteArrayInputStream(text.getBytes(DEFAULT_ENCODING))) {
        checksum(is, crc);/*w  ww .j  a  va 2s . c  om*/
    } catch (IOException e) {
        throw new IllegalStateException("Error getting checksum, e:" + e, e);
    }
    return crc.getValue();
}

From source file:com.yifanlu.PSXperiaTool.ZpakCreate.java

public static long getCRC32(File file) throws IOException {
    CheckedInputStream cis = null;
    long fileSize = 0;
    // Computer CRC32 checksum
    cis = new CheckedInputStream(new FileInputStream(file), new CRC32());

    fileSize = file.length();//from w  ww .j a v a2  s. c  om

    byte[] buf = new byte[128];
    while (cis.read(buf) != -1)
        ;

    long checksum = cis.getChecksum().getValue();
    cis.close();
    Logger.verbose("CRC32 of %s is %d", file.getPath(), checksum);
    return checksum;
}

From source file:org.apache.hadoop.hdfs.RaidDFSUtil.java

public static long getCRC(FileSystem fs, Path p) throws IOException {
    CRC32 crc = new CRC32();
    FSDataInputStream stm = fs.open(p);/*from w  ww .  ja va  2s.c o m*/
    int b;
    while ((b = stm.read()) >= 0) {
        crc.update(b);
    }
    stm.close();
    return crc.getValue();
}

From source file:org.ambiance.codec.YEncEncoder.java

public void encode(File input) throws EncoderException {
    try {//w ww .  ja va 2  s  . c o  m
        // Initialize
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(input));

        YEncHeader header = new YEncHeader();
        header.setName(input.getName());
        header.setLine(lineSize);

        YEncTrailer trailer = new YEncTrailer();

        CRC32 crc32 = new CRC32();

        StringBuffer outputName = new StringBuffer(outputDirName);
        outputName.append(File.separator);
        outputName.append(header.getName());
        outputName.append(extension);

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outputName.toString()));

        // Read and encode
        int c;
        long size = 0;
        while ((c = bis.read()) != -1) {
            crc32.update(c);
            if (size % header.getLine() == 0 && size != 0) {
                bos.write((int) '\r');
                bos.write((int) '\n');
            }
            size++;
        }
    } catch (FileNotFoundException e) {
        throw new EncoderException("Unable to find file to encode");
    } catch (IOException e) {
        throw new EncoderException("Unable to read file to encode");
    }

}

From source file:PNGDecoder.java

protected static boolean verifyCRC(byte[] typeBytes, byte[] data, long crc) {
    CRC32 crc32 = new CRC32();
    crc32.update(typeBytes);/* www  .  j a v a 2 s  . c om*/
    crc32.update(data);
    long calculated = crc32.getValue();
    return (calculated == crc);
}