public boolean readIndex(String filename, long gribLastModified, CollectionManager.Force force) throws IOException { File idxFile = GribCollection.getIndexFile(filename + GBX9_IDX); if (!idxFile.exists()) return false; long idxModified = idxFile.lastModified(); if ((force != CollectionManager.Force.nocheck) && (idxModified < gribLastModified)) return false; // force new index if file was updated FileInputStream fin = new FileInputStream(idxFile); // LOOK need DiskCache for non-writeable directories try { //// check header is ok if (!NcStream.readAndTest(fin, MAGIC_START.getBytes())) { log.info("Bad magic number of grib index, on file" + idxFile); return false; } int v = NcStream.readVInt(fin); if (v != version) { if ((v == 0) || (v > version)) throw new IOException( "Grib1Index found version " + v + ", want version " + version + " on " + filename); if (log.isDebugEnabled()) log.debug( "Grib1Index found version " + v + ", want version " + version + " on " + filename); return false; } int size = NcStream.readVInt(fin); if (size <= 0 || size > 100 * 1000 * 1000) { // try to catch garbage log.warn("Grib1Index bad size = {} for {} ", size, filename); return false; } byte[] m = new byte[size]; NcStream.readFully(fin, m); Grib1IndexProto.Grib1Index proto = Grib1IndexProto.Grib1Index.parseFrom(m); String fname = proto.getFilename(); if (debug) System.out.printf("%s for %s%n", fname, filename); gdsList = new ArrayList<Grib1SectionGridDefinition>(proto.getGdsListCount()); for (Grib1IndexProto.Grib1GdsSection pgds : proto.getGdsListList()) { Grib1SectionGridDefinition gds = readGds(pgds); gdsList.add(gds); } if (debug) System.out.printf(" read %d gds%n", gdsList.size()); records = new ArrayList<Grib1Record>(proto.getRecordsCount()); for (Grib1IndexProto.Grib1Record precord : proto.getRecordsList()) { records.add(readRecord(precord)); } if (debug) System.out.printf(" read %d records%n", records.size()); } catch (java.lang.NegativeArraySizeException e) { log.error("GribIndex failed on " + filename, e); return false; } catch (IOException e) { log.error("GribIndex failed on " + filename, e); return false; } finally { fin.close(); } return true; }