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

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

本文整理了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

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

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

  1. public static boolean failFast(RandomAccessFile raf) throws IOException {
  2. raf.seek(0);
  3. boolean ok = raf.searchForward(matchDSET, 1000); // look in first 1K
  4. if (!ok) {
  5. raf.seek(0);
  6. ok = raf.searchForward(matchdset, 1000); // look in first 1K
  7. if (!ok) return true;
  8. }
  9. long pos = raf.getFilePointer();
  10. ok = raf.searchForward(matchENDVARS, 20000); // look in next 20K
  11. if (!ok) {
  12. raf.seek(pos);
  13. ok = raf.searchForward(matchendvars, 20000); // look in next 20K
  14. }
  15. return !ok;
  16. }

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

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

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

  1. public boolean hasNext() throws IOException {
  2. if (lastPos >= raf.length()) return false;
  3. raf.seek(lastPos);
  4. boolean more = raf.searchForward(matcher, -1); // will scan to end for another BUFR header
  5. if (more) {
  6. long stop = raf.getFilePointer();
  7. int sizeHeader = (int) (stop - lastPos);
  8. if (sizeHeader > 30) sizeHeader = 30;
  9. header = new byte[sizeHeader];
  10. startPos = stop-sizeHeader;
  11. raf.seek(startPos);
  12. int nRead = raf.read(header);
  13. if (nRead != header.length) {
  14. log.warn("Unable to read full BUFR header. Got " + nRead +
  15. " but expected " + header.length);
  16. return false;
  17. }
  18. }
  19. if (debug && countMsgs % 100 == 0) System.out.printf("%d ", countMsgs);
  20. return more;
  21. }

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

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

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

  1. public boolean hasNext() throws IOException {
  2. if (lastPos >= raf.length()) return false;
  3. raf.seek(lastPos);
  4. boolean more = raf.searchForward(matcher, -1); // will scan to end for another BUFR header
  5. if (more) {
  6. long stop = raf.getFilePointer();
  7. int sizeHeader = (int) (stop - lastPos);
  8. if (sizeHeader > 30) sizeHeader = 30;
  9. header = new byte[sizeHeader];
  10. startPos = stop-sizeHeader;
  11. raf.seek(startPos);
  12. raf.read(header);
  13. }
  14. // System.out.println(" more "+more+" at "+startPos+" lastPos "+ lastPos+" nbytes= "+nbytes+ " msg "+countMsgs);
  15. return more;
  16. }

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

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

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

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

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

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

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

  1. static public boolean isValidFile(RandomAccessFile raf) {
  2. try {
  3. raf.seek(0);
  4. boolean found = raf.searchForward(matcher, maxScan); // look in first 16K
  5. if (!found) return false;
  6. raf.skipBytes(7); // will be positioned on byte 0 of indicator section
  7. int edition = raf.read(); // read at byte 8
  8. if (edition != 2) return false;
  9. // check ending = 7777
  10. long len = GribNumbers.int8(raf);
  11. if (len > raf.length()) return false;
  12. raf.skipBytes(len-20);
  13. for (int i = 0; i < 4; i++) {
  14. if (raf.read() != 55) return false;
  15. }
  16. return true;
  17. } catch (IOException e) {
  18. return false;
  19. }
  20. }

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

  1. static public boolean isValidFile(RandomAccessFile raf) {
  2. try {
  3. raf.seek(0);
  4. boolean found = raf.searchForward(matcher, maxScan); // look in first 16K
  5. if (!found) return false;
  6. raf.skipBytes(4); // will be positioned on byte 0 of indicator section
  7. int len = GribNumbers.uint3(raf);
  8. int edition = raf.read(); // read at byte 8
  9. if (edition != 1) return false;
  10. /* Due to a trick done by ECMWF's GRIBEX to support large GRIBs, we need a special treatment
  11. * to fix the length of the GRIB message. See:
  12. * https://software.ecmwf.int/wiki/display/EMOS/Changes+in+cycle+000281
  13. * https://github.com/Unidata/thredds/issues/445
  14. */
  15. len = getFixedTotalLengthEcmwfLargeGrib(raf,len);
  16. // check ending = 7777
  17. if (len > raf.length()) return false;
  18. if (allowBadIsLength) return true;
  19. raf.skipBytes(len-12);
  20. for (int i = 0; i < 4; i++) {
  21. if (raf.read() != 55) return false;
  22. }
  23. return true;
  24. } catch (IOException e) {
  25. return false;
  26. }
  27. }

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

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

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

  1. public boolean hasNext() throws IOException {
  2. if (lastPos >= raf.length()) return false;
  3. boolean more;
  4. long foundAt = 0;
  5. while (true) { // scan until we get a GRIB-1 or more is false
  6. raf.seek(lastPos);
  7. more = raf.searchForward(matcher, -1); // will scan to end for a 'GRIB' string
  8. if (!more) break;
  9. foundAt = raf.getFilePointer();
  10. // see if its GRIB-1
  11. raf.skipBytes(7);
  12. int edition = raf.read();
  13. if (edition == 1) break;
  14. lastPos = raf.getFilePointer(); // not edition 1 ! could terminate ??
  15. }
  16. if (more) {
  17. // read the header - stuff between the records
  18. int sizeHeader = (int) (foundAt - lastPos);
  19. if (sizeHeader > 100) sizeHeader = 100; // maximum 100 bytes, more likely to be garbage
  20. long startPos = foundAt-sizeHeader;
  21. header = new byte[sizeHeader];
  22. raf.seek(startPos);
  23. raf.readFully(header);
  24. raf.seek(foundAt);
  25. this.lastPos = foundAt; // ok start from here next time
  26. }
  27. return more;
  28. }

相关文章