ChecksumCRC.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.checksum;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.InputStream;
  25. import java.util.zip.CRC32;
  26. import java.util.zip.CheckedInputStream;

  27. /**
  28.  * ChecksumCRC
  29.  *
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class ChecksumCRC {

  36.    
  37.     public static long checksumCRC32(String fileName) throws ChecksumException{
  38.         try{
  39.             File file = new File(fileName);
  40.             return ChecksumCRC.checksumCRC32(file);
  41.         }catch(Exception e){
  42.             throw new ChecksumException("Calcolo checksum non riuscito: "+e.getMessage(),e);
  43.         }
  44.     }
  45.    
  46.     public static long checksumCRC32(File file) throws ChecksumException{
  47.         FileInputStream fin = null;
  48.         try{
  49.             fin = new FileInputStream(file);
  50.             return ChecksumCRC.checksumCRC32(fin);
  51.         }catch(Exception e){
  52.             throw new ChecksumException("Calcolo checksum non riuscito: "+e.getMessage(),e);
  53.         }finally{
  54.             try{
  55.                 if(fin!=null)
  56.                     fin.close();
  57.             }catch(Exception eClose){
  58.                 // close
  59.             }
  60.         }
  61.     }
  62.    
  63.     public static long checksumCRC32(byte[] bytes) throws ChecksumException{
  64.         ByteArrayInputStream bin = null;
  65.         try{
  66.             bin = new ByteArrayInputStream(bytes);
  67.             return ChecksumCRC.checksumCRC32(bin);
  68.         }catch(Exception e){
  69.             throw new ChecksumException("Calcolo checksum non riuscito: "+e.getMessage(),e);
  70.         }finally{
  71.             try{
  72.                 if(bin!=null)
  73.                     bin.close();
  74.             }catch(Exception eClose){
  75.                 // close
  76.             }
  77.         }
  78.     }
  79.    
  80.     public static long checksumCRC32(InputStream i) throws ChecksumException{
  81.         try{
  82.             CheckedInputStream cis = new CheckedInputStream(i,new CRC32());
  83.             byte[]tempBuf = new byte[128];
  84.             while(cis.read(tempBuf)>=0){}
  85.             long checksum = cis.getChecksum().getValue();
  86.             return checksum;
  87.         }catch(Exception e){
  88.             throw new ChecksumException("Calcolo checksum non riuscito: "+e.getMessage(),e);
  89.         }
  90.     }
  91.    
  92. }