ucar.unidata.io.RandomAccessFile.searchForward()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(177)

本文整理了Java中ucar.unidata.io.RandomAccessFile.searchForward方法的一些代码示例,展示了RandomAccessFile.searchForward的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RandomAccessFile.searchForward方法的具体详情如下:
包路径:ucar.unidata.io.RandomAccessFile
类名称:RandomAccessFile
方法名:searchForward

RandomAccessFile.searchForward介绍

[英]Search forward from the current pos, looking for a match.
[中]从当前pos向前搜索,寻找匹配项。

代码示例

代码示例来源:origin: edu.ucar/cdm

public static boolean failFast(RandomAccessFile raf) throws IOException {
 return !raf.searchForward(matcher, 1000); // look in first 1K
}

代码示例来源:origin: Unidata/thredds

public static boolean failFast(RandomAccessFile raf) throws IOException {
 raf.seek(0);
 boolean ok = raf.searchForward(matchDSET, 1000); // look in first 1K
 if (!ok) {
  raf.seek(0);
  ok = raf.searchForward(matchdset, 1000); // look in first 1K
  if (!ok) return true;
 }
 long pos = raf.getFilePointer();
 ok = raf.searchForward(matchENDVARS, 20000); // look in next 20K
 if (!ok) {
  raf.seek(pos);
  ok = raf.searchForward(matchendvars, 20000); // look in next 20K
 }
 return !ok;
}

代码示例来源:origin: Unidata/thredds

public static void main2(String[] args) throws IOException {
 String filename = (args.length > 0 && args[0] != null) ? args[0] : "Q:/cdmUnitTest/formats/grib2/LMPEF_CLM_050518_1200.grb";
 System.out.printf("Scan %s%n", filename);
 try (RandomAccessFile raf = new RandomAccessFile(filename, "r")) {
  raf.seek(0);
  while (!raf.isAtEndOfFile()) {
   boolean found = raf.searchForward(matcher, -1);
   if (found) {
    raf.skipBytes(7); // will be positioned on byte 0 of indicator section
    int edition = raf.read(); // read at byte 8
    System.out.printf(" GRIB edition %d found at pos %d%n", edition, raf.getFilePointer());
    break;
   }
  }
 } catch (IOException e) {
  e.printStackTrace();
 }
}

代码示例来源:origin: Unidata/thredds

public boolean hasNext() throws IOException {
 if (lastPos >= raf.length()) return false;
 raf.seek(lastPos);
 boolean more = raf.searchForward(matcher, -1); // will scan to end for another BUFR header
 if (more) {
  long stop = raf.getFilePointer();
  int sizeHeader = (int) (stop - lastPos);
  if (sizeHeader > 30) sizeHeader = 30;
  header = new byte[sizeHeader];
  startPos = stop-sizeHeader;
  raf.seek(startPos);
  int nRead = raf.read(header);
  if (nRead != header.length) {
    log.warn("Unable to read full BUFR header. Got " + nRead +
        " but expected " + header.length);
    return false;
  }
 }
 if (debug && countMsgs % 100 == 0) System.out.printf("%d ", countMsgs);
 return more;
}

代码示例来源:origin: edu.ucar/grib

/**
 * returns Grib file type, 1 or 2, or 0 not a Grib file.
 *
 * @return GribFileType
 * @throws IOException           on data read
 * @throws NotSupportedException NotSupportedException
 */
public final int getEdition() throws IOException {
 raf.seek(0);
 if (!raf.searchForward(matcher, 8000)) return 0; // must find "GRIB" in first 8k
 raf.skipBytes(4);
 //  Read Section 0 Indicator Section to get Edition number
 Grib2IndicatorSection is = new Grib2IndicatorSection(raf);  // section 0
 return is.getGribEdition();
}

代码示例来源:origin: edu.ucar/bufr

public boolean hasNext() throws IOException {
 if (lastPos >= raf.length()) return false;
 raf.seek(lastPos);
 boolean more = raf.searchForward(matcher, -1); // will scan to end for another BUFR header
 if (more) {
  long stop = raf.getFilePointer();
  int sizeHeader = (int) (stop - lastPos);
  if (sizeHeader > 30) sizeHeader = 30;
  header = new byte[sizeHeader];
  startPos = stop-sizeHeader;
  raf.seek(startPos);
  raf.read(header);
 }
 // System.out.println(" more "+more+" at "+startPos+" lastPos "+ lastPos+" nbytes= "+nbytes+ " msg "+countMsgs);
 return more;
}

代码示例来源:origin: edu.ucar/grib

static public boolean isValidFile(RandomAccessFile raf) throws IOException {
 raf.seek(0);
 if (!raf.searchForward(matcher, 8000)) return false; // must find "GRIB" in first 8k
 raf.skipBytes(4);
 //  Read Section 0 Indicator Section to get Edition number
 Grib2IndicatorSection is = new Grib2IndicatorSection(raf);  // section 0
 if (is.getGribEdition() != 1 && is.getGribEdition() != 2)
  return false;
 if (is.getGribLength() > raf.length())
  return false;
 return true;
}

代码示例来源:origin: Unidata/thredds

/**
 * is this a valid BUFR file.
 *
 * @param raf check this file
 * @return true if its a BUFR file
 * @throws IOException on read error
 */
static public boolean isValidFile(ucar.unidata.io.RandomAccessFile raf) throws IOException {
 raf.seek(0);
 if (!raf.searchForward(matcher, 40 * 1000)) return false; // must find "BUFR" in first 40k
 raf.skipBytes(4);
 BufrIndicatorSection is = new BufrIndicatorSection(raf);
 if (is.getBufrEdition() > 4) return false;
 // if(is.getBufrLength() > MAX_MESSAGE_SIZE) return false;
 return !(is.getBufrLength() > raf.length());
}

代码示例来源:origin: edu.ucar/bufr

/**
 * is this a valid BUFR file.
 *
 * @param raf check this file
 * @return true if its a BUFR file
 * @throws IOException on read error
 */
static public boolean isValidFile(ucar.unidata.io.RandomAccessFile raf) throws IOException {
 raf.seek(0);
 if (!raf.searchForward(matcher, 8000)) return false; // must find "BUFR" in first 8k
 raf.skipBytes(4);
 BufrIndicatorSection is = new BufrIndicatorSection(raf);
 if (is.getBufrEdition() > 4) return false;
 // if(is.getBufrLength() > MAX_MESSAGE_SIZE) return false;
 if (is.getBufrLength() > raf.length()) return false;
 return true;
}

代码示例来源:origin: Unidata/thredds

static public 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;
 }
}

代码示例来源:origin: Unidata/thredds

static public 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;
 }
}

代码示例来源:origin: Unidata/thredds

more = raf.searchForward(matcher, -1); // will scan to end for a 'GRIB' string
if (!more) break;

代码示例来源:origin: Unidata/thredds

public boolean hasNext() throws IOException {
 if (lastPos >= raf.length()) return false;
 boolean more;
 long foundAt = 0;
 while (true) { // scan until we get a GRIB-1 or more is false
  raf.seek(lastPos);
  more = raf.searchForward(matcher, -1); // will scan to end for a 'GRIB' string
  if (!more) break;
  foundAt = raf.getFilePointer();
  // see if its GRIB-1
  raf.skipBytes(7);
  int edition = raf.read();
  if (edition == 1) break;
  lastPos = raf.getFilePointer(); // not edition 1 ! could terminate ??
 }
 if (more) {
  // read the header - stuff between the records
  int sizeHeader = (int) (foundAt - lastPos);
  if (sizeHeader > 100) sizeHeader = 100;   // maximum 100 bytes, more likely to be garbage
  long startPos = foundAt-sizeHeader;
  header = new byte[sizeHeader];
  raf.seek(startPos);
  raf.readFully(header);
  raf.seek(foundAt);
  this.lastPos = foundAt; // ok start from here next time
 }
 return more;
}

相关文章