View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.util;
18  
19  import java.io.InputStream;
20  import java.io.IOException;
21  import java.util.zip.Adler32;
22  import java.util.zip.CheckedInputStream;
23  
24  
25  /***
26   * implements checksum related utilities
27   *
28   * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
29   * @version $Id: ChecksumHelper.java 516448 2007-03-09 16:25:47Z ate $
30   */
31  public final class ChecksumHelper
32  {
33      public static long getChecksum(InputStream is)
34      {
35          CheckedInputStream cis = null;        
36          long checksum = 0;
37          try 
38          {
39              cis = new CheckedInputStream(is, new Adler32());
40              byte[] tempBuf = new byte[128];
41              while (cis.read(tempBuf) >= 0) 
42              {
43              }
44              checksum = cis.getChecksum().getValue();
45          } 
46          catch (IOException e) 
47          {
48              checksum = 0;
49          }
50          finally
51          {
52              if (cis != null)
53              {
54                  try
55                  {
56                      cis.close();
57                  }
58                  catch (IOException ioe)
59                  {                    
60                  }
61              }
62          }
63          return checksum;
64      }
65  }
66