Package java.util.zip

Examples of java.util.zip.CRC32


  private static Random rnd = new Random();
  final static Map<String, Byte> test = Maps.newHashMap();
  private static int errors;

  public long crc(String str) {
    final Checksum checksum = new CRC32();

    final byte bytes[] = str.getBytes();
    checksum.update(bytes,0,bytes.length);
    return checksum.getValue();
  }
View Full Code Here


          dos.write(crcLengthBytes);

        if (checkCRCs) {
          long readCRC = (((crcLengthBytes[0] & 0xff) << 24) + ((crcLengthBytes[1] & 0xff) << 16)
                  + ((crcLengthBytes[2] & 0xff) << 8) + (crcLengthBytes[3] & 0xff)) & 0x00000000ffffffffL;
          CRC32 crc = new CRC32();
          crc.update(chunkTypeBytes);
          if(length > 0)
                                            crc.update(chunkData);
          long computedCRC = crc.getValue();

          if (readCRC != computedCRC) {
            skip = true;
            if (logMINOR)
              Logger.minor(this, "CRC of the chunk " + chunkTypeString + " doesn't match ("
View Full Code Here

        return 4;
    }

    @Override
    public OutputStream checksumWriter(OutputStream os, int prefix) {
        return new ChecksumOutputStream(os, new CRC32(), true, prefix);
    }
View Full Code Here

    }

    @Override
    public boolean checkChecksum(byte[] data, int offset, int length, byte[] checksum) {
        if(checksum.length != 4) throw new IllegalArgumentException();
        CRC32 crc = new CRC32();
        crc.update(data, offset, length);
        int computed = (int)crc.getValue();
        int stored = Fields.bytesToInt(checksum);
        return computed == stored;
    }
View Full Code Here

    @Override
    public byte[] appendChecksum(byte[] data) {
        byte[] output = new byte[data.length+4];
        System.arraycopy(data, 0, output, 0, data.length);
        Checksum crc = new CRC32();
        crc.update(data, 0, data.length);
        byte[] checksum = Fields.intToBytes((int)crc.getValue());
        System.arraycopy(checksum, 0, output, data.length, 4);
        return output;
    }
View Full Code Here

        return output;
    }

    @Override
    public byte[] generateChecksum(byte[] data, int offset, int length) {
        Checksum crc = new CRC32();
        crc.update(data, offset, length);
        return Fields.intToBytes((int)crc.getValue());
    }
View Full Code Here

    }

    @Override
    public void copyAndStripChecksum(InputStream is, OutputStream destination, long length) throws IOException, ChecksumFailedException {
        // FIXME refactor via a base class for ChecksumOutputStream.
        CRC32 crc = new CRC32();
        long remaining = length;
        byte[] buffer = new byte[FileUtil.BUFFER_SIZE];
        int read = 0;
        DataInputStream source = new DataInputStream(is);
        while ((remaining == -1) || (remaining > 0)) {
            read = source.read(buffer, 0, ((remaining > FileUtil.BUFFER_SIZE) || (remaining == -1)) ? FileUtil.BUFFER_SIZE : (int) remaining);
            if (read == -1) {
                if (length == -1) {
                    return;
                }
                throw new EOFException("stream reached eof");
            }
            if(read == 0) throw new IOException("stream returning 0 bytes");
            if(read != 0)
                crc.update(buffer, 0, read);
            destination.write(buffer, 0, read);
            if (remaining > 0)
                remaining -= read;
        }
        byte[] checksum = new byte[checksumLength()];
        source.readFully(checksum);
        byte[] myChecksum = Fields.intToBytes((int)crc.getValue());
        if(!Arrays.equals(checksum, myChecksum))
            throw new ChecksumFailedException();
    }
View Full Code Here

        if (LOG.isDebugEnabled()) {
            LOG.debug(MessageFormat.format(
                    "Computing checksum: {0}",
                    file));
        }
        Checksum checksum = new CRC32();
        byte[] buf = byteBuffers.get();
        FSDataInputStream input = fs.open(file);
        try {
            while (true) {
                int read = input.read(buf);
                if (read < 0) {
                    break;
                }
                checksum.update(buf, 0, read);
            }
        } finally {
            input.close();
        }
        return checksum.getValue();
    }
View Full Code Here

   
    /**
     * Create a new PNG image data that can read image data from PNG formated files
     */
    public PNGImageData() {
        this.crc = new CRC32();
        this.buffer = new byte[4096];
    }
View Full Code Here

    if (cnode != null) { cnode.stop(); cnode.join(); }
    if (dfs != null) { dfs.shutdown(); }
  }

  private long getCRC(FileSystem fs, Path p) throws IOException {
    CRC32 crc = new CRC32();
    FSDataInputStream stm = fs.open(p);
    for (int b = 0; b > 0; b = stm.read()) {
      crc.update(b);
    }
    stm.close();
    return crc.getValue();
  }
View Full Code Here

TOP

Related Classes of java.util.zip.CRC32

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.