Example #1
0
 public static boolean checkMd5sum(File file, String checkCode) throws IOException {
   DigestInputStream dInput = null;
   try {
     FileInputStream fInput = new FileInputStream(file);
     dInput = new DigestInputStream(fInput, getMd5Instance());
     byte[] buf = new byte[8192];
     while (dInput.read(buf) > 0) {}
     byte[] bytes = dInput.getMessageDigest().digest();
     return bytes2hex(bytes).equals(checkCode);
   } finally {
     closeQuietly(dInput);
   }
 }
  public static String getMd5(File file) {
    DigestInputStream stream = null;
    try {
      stream =
          new DigestInputStream(
              new java.io.FileInputStream(file), MessageDigest.getInstance("MD5"));
      byte[] buffer = new byte[' '];
      while (stream.read(buffer) != -1) {}
      stream.close();
    } catch (Exception ignored) {
      return null;
    }

    return String.format(
        "%1$032x", new Object[] {new BigInteger(1, stream.getMessageDigest().digest())});
  }
 /**
  * Creates a Fingerprint based on the contents of a file.
  *
  * <p>Note that this will close() stream after calculating the digest.
  *
  * @param byteCount length of original data will be stored at byteCount[0] as a side product of
  *     the fingerprint calculation
  */
 public static Fingerprint fromInputStream(InputStream stream, long[] byteCount)
     throws IOException {
   DigestInputStream in = null;
   long count = 0;
   try {
     in = new DigestInputStream(stream, DIGESTER);
     byte[] bytes = new byte[8192];
     while (true) {
       // scan through file to compute a fingerprint.
       int n = in.read(bytes);
       if (n < 0) break;
       count += n;
     }
   } finally {
     if (in != null) in.close();
   }
   if ((byteCount != null) && (byteCount.length > 0)) byteCount[0] = count;
   return new Fingerprint(in.getMessageDigest().digest());
 }
Example #4
0
 @Override
 public UploadedFileInfo call() throws Exception {
   UploadedFileInfo ufi;
   Path targetFile = createTempFile();
   StopWatch sw = new StopWatch();
   sw.start();
   try {
     LOGGER.debug(
         "Saving {} ({}) Expected MD5:{}...",
         targetFile.toString(),
         Util.byteCountToDisplaySize(expectedLength),
         expectedMd5);
     this.actualLength = saveStreamToFile(this.dis, targetFile);
     this.actualMd5 = Hex.encodeHexString(dis.getMessageDigest().digest());
     verifyExpecteds();
     ufi = new UploadedFileInfo(targetFile, this.actualLength, this.actualMd5);
     sw.stop();
     LOGGER.debug(
         "Saved {} ({}) Computed MD5:{}, Time: {}, Speed: {}",
         ufi.getFilepath().toString(),
         Util.byteCountToDisplaySize(ufi.getSize()),
         ufi.getMd5(),
         sw.getTimeElapsedFormatted(),
         sw.getRate(ufi.getSize()));
   } catch (Exception e) {
     LOGGER.error(
         "Error saving {} ({} bytes) Expected MD5:{} - {}",
         targetFile.toString(),
         this.expectedLength,
         this.expectedMd5,
         e.getMessage());
     throw e;
   } finally {
     IOUtils.closeQuietly(dis);
   }
   return ufi;
 }