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.File;
20: import java.io.FileInputStream;
21: import java.io.IOException;
22: import java.util.zip.Adler32;
23: import java.util.zip.CheckedInputStream;
24: import java.util.zip.Checksum;
25:
26: /**
27: * Perform a single checksum calculation for multiple files
28: *
29: * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
30: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
31: * @version $Id$
32: */
33: public class MultiFileChecksumHelper {
34: public static long getChecksum(File[] files) {
35: CheckedInputStream cis = null;
36: FileInputStream is = null;
37: Checksum checksum = new Adler32();
38: byte[] tempBuf = new byte[128];
39:
40: for (int i = 0; i < files.length && files[i] != null
41: && files[i].exists() && files[i].isFile(); i++) {
42: try {
43: is = new FileInputStream(files[i]);
44: cis = new CheckedInputStream(is, checksum);
45: while (cis.read(tempBuf) >= 0) {
46: }
47: } catch (Exception e) {
48: throw new RuntimeException(e);
49: } finally {
50: if (cis != null) {
51: try {
52: cis.close();
53: } catch (IOException ioe) {
54: }
55: cis = null;
56: }
57: if (is != null) {
58: try {
59: is.close();
60: } catch (IOException ioe) {
61: }
62: is = null;
63: }
64: }
65: }
66: return checksum.getValue();
67: }
68: }
|