MATSIM
CRCChecksum.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * CRCChecksum.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.utils.misc;
22 
23 import java.io.BufferedInputStream;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.zip.CRC32;
29 import java.util.zip.CheckedInputStream;
30 import java.util.zip.GZIPInputStream;
31 
32 import org.apache.logging.log4j.LogManager;
33 import org.apache.logging.log4j.Logger;
34 
35 public class CRCChecksum {
36  private static final Logger log = LogManager.getLogger( CRCChecksum.class );
37 
38  private static long getCRCFromStream(final InputStream in) {
39  long check = 0;
40  if (in == null) return 0;
41  CRC32 crc = new CRC32();
42  try (CheckedInputStream cis = new CheckedInputStream(in, crc)) {
43  byte[] buffer = new byte[4096];
44  while (cis.read(buffer) != -1) {
45  /* Read until the end of the stream is reached. */
46  }
47  check = crc.getValue();
48  cis.close();
49  }
50  catch (IOException e) {
51  throw new RuntimeException(e);
52  }
53  return check;
54  }
55 
69  public static long getCRCFromFile(final String filename) {
70  log.info("filename=" + filename ) ;
71  if (new File(filename).exists()) {
72  log.info( "file exists");
73  if (filename.endsWith(".gz")) {
74  log.info( "file ends in gz");
75  try ( InputStream in = new GZIPInputStream(new BufferedInputStream(new FileInputStream(filename))) ) {
76  long result = getCRCFromStream(in);
77  in.close();
78  return result ;
79  } catch (IOException e) {
80  throw new RuntimeException(e);
81  }
82  } else { // not with gz
83  log.info( "file does not end in gz");
84  try ( InputStream in = new BufferedInputStream(new FileInputStream(filename)) ) {
85  long result = getCRCFromStream(in);
86  in.close();
87  return result ;
88  } catch (IOException e) {
89  throw new RuntimeException(e);
90  }
91  }
92  } else { // does not exist
93  log.info( "file does not exist; search via class loader");
94  // (the logic is: if the file cannot be found directly on the file system, this is some method to search
95  // in generic locations (which ones?). kai, feb'14)
96  if (filename.endsWith(".gz")) {
97  log.info( "file ends in gz");
98  try ( InputStream stream = CRCChecksum.class.getClassLoader().getResourceAsStream(filename) ;
99  InputStream in = new GZIPInputStream(new BufferedInputStream(stream))) {
100  long result = getCRCFromStream(in);
101  in.close();
102  return result ;
103  } catch (IOException e) {
104  throw new RuntimeException(e);
105  }
106  } else { // not work gz
107  log.info( "file does not end in gz");
108  try ( InputStream stream = CRCChecksum.class.getClassLoader().getResourceAsStream(filename) ;
109  InputStream in = new BufferedInputStream(stream)) {
110  long result = getCRCFromStream(in);
111  in.close();
112  return result ;
113  } catch (IOException e) {
114  throw new RuntimeException(e);
115  }
116  }
117  }
118  }
119 }
static long getCRCFromStream(final InputStream in)
static long getCRCFromFile(final String filename)