01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: * implements checksum related utilities
26: *
27: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
28: * @version $Id: ChecksumHelper.java 516448 2007-03-09 16:25:47Z ate $
29: */
30: public final class ChecksumHelper {
31: public static long getChecksum(InputStream is) {
32: CheckedInputStream cis = null;
33: long checksum = 0;
34: try {
35: cis = new CheckedInputStream(is, new Adler32());
36: byte[] tempBuf = new byte[128];
37: while (cis.read(tempBuf) >= 0) {
38: }
39: checksum = cis.getChecksum().getValue();
40: } catch (IOException e) {
41: checksum = 0;
42: } finally {
43: if (cis != null) {
44: try {
45: cis.close();
46: } catch (IOException ioe) {
47: }
48: }
49: }
50: return checksum;
51: }
52: }
|