@Override
 public synchronized void reset() throws IOException {
   log.trace("Resetting {}", in);
   super.reset();
   totalBytesRead = 0L;
   for (Checksum checksum : checksums) {
     checksum.reset();
   }
 }
 public final void update(int i) {
   int b0 = (i >> 24) & 0xff;
   int b1 = (i >> 16) & 0xff;
   int b2 = (i >> 8) & 0xff;
   int b3 = i & 0xff;
   crc.update(b0);
   crc.update(b1);
   crc.update(b2);
   crc.update(b3);
   //		com.oddlabs.tt.util.ChecksumLogger.log(i);
 }
 @Override
 public int read(byte b[], int off, int len) throws IOException {
   int bytesRead = super.read(b, off, len);
   log.trace("{} bytes read from {}", bytesRead, in);
   if (bytesRead != -1) {
     totalBytesRead += bytesRead;
     for (Checksum checksum : checksums) {
       checksum.update(b, off, bytesRead);
     }
   }
   return bytesRead;
 }
 @Override
 public void close() throws IOException {
   super.close();
   if (!closed) {
     log.trace("Total bytes read: {}", totalBytesRead);
     for (Checksum checksum : checksums) {
       checksum.calc();
       log.trace("Calculated checksum: '{}:{}'", checksum.getType(), checksum.getChecksum());
     }
     closed = true;
   }
 }
 public static byte calcCrcSum8(byte b[]) {
   if (!ListTools.isEmpty(b)) {
     return Checksum.calcCrcSum8(b, 0, b.length);
   } else {
     return (byte) 0;
   }
 }
 public static long calcCrc32(byte b[]) {
   if (!ListTools.isEmpty(b)) {
     return Checksum.calcCrc32(b, 0, b.length);
   } else {
     return 0L;
   }
 }
 public static int calcCrc16_1(byte b[]) {
   if (!ListTools.isEmpty(b)) {
     return Checksum.calcCrc16_1(b, 0, b.length);
   } else {
     return 0;
   }
 }
 /**
  * Reads up to {@code byteCount} bytes of data from the underlying input stream, storing it into
  * {@code buffer}, starting at offset {@code byteOffset}. The checksum is updated with the bytes
  * read. Returns the number of bytes actually read or {@code -1} if arrived at the end of the
  * filtered stream while reading the data.
  *
  * @throws IOException if this stream is closed or some I/O error occurs.
  */
 @Override
 public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
   int bytesRead = in.read(buffer, byteOffset, byteCount);
   if (bytesRead != -1) {
     check.update(buffer, byteOffset, bytesRead);
   }
   return bytesRead;
 }
 /**
  * Reads one byte of data from the underlying input stream and updates the checksum with the byte
  * data.
  *
  * @return {@code -1} at the end of the stream, a single byte value otherwise.
  * @throws IOException if an {@code IOException} occurs.
  */
 @Override
 public int read() throws IOException {
   int x = in.read();
   if (x != -1) {
     check.update(x);
   }
   return x;
 }
 /**
  * Reads up to n bytes of data from the underlying input stream, storing it into {@code buf},
  * starting at offset {@code off}. The checksum is updated with the bytes read.
  *
  * @param buf the byte array in which to store the bytes read.
  * @param off the initial position in {@code buf} to store the bytes read from this stream.
  * @param nbytes the maximum number of bytes to store in {@code buf}.
  * @return the number of bytes actually read or {@code -1} if arrived at the end of the filtered
  *     stream while reading the data.
  * @throws IOException if this stream is closed or some I/O error occurs.
  */
 @Override
 public int read(byte[] buf, int off, int nbytes) throws IOException {
   int x = in.read(buf, off, nbytes);
   if (x != -1) {
     check.update(buf, off, x);
   }
   return x;
 }
 /**
  * Skip up to n bytes of data on the underlying input stream. Any skipped bytes are added to the
  * running checksum value.
  *
  * @param nbytes the number of bytes to skip.
  * @throws IOException if this stream is closed or another I/O error occurs.
  * @return the number of bytes skipped.
  */
 @Override
 public long skip(long nbytes) throws IOException {
   if (nbytes < 1) {
     return 0;
   }
   long skipped = 0;
   byte[] b = new byte[2048];
   int x, v;
   while (skipped != nbytes) {
     x = in.read(b, 0, (v = (int) (nbytes - skipped)) > b.length ? b.length : v);
     if (x == -1) {
       return skipped;
     }
     check.update(b, 0, x);
     skipped += x;
   }
   return skipped;
 }
Beispiel #12
0
  public static int calcCrcCCITT(byte b[], int bOfs, int bLen) {
    int W = 0xFFFF;
    if (b != null) {

      /* initialize CRC table */
      if (crc_CCITT_Table == null) {
        Checksum.initCrcCCITT();
      }

      /* adjust offset/length */
      int ofs = (bOfs <= 0) ? 0 : (bOfs >= b.length) ? b.length : bOfs;
      int len = ((bLen >= 0) && (bLen <= (b.length - ofs))) ? bLen : (b.length - ofs);

      /* calc CRC */
      for (int i = 0; i < len; i++) {
        W = (crc_CCITT_Table[(b[i + ofs] ^ (W >>> 8)) & 0xFF] ^ (W << 8)) & 0xFFFF;
      }
    }
    return W;
  }
Beispiel #13
0
  @Test
  public void testChecksum() {
    for (String test : SAMPLE_DATA) {
      byte[] src = test.getBytes();
      String encoded = Checksum.encode(src);
      byte[] decoded = Checksum.decode(encoded);

      Assert.assertTrue(
          String.format("String(%s) decoded result not equals!", src), Arrays.equals(src, decoded));
    }

    for (String test : SAMPLE_DATA) {
      byte[] src = test.getBytes();
      String ck = Checksum.checksum(src);

      Assert.assertTrue(
          String.format("String(%s) checksum validation falied!", ck),
          Checksum.validateChecksum(ck));
    }

    for (int i = 0; i < 10000; ++i) {
      int size = (int) (Math.random() * 1000);
      byte[] buf = new byte[size];
      for (int j = 0; j < size; ++j) {
        buf[j] = (byte) (Math.random() * 256);
      }

      String ck = Checksum.checksum(buf);

      Assert.assertTrue(
          String.format("String(%s) checksum validation falied!", ck),
          Checksum.validateChecksum(ck));
    }
    Assert.assertTrue(Checksum.decode(null).length == 0);
    Assert.assertFalse(Checksum.validateChecksum(null));
    Assert.assertFalse(Checksum.validateChecksum("abc"));
    Assert.assertFalse(Checksum.validateChecksum("abcabcabcabcabcabcabcabcab!"));
    Assert.assertFalse(Checksum.validateChecksum("abcabcabcabcabcabcabcabcabc"));
  }
Beispiel #14
0
 public final int getValue() {
   return (int) crc.getValue();
 }
Beispiel #15
0
 /* startup init */
 static {
   Checksum.initCrcCCITT();
 }