Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/util/Checksum.java


1   /*
2    * Checksum.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:31 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.util;
12  
13  import java.io.File;
14  import java.io.FileInputStream;
15  import java.io.FileNotFoundException;
16  import java.io.IOException;
17  import java.util.zip.CRC32;
18  import java.util.zip.CheckedInputStream;
19  
20  /**
21  *
22  *
23  */
24  public class Checksum
25  {
26  
27      /**
28      *
29      *
30      */
31      public Checksum()
32      {
33      
34      } // constructor
35      
36      
37      /**
38      *
39      *
40      */
41      public long getChecksum(String sFilename)
42      {
43          long nResult = -1;
44          FileInputStream inStream = null;
45          
46          // Create a file object and verify that the file can
47          // be read
48          File refFile = new File(sFilename);
49          if ((refFile == null) || (refFile.canRead() == false))
50          {
51              System.out.println("Cannot open file: " + sFilename);
52              return -1;
53          }
54          
55          // Open the file as a normal input stream
56          try
57          {
58              inStream = new FileInputStream(refFile);
59          }
60          
61          catch(FileNotFoundException fnfe)
62          {
63              System.out.println("Error opening file: " + sFilename + " , fnfe.toString()");
64          }
65          
66          // Create the type of checksum to be used
67          CRC32 checksumType = new CRC32();
68  //        Adler32 checksumType = new Adler32();
69          System.out.println(checksumType.toString());
70          
71          // Create the checksum input stream
72          CheckedInputStream  checkedStream = new CheckedInputStream(inStream, checksumType);
73          
74          byte[] bData  = new byte[4096];
75          int    nCount = 0;
76          
77          // Dummy read the file
78          while (nCount != -1)
79          {
80              try
81              {
82                  nCount = checkedStream.read(bData);
83              }
84              
85              catch(IOException ioe)
86              {
87                  System.out.println("Error reading file: " + sFilename + " , " + ioe.toString());
88                  return -1;
89              }
90          } // while
91          
92          // File read ok, get the checksum
93          nResult = checkedStream.getChecksum().getValue();
94  
95          return nResult;
96      } // getChecksum
97      
98  } // Checksum class