FileDocCategorySizeDatePackage
FileDigest.javaAPI DocExample1121Mon Feb 13 11:59:26 GMT 2006None

FileDigest

public class FileDigest extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidcopyFileWithDigest(java.io.InputStream in, java.io.OutputStream out)

    
     MessageDigest sha = MessageDigest.getInstance("SHA-512");
     DigestOutputStream dout = new DigestOutputStream(out, sha);
     byte[] data = new byte[128];
     while (true) {
       int bytesRead = in.read(data);
       if (bytesRead < 0) break;
       dout.write(data, 0, bytesRead);
     }
     dout.flush();
     byte[] result = dout.getMessageDigest().digest();
     for (int i = 0; i < result.length; i++) {
       System.out.print(result[i] + " ");
     }
     System.out.println();
  
public static voidmain(java.lang.String[] args)


    if (args.length != 2) {
      System.err.println("Usage: java FileDigest url filename");
      return;
    }

    URL u = new URL(args[0]);
    FileOutputStream out = new FileOutputStream(args[1]);
    copyFileWithDigest(u.openStream(), out);
    out.close();