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

The following examples show how to use ucar.unidata.io.RandomAccessFile#length() . 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: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static boolean isValidFile(RandomAccessFile raf) throws IOException {
  // For HDF5, we need to search forward
  long filePos = 0;
  long size = raf.length();
  while ((filePos < size - 8) && (filePos < maxHeaderPos)) {
    byte[] buff = new byte[magic.length];
    raf.seek(filePos);
    if (raf.read(buff) < magic.length)
      return false;
    if (NetcdfFileFormat.memequal(buff, magic, magic.length)) {
      return true;
    }
    // The offsets that the header can be at
    filePos = (filePos == 0) ? 512 : 2 * filePos;
  }
  return false;
}
 
Example 2
Source File: Grib2RecordScanner.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static boolean isValidFile(RandomAccessFile raf) {
  try {
    raf.seek(0);
    boolean found = raf.searchForward(matcher, maxScan); // look in first 16K
    if (!found)
      return false;
    raf.skipBytes(7); // will be positioned on byte 0 of indicator section
    int edition = raf.read(); // read at byte 8
    if (edition != 2)
      return false;

    // check ending = 7777
    long len = GribNumbers.int8(raf);
    if (len > raf.length())
      return false;
    raf.skipBytes(len - 20);
    for (int i = 0; i < 4; i++) {
      if (raf.read() != 55)
        return false;
    }
    return true;

  } catch (IOException e) {
    return false;
  }
}
 
Example 3
Source File: Grib1Iosp.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public boolean isValidFile(RandomAccessFile raf) throws IOException {
  if (raf instanceof HTTPRandomAccessFile) { // only do remote if memory resident
    if (raf.length() > raf.getBufferSize())
      return false;

  } else { // wont accept remote index
    GribCdmIndex.GribCollectionType type = GribCdmIndex.getType(raf);
    if (type == GribCdmIndex.GribCollectionType.GRIB1)
      return true;
    if (type == GribCdmIndex.GribCollectionType.Partition1)
      return true;
  }

  // check for GRIB1 data file
  return Grib1RecordScanner.isValidFile(raf);
}
 
Example 4
Source File: Grib2Iosp.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public boolean isValidFile(RandomAccessFile raf) throws IOException {
  if (raf instanceof HTTPRandomAccessFile) { // only do remote if memory resident
    if (raf.length() > raf.getBufferSize())
      return false;

  } else { // wont accept remote index
    GribCdmIndex.GribCollectionType type = GribCdmIndex.getType(raf);
    if (type == GribCdmIndex.GribCollectionType.GRIB2)
      return true;
    if (type == GribCdmIndex.GribCollectionType.Partition2)
      return true;
  }

  // check for GRIB2 data file
  return Grib2RecordScanner.isValidFile(raf);
}
 
Example 5
Source File: Level2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Level2Record factory(RandomAccessFile din, int record, long message_offset31) throws IOException {
  long offset = record * RADAR_DATA_SIZE + FILE_HEADER_SIZE + message_offset31;
  if (offset >= din.length())
    return null;
  else
    return new Level2Record(din, record, message_offset31);
}
 
Example 6
Source File: Cinrad2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Cinrad2Record factory(RandomAccessFile din, int record) throws IOException {
  long offset = (long) record * RADAR_DATA_SIZE + FILE_HEADER_SIZE;
  if (offset >= din.length())
    return null;
  else
    return new Cinrad2Record(din, record);
}
 
Example 7
Source File: Uspln.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Check if this is a valid file for this IOServiceProvider.
 *
 * @param raf RandomAccessFile
 * @return true if valid.
 * @throws IOException if read error
 */
public boolean isValidFile(RandomAccessFile raf) throws IOException {
  raf.seek(0);
  int n = MAGIC.length();
  if (raf.length() < n) {
    return false;
  }

  String got = raf.readString(n);

  return (pMAGIC.matcher(got).find() || pMAGIC_OLD.matcher(got).find());
}
 
Example 8
Source File: NmcObsLegacy.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean isValidFile(RandomAccessFile raf) throws IOException {
  raf.seek(0);
  if (raf.length() < 60)
    return false;
  byte[] h = raf.readBytes(60);

  // 32 - 56 are X's
  for (int i = 32; i < 56; i++)
    if (h[i] != (byte) 'X')
      return false;

  try {
    short hour = Short.parseShort(new String(h, 0, 2, StandardCharsets.UTF_8));
    short minute = Short.parseShort(new String(h, 2, 2, StandardCharsets.UTF_8));
    short year = Short.parseShort(new String(h, 4, 2, StandardCharsets.UTF_8));
    short month = Short.parseShort(new String(h, 6, 2, StandardCharsets.UTF_8));
    short day = Short.parseShort(new String(h, 8, 2, StandardCharsets.UTF_8));

    if ((hour < 0) || (hour > 24))
      return false;
    if ((minute < 0) || (minute > 60))
      return false;
    if ((year < 0) || (year > 100))
      return false;
    if ((month < 0) || (month > 12))
      return false;
    if ((day < 0) || (day > 31))
      return false;

  } catch (Exception e) {
    return false;
  }

  return true;
}
 
Example 9
Source File: Grib2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void check(RandomAccessFile raf, Formatter f) throws IOException {
  long messLen = is.getMessageLength();
  long startPos = is.getStartPos();
  long endPos = is.getEndPos();

  if (endPos > raf.length()) {
    f.format("End of GRIB message (start=%d len=%d) end=%d > file.length=%d for %s%n", startPos, messLen, endPos,
        raf.length(), raf.getLocation());
    return;
  }

  raf.seek(endPos - 4);
  for (int i = 0; i < 4; i++) {
    if (raf.read() != 55) {
      String clean = StringUtil2.cleanup(header);
      if (clean.length() > 40)
        clean = clean.substring(0, 40) + "...";
      f.format("Missing End of GRIB message (start=%d len=%d) end=%d header= %s for %s (len=%d)%n", startPos, messLen,
          endPos, clean, raf.getLocation(), raf.length());
      break;
    }
  }

  long dataLen = dataSection.getMsgLength();
  long dataStart = dataSection.getStartingPosition();
  long dataEnd = dataStart + dataLen;

  if (dataEnd > raf.length()) {
    f.format("GRIB data section (start=%d len=%d) end=%d > file.length=%d for %s%n", dataStart, dataLen, dataEnd,
        raf.length(), raf.getLocation());
    return;
  }

  if (dataEnd > endPos) {
    f.format("GRIB data section (start=%d len=%d) end=%d > message end=%d for %s%n", dataStart, dataLen, dataEnd,
        endPos, raf.getLocation());
  }

}
 
Example 10
Source File: Grib1RecordScanner.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static boolean isValidFile(RandomAccessFile raf) {
  try {
    raf.seek(0);
    boolean found = raf.searchForward(matcher, maxScan); // look in first 16K
    if (!found) {
      return false;
    }
    raf.skipBytes(4); // will be positioned on byte 0 of indicator section
    int len = GribNumbers.uint3(raf);
    int edition = raf.read(); // read at byte 8
    if (edition != 1) {
      return false;
    }

    /*
     * Due to a trick done by ECMWF's GRIBEX to support large GRIBs, we need a special treatment
     * to fix the length of the GRIB message. See:
     * https://software.ecmwf.int/wiki/display/EMOS/Changes+in+cycle+000281
     * https://github.com/Unidata/thredds/issues/445
     */
    len = getFixedTotalLengthEcmwfLargeGrib(raf, len);

    // check ending = 7777
    if (len > raf.length()) {
      return false;
    }
    if (allowBadIsLength) {
      return true;
    }

    raf.skipBytes(len - 12);
    for (int i = 0; i < 4; i++) {
      if (raf.read() != 55) {
        return false;
      }
    }
    return true;

  } catch (IOException e) {
    return false;
  }
}