Exemplo n.º 1
0
  @Override
  public void run() {
    File root = new File(Environment.getExternalStorageDirectory(), "library");

    try {
      File target = new File(root, itemPath);
      Adler32 checksum = new Adler32();

      InputStream input = prepareConnection();

      if (input != null) {

        FileOutputStream output = FileUtils.openOutputStream(target);

        byte[] buffer = new byte[4 * 1024];
        int n;
        while (-1 != (n = input.read(buffer))) {
          output.write(buffer, 0, n);
          checksum.update(buffer, 0, n);
        }

        output.flush();
        output.close();
        input.close();
      }

      tracker.track(config, target, checksum.getValue());
    } catch (IOException e) {
      Log.e("DOWNLOAD", "Failed to download", e);
    }

    progressUpdater.increment();
    itemsLeftLatch.countDown();
  }
Exemplo n.º 2
0
  /** @tests java.util.zip.Adler32#getValue() */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "getValue",
      args = {})
  public void test_getValue() {
    // test methods of java.util.zip.getValue()
    Adler32 adl = new Adler32();
    assertEquals(
        "GetValue should return a zero as a result of construction an object of Adler32",
        1,
        adl.getValue());

    adl.reset();
    adl.update(1);
    // System.out.print("value of adl"+adl.getValue());
    // The value of the adl should be 131074
    assertEquals(
        "update(int) failed to update the checksum to the correct value ", 131074, adl.getValue());
    adl.reset();
    assertEquals("reset failed to reset the checksum value to zero", 1, adl.getValue());

    adl.reset();
    adl.update(Integer.MIN_VALUE);
    // System.out.print("value of adl " + adl.getValue());
    // The value of the adl should be 65537
    assertEquals(
        "update(min) failed to update the checksum to the correct value ", 65537L, adl.getValue());
  }
Exemplo n.º 3
0
Arquivo: IOR.java Projeto: Blueash/gcc
 /** Get the hashcode of this IOR. */
 public int hashCode() {
   Adler32 adler = new Adler32();
   if (key != null) adler.update(key);
   if (Internet != null) {
     if (Internet.host != null) adler.update(Internet.host.getBytes());
     adler.update(Internet.port);
   }
   return (int) adler.getValue();
 }
Exemplo n.º 4
0
 /** @tests java.util.zip.Adler32#Adler32() */
 @TestTargetNew(
     level = TestLevel.COMPLETE,
     notes = "",
     method = "Adler32",
     args = {})
 public void test_Constructor() {
   // test method of java.util.zip.Adler32()
   Adler32 adl = new Adler32();
   assertEquals("Constructor of adl32 failed", 1, adl.getValue());
 }
Exemplo n.º 5
0
  /** @tests java.util.zip.Adler32#update(int) */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "update",
      args = {int.class})
  public void test_updateI() {
    // test methods of java.util.zip.update(int)
    Adler32 adl = new Adler32();
    adl.update(1);
    // The value of the adl should be 131074
    assertEquals(
        "update(int) failed to update the checksum to the correct value ", 131074, adl.getValue());

    adl.reset();
    adl.update(Integer.MAX_VALUE);
    // System.out.print("value of adl " + adl.getValue());
    // The value of the adl should be 16777472
    assertEquals(
        "update(max) failed to update the checksum to the correct value ",
        16777472L,
        adl.getValue());

    adl.reset();
    adl.update(Integer.MIN_VALUE);
    // System.out.print("value of adl " + adl.getValue());
    // The value of the adl should be 65537
    assertEquals(
        "update(min) failed to update the checksum to the correct value ", 65537L, adl.getValue());
  }
  /** Test verifies that only the specified region of the buffer is used to compute the checksum. */
  public void test_checksum02() {

    byte[] data = new byte[100];
    r.nextBytes(data);

    Adler32 adler32 = new Adler32();
    adler32.update(data, 10, 90);
    final int expectedChecksum = (int) adler32.getValue();

    assertEquals(expectedChecksum, chk.checksum(ByteBuffer.wrap(data), 10, data.length));
  }
Exemplo n.º 7
0
  /**
   * Compute the {@link Adler32} checksum of the buffer. The position, mark, and limit are unchanged
   * by this operation. The operation is optimized when the buffer is backed by an array.
   *
   * @param buf The buffer.
   * @param pos The starting position.
   * @param limit The limit.
   * @return The checksum.
   * @todo why pass in the pos/lim when [buf] could incorporate them?
   */
  public int checksum(final ByteBuffer buf, final int pos, final int limit) {

    assert buf != null;
    assert pos >= 0;
    assert limit > pos;

    // reset before computing the checksum.
    chk.reset();

    // update the checksum.
    update(buf, pos, limit);

    // The Adler checksum is a 32-bit value.
    return (int) chk.getValue();
  }
Exemplo n.º 8
0
  public int checksum(final IByteArraySlice slice) {

    assert slice != null;

    // reset before computing the checksum.
    chk.reset();

    chk.update(slice.array(), slice.off(), slice.len());

    /*
     * The Adler checksum is a 32-bit value.
     */

    return (int) chk.getValue();
  }
  /**
   * Verify that the computed checksum is the same whether the buffer is backed by an array or not
   * when the checksum is computed for only a region of the buffer (native heap buffer version).
   */
  public void test_checksum05_direct() {

    byte[] data = new byte[100];
    r.nextBytes(data);

    Adler32 adler32 = new Adler32();
    adler32.update(data, 20, 100 - 10 - 20);
    final int expectedChecksum = (int) adler32.getValue();

    assertEquals(expectedChecksum, chk.checksum(ByteBuffer.wrap(data), 20, data.length - 10));

    ByteBuffer direct = ByteBuffer.allocateDirect(data.length);
    direct.put(data);
    assertEquals(expectedChecksum, chk.checksum(direct, 20, data.length - 10));
  }
Exemplo n.º 10
0
  public int checksum(final byte[] buf, int off, int sze) {

    assert buf != null;

    // reset before computing the checksum.
    chk.reset();

    chk.update(buf, off, sze);

    /*
     * The Adler checksum is a 32-bit value.
     */

    return (int) chk.getValue();
  }
Exemplo n.º 11
0
 public static String calculateCrc32(String source) {
   CRC32 crc32 = new CRC32();
   Adler32 adler32 = new Adler32();
   adler32.update(source.getBytes());
   System.out.println(adler32.getValue());
   crc32.update(source.getBytes());
   StringBuilder builder = new StringBuilder(8);
   System.out.println(crc32.getValue());
   System.out.println(Long.toHexString(crc32.getValue()));
   builder.append(Long.toHexString(crc32.getValue()));
   if (builder.length() < 8) {
     builder.append(CHAR_TEMPLATE, 0, 8 - builder.length());
   }
   System.out.println(builder.toString());
   return builder.toString();
 }
Exemplo n.º 12
0
 /** @tests java.util.zip.Adler32#reset() */
 @TestTargetNew(
     level = TestLevel.COMPLETE,
     notes = "",
     method = "reset",
     args = {})
 public void test_reset() {
   // test methods of java.util.zip.reset()
   Adler32 adl = new Adler32();
   adl.update(1);
   // System.out.print("value of adl"+adl.getValue());
   // The value of the adl should be 131074
   assertEquals(
       "update(int) failed to update the checksum to the correct value ", 131074, adl.getValue());
   adl.reset();
   assertEquals("reset failed to reset the checksum value to zero", 1, adl.getValue());
 }
Exemplo n.º 13
0
    @Override
    public boolean onStdinReady(ByteBuffer buffer) {
      writeAdler32.update(bytes);

      buffer.put(bytes);
      buffer.flip();

      return (++writes < WRITES);
    }
Exemplo n.º 14
0
    @Override
    public void onStdout(ByteBuffer buffer, boolean closed) {
      size += buffer.remaining();
      if (size == (WRITES * bytes.length)) {
        nuProcess.closeStdin();
      }

      byte[] bytes = new byte[buffer.remaining()];
      buffer.get(bytes);
      readAdler32.update(bytes);
    }
Exemplo n.º 15
0
 static long a(m paramm)
 {
   if (paramm != null)
   {
     Object[] arrayOfObject = new Object[5];
     arrayOfObject[0] = paramm.f();
     arrayOfObject[1] = paramm.e();
     arrayOfObject[2] = Long.valueOf(paramm.b());
     arrayOfObject[3] = paramm.d();
     arrayOfObject[4] = paramm.c();
     String str = String.format("%s%s%s%s%s", arrayOfObject);
     if (!f.a(str))
     {
       Adler32 localAdler32 = new Adler32();
       localAdler32.reset();
       localAdler32.update(str.getBytes());
       return localAdler32.getValue();
     }
   }
   return 0L;
 }
Exemplo n.º 16
0
 static long getMetadataCheckSum(Device paramDevice) {
   if (paramDevice != null) {
     paramDevice =
         String.format(
             "%s%s%s%s%s",
             new Object[] {
               paramDevice.getUtdid(),
               paramDevice.getDeviceId(),
               Long.valueOf(paramDevice.getCreateTimestamp()),
               paramDevice.getImsi(),
               paramDevice.getImei()
             });
     if (!StringUtils.isEmpty(paramDevice)) {
       Adler32 localAdler32 = new Adler32();
       localAdler32.reset();
       localAdler32.update(paramDevice.getBytes());
       return localAdler32.getValue();
     }
   }
   return 0L;
 }
Exemplo n.º 17
0
  /** @tests java.util.zip.Adler32#update(byte[]) */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "update",
      args = {byte[].class})
  public void test_update$B() {
    // test method of java.util.zip.update(byte[])
    byte byteArray[] = {1, 2};
    Adler32 adl = new Adler32();
    adl.update(byteArray);
    // System.out.print("value of adl"+adl.getValue());
    // The value of the adl should be 393220
    assertEquals(
        "update(byte[]) failed to update the checksum to the correct value ",
        393220,
        adl.getValue());

    adl.reset();
    byte byteEmpty[] = new byte[10000];
    adl.update(byteEmpty);
    // System.out.print("value of adl"+adl.getValue());
    // The value of the adl should be 655360001
    assertEquals(
        "update(byte[]) failed to update the checksum to the correct value ",
        655360001L,
        adl.getValue());
  }
Exemplo n.º 18
0
  /** @tests java.util.zip.Adler32#update(byte[], int, int) */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "update",
      args = {byte[].class, int.class, int.class})
  public void test_update$BII() {
    // test methods of java.util.zip.update(byte[],int,int)
    byte[] byteArray = {1, 2, 3};
    Adler32 adl = new Adler32();
    int off = 2; // accessing the 2nd element of byteArray
    int len = 1;
    int lenError = 3;
    int offError = 4;
    adl.update(byteArray, off, len);
    // System.out.print("value of adl"+adl.getValue());
    // The value of the adl should be 262148
    assertEquals(
        "update(byte[],int,int) failed to update the checksum to the correct value ",
        262148,
        adl.getValue());
    int r = 0;

    try {
      adl.update(byteArray, off, lenError);
    } catch (ArrayIndexOutOfBoundsException e) {
      r = 1;
    }
    assertEquals("update(byte[],int,int) failed b/c lenError>byte[].length-off", 1, r);

    try {
      adl.update(byteArray, offError, len);
    } catch (ArrayIndexOutOfBoundsException e) {
      r = 2;
    }
    assertEquals("update(byte[],int,int) failed b/c offError>byte[].length", 2, r);
  }
Exemplo n.º 19
0
 // Test for issue 1016037
 private void wrongChecksumWithAdler32Test() {
   byte[] bytes = {1, 0, 5, 0, 15, 0, 1, 11, 0, 1};
   Adler32 adler = new Adler32();
   adler.update(bytes);
   long arrayChecksum = adler.getValue();
   adler.reset();
   for (int i = 0; i < bytes.length; i++) {
     adler.update(bytes[i]);
   }
   assertEquals(
       "Checksums not equal: expected: " + arrayChecksum + " actual: " + adler.getValue(),
       arrayChecksum,
       adler.getValue());
 }
Exemplo n.º 20
0
  private void adler32Test(byte[] values, long expected) {
    Adler32 adler = new Adler32();

    // try it all at once
    adler.update(values);
    assertEquals(adler.getValue(), expected);

    // try resetting and computing one byte at a time
    adler.reset();
    for (int i = 0; i < values.length; i++) {
      adler.update(values[i]);
    }
    assertEquals(adler.getValue(), expected);
  }
Exemplo n.º 21
0
 private int adlerHash(byte[] input) {
   Adler32 hasher = new Adler32();
   hasher.update(input);
   return (int) hasher.getValue();
 }
Exemplo n.º 22
0
 boolean checkAdlers() {
   return readAdler32.getValue() == writeAdler32.getValue();
 }
Exemplo n.º 23
0
  /** Reset the checksum. */
  protected void reset() {

    chk.reset();
  }
Exemplo n.º 24
0
  /** Return the Alder checksum, which is a 32bit value. */
  protected int getChecksum() {

    return (int) chk.getValue();
  }
 private static int calcCheckSum(byte[] content) {
   Adler32 checksumCalculator = new Adler32();
   checksumCalculator.update(content);
   return (int) checksumCalculator.getValue();
 }
Exemplo n.º 26
0
  /**
   * Core implementation updates the {@link Adler32} checksum from the data in the buffer. The
   * position, mark, and limit are unchanged by this operation. The operation is optimized when the
   * buffer is backed by an array.
   *
   * @param buf
   * @param pos
   * @param limit
   */
  protected void update(final ByteBuffer buf, final int pos, final int limit) {

    assert buf != null;
    assert pos >= 0;
    assert limit > pos;

    // reset before computing the checksum.
    //        chk.reset();

    if (buf.hasArray()) {

      /*
       * Optimized when the buffer is backed by an array.
       */

      final byte[] bytes = buf.array();

      final int len = limit - pos;

      if (pos > bytes.length - len) {

        throw new BufferUnderflowException();
      }

      chk.update(bytes, pos + buf.arrayOffset(), len);

    } else {

      /*
       * Compute the checksum of a byte[] on the native heap.
       *
       * @todo this should be a JNI call. The Adler32 class is already a
       * JNI implementation.
       */

      if (a == null) {

        a = new byte[512];
      }

      // isolate changes to (pos,limit).
      final ByteBuffer b = buf.asReadOnlyBuffer();

      // stopping point.
      final int m = limit;

      // update checksum a chunk at a time.
      for (int p = pos; p < m; p += a.length) {

        // #of bytes to copy into the local byte[].
        final int len = Math.min(m - p, a.length);

        // set the view
        b.limit(p + len);
        b.position(p);

        // copy into Java heap byte[], advancing b.position().
        b.get(a, 0 /* off */, len);

        // update the running checksum.
        chk.update(a, 0 /* off */, len);
      }

      // This is WAY to expensive since it is a JNI call per byte.
      //
      //            for (int i = pos; i < limit; i++) {
      //
      //                chk.update(buf.get(i));
      //
      //            }

    }
  }
Exemplo n.º 27
0
  public void testCheckIntegrity() throws IOException {
    Directory dir = newDirectory();
    long luceneFileLength = 0;

    try (IndexOutput output = dir.createOutput("lucene_checksum.bin", IOContext.DEFAULT)) {
      int iters = scaledRandomIntBetween(10, 100);
      for (int i = 0; i < iters; i++) {
        BytesRef bytesRef = new BytesRef(TestUtil.randomRealisticUnicodeString(random(), 10, 1024));
        output.writeBytes(bytesRef.bytes, bytesRef.offset, bytesRef.length);
        luceneFileLength += bytesRef.length;
      }
      CodecUtil.writeFooter(output);
      luceneFileLength += CodecUtil.footerLength();
    }

    final Adler32 adler32 = new Adler32();
    long legacyFileLength = 0;
    try (IndexOutput output = dir.createOutput("legacy.bin", IOContext.DEFAULT)) {
      int iters = scaledRandomIntBetween(10, 100);
      for (int i = 0; i < iters; i++) {
        BytesRef bytesRef = new BytesRef(TestUtil.randomRealisticUnicodeString(random(), 10, 1024));
        output.writeBytes(bytesRef.bytes, bytesRef.offset, bytesRef.length);
        adler32.update(bytesRef.bytes, bytesRef.offset, bytesRef.length);
        legacyFileLength += bytesRef.length;
      }
    }
    final long luceneChecksum;
    final long adler32LegacyChecksum = adler32.getValue();
    try (IndexInput indexInput = dir.openInput("lucene_checksum.bin", IOContext.DEFAULT)) {
      assertEquals(luceneFileLength, indexInput.length());
      luceneChecksum = CodecUtil.retrieveChecksum(indexInput);
    }

    { // positive check
      StoreFileMetaData lucene =
          new StoreFileMetaData(
              "lucene_checksum.bin",
              luceneFileLength,
              Store.digestToString(luceneChecksum),
              Version.LUCENE_4_8_0);
      StoreFileMetaData legacy =
          new StoreFileMetaData(
              "legacy.bin", legacyFileLength, Store.digestToString(adler32LegacyChecksum));
      assertTrue(legacy.hasLegacyChecksum());
      assertFalse(lucene.hasLegacyChecksum());
      assertTrue(Store.checkIntegrityNoException(lucene, dir));
      assertTrue(Store.checkIntegrityNoException(legacy, dir));
    }

    { // negative check - wrong checksum
      StoreFileMetaData lucene =
          new StoreFileMetaData(
              "lucene_checksum.bin",
              luceneFileLength,
              Store.digestToString(luceneChecksum + 1),
              Version.LUCENE_4_8_0);
      StoreFileMetaData legacy =
          new StoreFileMetaData(
              "legacy.bin", legacyFileLength, Store.digestToString(adler32LegacyChecksum + 1));
      assertTrue(legacy.hasLegacyChecksum());
      assertFalse(lucene.hasLegacyChecksum());
      assertFalse(Store.checkIntegrityNoException(lucene, dir));
      assertFalse(Store.checkIntegrityNoException(legacy, dir));
    }

    { // negative check - wrong length
      StoreFileMetaData lucene =
          new StoreFileMetaData(
              "lucene_checksum.bin",
              luceneFileLength + 1,
              Store.digestToString(luceneChecksum),
              Version.LUCENE_4_8_0);
      StoreFileMetaData legacy =
          new StoreFileMetaData(
              "legacy.bin", legacyFileLength + 1, Store.digestToString(adler32LegacyChecksum));
      assertTrue(legacy.hasLegacyChecksum());
      assertFalse(lucene.hasLegacyChecksum());
      assertFalse(Store.checkIntegrityNoException(lucene, dir));
      assertFalse(Store.checkIntegrityNoException(legacy, dir));
    }

    { // negative check - wrong file
      StoreFileMetaData lucene =
          new StoreFileMetaData(
              "legacy.bin",
              luceneFileLength,
              Store.digestToString(luceneChecksum),
              Version.LUCENE_4_8_0);
      StoreFileMetaData legacy =
          new StoreFileMetaData(
              "lucene_checksum.bin", legacyFileLength, Store.digestToString(adler32LegacyChecksum));
      assertTrue(legacy.hasLegacyChecksum());
      assertFalse(lucene.hasLegacyChecksum());
      assertFalse(Store.checkIntegrityNoException(lucene, dir));
      assertFalse(Store.checkIntegrityNoException(legacy, dir));
    }
    dir.close();
  }
 static {
   // IOFlavor.forceJDK13();
   Adler32 adler = new Adler32();
   adler.update(goodHeader);
   System.arraycopy(Conversion.uint2bytes(adler.getValue()), 0, goodHeader, 12, 4);
 }