Example usage for java.util.zip CRC32 update

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

Introduction

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

Prototype

@Override
public void update(ByteBuffer buffer) 

Source Link

Document

Updates the CRC-32 checksum with the bytes from the specified buffer.

Usage

From source file:Main.java

public static long getStringCRC(String localData) {
    if (localData == null)
        return 0;
    CRC32 crc = new CRC32();
    crc.update(localData.getBytes());
    return crc.getValue();
}

From source file:com.esri.geoportal.harvester.api.base.SimpleScrambler.java

/**
 * Encodes string./*  w  w w . j  a v  a  2  s  . com*/
 * @param txt string to encode
 * @return encoded string or <code>null</code> if error encoding string
 */
public static String encode(String txt) {
    txt = StringUtils.defaultIfEmpty(txt, "");
    try {
        CRC32 crC32 = new CRC32();
        crC32.update(txt.getBytes("UTF-8"));
        long crc = crC32.getValue();
        String crctxt = String.format("%10d%s", crc, txt);
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(crctxt.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException ex) {
        return null;
    }
}

From source file:com.esri.geoportal.harvester.api.base.SimpleScrambler.java

/**
 * Decodes string.//  w  w  w  .jav  a2s  .  c o  m
 * @param encoded encoded string to decode
 * @return decoded string or <code>null</code> if error decoding string
 */
public static String decode(String encoded) {
    try {
        encoded = StringUtils.defaultIfEmpty(encoded, "");
        Base64.Decoder decoder = Base64.getDecoder();
        String crctxt = new String(decoder.decode(encoded), "UTF-8");
        if (crctxt.length() < 10) {
            return null;
        }
        long crc = Long.parseLong(StringUtils.trimToEmpty(crctxt.substring(0, 10)));
        String txt = crctxt.substring(10);
        CRC32 crC32 = new CRC32();
        crC32.update(txt.getBytes("UTF-8"));
        if (crc != crC32.getValue()) {
            return null;
        }
        return txt;
    } catch (NumberFormatException | UnsupportedEncodingException ex) {
        return null;
    }
}

From source file:org.apache.tika.server.writer.ZipWriter.java

private static void zipStoreBuffer(ZipArchiveOutputStream zip, String name, byte[] dataBuffer)
        throws IOException {
    ZipEntry zipEntry = new ZipEntry(name != null ? name : UUID.randomUUID().toString());
    zipEntry.setMethod(ZipOutputStream.STORED);

    zipEntry.setSize(dataBuffer.length);
    CRC32 crc32 = new CRC32();
    crc32.update(dataBuffer);
    zipEntry.setCrc(crc32.getValue());//  w w  w.java 2 s  . c o  m

    try {
        zip.putArchiveEntry(new ZipArchiveEntry(zipEntry));
    } catch (ZipException ex) {
        if (name != null) {
            zipStoreBuffer(zip, "x-" + name, dataBuffer);
            return;
        }
    }

    zip.write(dataBuffer);

    zip.closeArchiveEntry();
}

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  av  a2 s .c  om
 * @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:com.haulmont.cuba.core.sys.logging.LogArchiver.java

private static ArchiveEntry newTailArchive(String name, byte[] tail) {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(name);
    zipEntry.setSize(tail.length);//from  w ww  .ja  va 2s.  c  o m
    zipEntry.setCompressedSize(zipEntry.getSize());
    CRC32 crc32 = new CRC32();
    crc32.update(tail);
    zipEntry.setCrc(crc32.getValue());
    return zipEntry;
}

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

/**
 * Partition an identifier into a directory structure
 * @param identifier the identifier/*from   w  w w .j a  v  a  2s . c  om*/
 * @return a string usable as a directory path
 */
public static String partition(final String identifier) {
    requireNonNull(identifier, "identifier must not be null!");

    final StringJoiner joiner = new StringJoiner(separator);
    final CRC32 hasher = new CRC32();
    hasher.update(identifier.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(identifier));
    return joiner.toString();
}

From source file:com.hortonworks.registries.storage.tool.shell.ShellMigrationResolver.java

/**
 * Calculates the checksum of these bytes.
 *
 * @param bytes The bytes to calculate the checksum for.
 * @return The crc-32 checksum of the bytes.
 *//* w  w w .j a  v a2 s  .com*/
private static int calculateChecksum(byte[] bytes) {
    final CRC32 crc32 = new CRC32();
    crc32.update(bytes);
    return (int) crc32.getValue();
}

From source file:org.game.cs.core.condenser.steam.packets.SteamPacketFactory.java

/**
 * Reassembles the data of a split and/or compressed packet into a single
 * packet object//from  w w  w.j  av a  2  s  . co  m
 *
 * @param splitPackets An array of packet data
 * @param isCompressed whether the data of this packet is compressed
 * @param uncompressedSize The size of the decompressed packet data
 * @param packetChecksum The CRC32 checksum of the decompressed
 *        packet data
 * @throws SteamCondenserException if decompressing the packet data fails
 * @throws PacketFormatException if the calculated CRC32 checksum does not
 *         match the expected value
 * @return SteamPacket The reassembled packet
 * @see SteamPacketFactory#getPacketFromData
 */
public static SteamPacket reassemblePacket(ArrayList<byte[]> splitPackets, boolean isCompressed,
        int uncompressedSize, int packetChecksum) throws SteamCondenserException {
    byte[] packetData, tmpData;
    packetData = new byte[0];

    for (byte[] splitPacket : splitPackets) {
        tmpData = packetData;
        packetData = new byte[tmpData.length + splitPacket.length];
        System.arraycopy(tmpData, 0, packetData, 0, tmpData.length);
        System.arraycopy(splitPacket, 0, packetData, tmpData.length, splitPacket.length);
    }

    if (isCompressed) {
        try {
            ByteArrayInputStream stream = new ByteArrayInputStream(packetData);
            stream.read();
            stream.read();
            BZip2CompressorInputStream bzip2 = new BZip2CompressorInputStream(stream);
            byte[] uncompressedPacketData = new byte[uncompressedSize];
            bzip2.read(uncompressedPacketData, 0, uncompressedSize);

            CRC32 crc32 = new CRC32();
            crc32.update(uncompressedPacketData);
            int crc32checksum = (int) crc32.getValue();

            if (crc32checksum != packetChecksum) {
                throw new PacketFormatException("CRC32 checksum mismatch of uncompressed packet data.");
            }
            packetData = uncompressedPacketData;
        } catch (IOException e) {
            throw new SteamCondenserException(e.getMessage(), e);
        }
    }

    packetData = new String(packetData).substring(4).getBytes();

    return SteamPacketFactory.getPacketFromData(packetData);
}

From source file:com.github.koraktor.steamcondenser.servers.packets.SteamPacketFactory.java

/**
 * Reassembles the data of a split and/or compressed packet into a single
 * packet object/*w  w  w . ja va2  s  .c  om*/
 *
 * @param splitPackets An array of packet data
 * @param isCompressed whether the data of this packet is compressed
 * @param uncompressedSize The size of the decompressed packet data
 * @param packetChecksum The CRC32 checksum of the decompressed
 *        packet data
 * @throws SteamCondenserException if decompressing the packet data fails
 * @throws PacketFormatException if the calculated CRC32 checksum does not
 *         match the expected value
 * @return SteamPacket The reassembled packet
 * @see SteamPacketFactory#getPacketFromData
 */
public static SteamPacket reassemblePacket(ArrayList<byte[]> splitPackets, boolean isCompressed,
        int uncompressedSize, int packetChecksum) throws SteamCondenserException {
    byte[] packetData, tmpData;
    packetData = new byte[0];

    for (byte[] splitPacket : splitPackets) {
        tmpData = packetData;
        packetData = new byte[tmpData.length + splitPacket.length];
        System.arraycopy(tmpData, 0, packetData, 0, tmpData.length);
        System.arraycopy(splitPacket, 0, packetData, tmpData.length, splitPacket.length);
    }

    if (isCompressed) {
        try {
            ByteArrayInputStream stream = new ByteArrayInputStream(packetData);
            stream.read();
            stream.read();
            BZip2CompressorInputStream bzip2 = new BZip2CompressorInputStream(stream);
            byte[] uncompressedPacketData = new byte[uncompressedSize];
            bzip2.read(uncompressedPacketData, 0, uncompressedSize);

            CRC32 crc32 = new CRC32();
            crc32.update(uncompressedPacketData);
            int crc32checksum = (int) crc32.getValue();

            if (crc32checksum != packetChecksum) {
                throw new PacketFormatException("CRC32 checksum mismatch of uncompressed packet data.");
            }
            packetData = uncompressedPacketData;
        } catch (IOException e) {
            throw new SteamCondenserException(e.getMessage(), e);
        }
    }

    tmpData = packetData;
    packetData = new byte[tmpData.length - 4];
    System.arraycopy(tmpData, 4, packetData, 0, tmpData.length - 4);

    return SteamPacketFactory.getPacketFromData(packetData);
}