Author: tchemit Date: 2009-07-11 14:07:21 +0200 (Sat, 11 Jul 2009) New Revision: 1667 Modified: trunk/src/main/java/org/nuiton/util/MD5InputStream.java Log: push back some missing codes Modified: trunk/src/main/java/org/nuiton/util/MD5InputStream.java =================================================================== --- trunk/src/main/java/org/nuiton/util/MD5InputStream.java 2009-07-06 20:42:06 UTC (rev 1666) +++ trunk/src/main/java/org/nuiton/util/MD5InputStream.java 2009-07-11 12:07:21 UTC (rev 1667) @@ -53,135 +53,140 @@ * @author Santeri Paavolainen <santtu@cs.hut.fi> * @author Timothy W Macinta (twm@alum.mit.edu) (added main() method) **/ - - public class MD5InputStream extends FilterInputStream { - /** - * MD5 context - */ - private MD5 md5; - - /** - * Creates a MD5InputStream - * @param in The input stream - */ - public MD5InputStream (InputStream in) { - super(in); + /** Class logger. */ + private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(MD5InputStream.class); + /** + * MD5 context + */ + protected MD5 md5; + protected long streamLength; - md5 = new MD5(); - } + /** + * Creates a MD5InputStream + * @param in The input stream + */ + public MD5InputStream(InputStream in) { + super(in); - /** - * Read a byte of data. - * @see java.io.FilterInputStream - */ - public int read() throws IOException { - int c = in.read(); - - if (c == -1) - return -1; - - if ((c & ~0xff) != 0) { - System.out.println("MD5InputStream.read() got character with (c & ~0xff) != 0)!"); - } else { - md5.Update(c); + md5 = new MD5(); } - return c; - } + @Override + public int read() throws IOException { + int c = in.read(); - /** - * Reads into an array of bytes. - * - * @see java.io.FilterInputStream - */ - public int read (byte bytes[], int offset, int length) throws IOException { - int r; - - if ((r = in.read(bytes, offset, length)) == -1) - return r; + if (c == -1) { + return -1; + } - md5.Update(bytes, offset, r); + if ((c & ~0xff) != 0) { + log.warn("MD5InputStream.read() got character with (c & ~0xff) != 0)!"); + } else { + streamLength++; + md5.Update(c); + } - return r; - } + return c; + } - /** - * Returns array of bytes representing hash of the stream as - * finalized for the current state. - * @see MD5#Final - */ - public byte[] hash () { - return md5.Final(); - } + @Override + public int read(byte bytes[], int offset, int length) throws IOException { + int r; - public MD5 getMD5() { - return md5; - } + if ((r = in.read(bytes, offset, length)) == -1) { + return r; + } + streamLength += r; - /** - * This method is here for testing purposes only - do not rely - * on it being here. - **/ - public static void main(String[] arg) { - try { + md5.Update(bytes, offset, r); - //////////////////////////////////////////////////////////////// - // - // usage: java com.twmacinta.util.MD5InputStream [--use-default-md5] [--no-native-lib] filename - // - ///////// + return r; + } - // determine the filename to use and the MD5 impelementation to use + /** + * Returns array of bytes representing hash of the stream as + * finalized for the current state. + * @return + * @see MD5#Final + */ + public byte[] hash() { + return md5.Final(); + } - String filename = arg[arg.length-1]; - boolean use_default_md5 = false; - boolean use_native_lib = true; - for (int i = 0; i < arg.length-1; i++) { - if (arg[i].equals("--use-default-md5")) { - use_default_md5 = true; - } else if (arg[i].equals("--no-native-lib")) { - use_native_lib = false; - } - } + public MD5 getMD5() { + return md5; + } - // initialize common variables - - byte[] buf = new byte[65536]; - int num_read; - - // Use the default MD5 implementation that comes with Java - - if (use_default_md5) { - InputStream in = new BufferedInputStream(new FileInputStream(filename)); - java.security.MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); - while ((num_read = in.read(buf)) != -1) { - digest.update(buf, 0, num_read); - } - System.out.println(MD5.asHex(digest.digest())+" "+filename); - in.close(); - - // Use the optimized MD5 implementation - - } else { - - // disable the native library search, if requested - - if (!use_native_lib) { - MD5.initNativeLibrary(true); - } - - // calculate the checksum - - MD5InputStream in = new MD5InputStream(new BufferedInputStream(new FileInputStream(filename))); - while ((num_read = in.read(buf)) != -1); - System.out.println(MD5.asHex(in.hash())+" "+filename); - in.close(); - } - } catch (Exception e) { - e.printStackTrace(); + public long getStreamLength() { + return streamLength; } - } + //TODO TC-20090711 : do a unit test instead of this main ! +// /** +// * This method is here for testing purposes only - do not rely +// * on it being here. +// * +// * @param arg +// */ +// public static void main(String[] arg) { +// try { +// +// //////////////////////////////////////////////////////////////// +// // +// // usage: java com.twmacinta.util.MD5InputStream [--use-default-md5] [--no-native-lib] filename +// // +// ///////// +// +// // determine the filename to use and the MD5 impelementation to use +// +// String filename = arg[arg.length - 1]; +// boolean use_default_md5 = false; +// boolean use_native_lib = true; +// for (int i = 0; i < arg.length - 1; i++) { +// if (arg[i].equals("--use-default-md5")) { +// use_default_md5 = true; +// } else if (arg[i].equals("--no-native-lib")) { +// use_native_lib = false; +// } +// } +// +// // initialize common variables +// +// byte[] buf = new byte[65536]; +// int num_read; +// +// // Use the default MD5 implementation that comes with Java +// +// if (use_default_md5) { +// InputStream in = new BufferedInputStream(new FileInputStream(filename)); +// java.security.MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); +// while ((num_read = in.read(buf)) != -1) { +// digest.update(buf, 0, num_read); +// } +// System.out.println(MD5.asHex(digest.digest()) + " " + filename); +// in.close(); +// +// // Use the optimized MD5 implementation +// +// } else { +// +// // disable the native library search, if requested +// +// if (!use_native_lib) { +// MD5.initNativeLibrary(true); +// } +// +// // calculate the checksum +// +// MD5InputStream in = new MD5InputStream(new BufferedInputStream(new FileInputStream(filename))); +// while ((num_read = in.read(buf)) != -1); +// System.out.println(MD5.asHex(in.hash()) + " " + filename); +// in.close(); +// } +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } }