All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kingsoft.services.http.impl.CRC32CalculatingInputStream Maven / Gradle / Ivy

There is a newer version: 0.9.91
Show newest version
package com.kingsoft.services.http.impl;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.CRC32;

/**
 * Simple InputStream wrapper that examines the wrapped stream's contents as
 * they are read and calculates and CRC32 checksum.
 */
public class CRC32CalculatingInputStream extends FilterInputStream {

  /** The CRC32 being calculated by this input stream */
  private CRC32 crc32;

  public CRC32CalculatingInputStream(InputStream in) {
    super(in);
    crc32 = new CRC32();
  }

  public long getCRC32Checksum() {
    return crc32.getValue();
  }

  /**
   * @see java.io.InputStream#read()
   */
  @Override
  public int read() throws IOException {
    int ch = in.read();
    if (ch != -1) {
      crc32.update(ch);
    }
    return ch;
  }

  /**
   * @see java.io.InputStream#read(byte[], int, int)
   */
  @Override
  public int read(byte[] b, int off, int len) throws IOException {
    int result = in.read(b, off, len);
    if (result != -1) {
      crc32.update(b, off, result);
    }
    return result;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy