FileDocCategorySizeDatePackage
FileDigest.javaAPI DocExample1271Sun Mar 28 19:07:42 BST 1999None

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");
     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;
    }

    try {
      URL u = new URL(args[0]);
      FileOutputStream out = new FileOutputStream(args[1]);
      copyFileWithDigest(u.openStream(), out);
      out.close();
    }
    catch (MalformedURLException e) {
      System.err.println(args[0] + " is not a URL");
    }
    catch (Exception e) {
      System.err.println(e);
    }