Java Code Examples for ucar.unidata.io.RandomAccessFile#readLong()

The following examples show how to use ucar.unidata.io.RandomAccessFile#readLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GribCdmIndex.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean openIndex(RandomAccessFile indexRaf, Logger logger) {
  try {
    indexRaf.order(RandomAccessFile.BIG_ENDIAN);
    indexRaf.seek(0);

    //// header message
    byte[] magic = new byte[Grib2CollectionWriter.MAGIC_START.getBytes(StandardCharsets.UTF_8).length]; // they are
                                                                                                        // all the
    // same
    indexRaf.readFully(magic);

    int version = indexRaf.readInt();

    long recordLength = indexRaf.readLong();
    if (recordLength > Integer.MAX_VALUE) {
      logger.error("Grib2Collection {}: invalid recordLength size {}", indexRaf.getLocation(), recordLength);
      return false;
    }
    indexRaf.skipBytes(recordLength);

    int size = NcStream.readVInt(indexRaf);
    if ((size < 0) || (size > 100 * 1000 * 1000)) {
      logger.warn("GribCdmIndex {}: invalid index size {}", indexRaf.getLocation(), size);
      return false;
    }

    byte[] m = new byte[size];
    indexRaf.readFully(m);
    gribCollectionIndex = GribCollectionProto.GribCollection.parseFrom(m);
    return true;

  } catch (Throwable t) {
    logger.error("Error reading index " + indexRaf.getLocation(), t);
    return false;
  }
}