1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * 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 and15 * limitations under the License.16 */17packageorg.apache.jetspeed.util;
1819import java.io.File;
20import java.io.FileInputStream;
21import java.io.IOException;
22import java.util.zip.Adler32;
23import java.util.zip.CheckedInputStream;
24import java.util.zip.Checksum;
2526/***27 * Perform a single checksum calculation for multiple files28 *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 */33publicclassMultiFileChecksumHelper34 {
35publicstaticlong getChecksum(File[] files)
36 {
37 CheckedInputStream cis = null;
38 FileInputStream is = null;
39 Checksum checksum = new Adler32();
40 byte[] tempBuf = new byte[128];
4142for ( int i = 0; i < files.length && files[i] != null && files[i].exists() && files[i].isFile(); i++ )
43 {
44try45 {
46 is = new FileInputStream(files[i]);
47 cis = new CheckedInputStream(is, checksum);
48while (cis.read(tempBuf) >= 0) {}
49 }
50catch (Exception e)
51 {
52thrownew RuntimeException(e);
53 }
54finally55 {
56if (cis != null)
57 {
58try59 {
60 cis.close();
61 }
62catch (IOException ioe) {}
63 cis = null;
64 }
65if (is != null)
66 {
67try68 {
69 is.close();
70 }
71catch (IOException ioe) {}
72 is = null;
73 }
74 }
75 }
76return checksum.getValue();
77 }
78 }