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

The following examples show how to use ucar.unidata.io.RandomAccessFile#readString() . 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: Vis5DIosp.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Is this a valid file?
 *
 * @param raf RandomAccessFile to check
 * @return true if a valid McIDAS AREA file
 * @throws IOException problem reading file
 */
public boolean isValidFile(RandomAccessFile raf) throws IOException {
  // quick test
  raf.order(RandomAccessFile.BIG_ENDIAN);
  raf.seek(0);
  String got = raf.readString(V5D.length());
  if (got.equals(V5D)) {
    return true;
  } else { // more rigorous test
    V5DStruct vv;
    try {
      vv = V5DStruct.v5dOpenFile(raf);
    } catch (BadFormException bfe) {
      vv = null;
    }
    return vv != null;
  }
}
 
Example 2
Source File: NmcObsLegacy.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean endFile(RandomAccessFile raf) throws IOException {
  if (showSkip)
    System.out.println(" endFile start at " + raf.getFilePointer());

  String endRecord = raf.readString(10); // new String(raf.readBytes(10));
  while (endRecord.equals("ENDOF FILE")) {
    endRecord = raf.readString(10); // new String(raf.readBytes(10));
  }

  try {
    while (raf.read() != (int) 'X'); // find where X's start
    while (raf.read() == (int) 'X'); // skip X's till you run out
    raf.skipBytes(-1); // go back one
    readHeader(raf);
    return true;

  } catch (EOFException e) {
    return false;
  }
}
 
Example 3
Source File: GribCdmIndex.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Find out what kind of index this is
 *
 * @param raf open RAF
 * @return GribCollectionType
 * @throws IOException on read error
 */
public static GribCollectionType getType(RandomAccessFile raf) throws IOException {
  String magic;

  raf.seek(0);
  magic = raf.readString(Grib2CollectionWriter.MAGIC_START.getBytes(StandardCharsets.UTF_8).length);

  switch (magic) {
    case Grib2CollectionWriter.MAGIC_START:
      return GribCollectionType.GRIB2;

    case Grib1CollectionWriter.MAGIC_START:
      return GribCollectionType.GRIB1;

    case Grib2PartitionBuilder.MAGIC_START:
      return GribCollectionType.Partition2;

    case Grib1PartitionBuilder.MAGIC_START:
      return GribCollectionType.Partition1;

  }
  return GribCollectionType.none;
}
 
Example 4
Source File: UAMIVServiceProvider.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.
 */
public boolean isValidFile(RandomAccessFile raf) {
  try {
    raf.order(RandomAccessFile.BIG_ENDIAN);
    raf.seek(0);
    raf.skipBytes(4);
    String test = raf.readString(40);
    return test.equals(EMISSIONS) || test.equals(AVERAGE) || test.equals(AIRQUALITY) || test.equals(INSTANT);
  } catch (IOException ioe) {
    return false;
  }
}
 
Example 5
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 6
Source File: H5objects.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Read a zero terminated String. Leave file positioned after zero terminator byte.
 *
 * @param raf from this file
 * @return String (dont include zero terminator)
 * @throws IOException on io error
 */
private String readString(RandomAccessFile raf) throws IOException {
  long filePos = raf.getFilePointer();

  int count = 0;
  while (raf.readByte() != 0)
    count++;

  raf.seek(filePos);
  String result = raf.readString(count);
  raf.readByte(); // skip the zero byte! nn
  return result;
}
 
Example 7
Source File: H5header.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Read a zero terminated String. Leave file positioned after zero terminator byte.
 *
 * @param raf from this file
 * @return String (dont include zero terminator)
 * @throws java.io.IOException on io error
 */
private String readString(RandomAccessFile raf) throws IOException {
  long filePos = raf.getFilePointer();

  int count = 0;
  while (raf.readByte() != 0)
    count++;

  raf.seek(filePos);
  String result = raf.readString(count);
  raf.readByte(); // skip the zero byte! nn
  return result;
}
 
Example 8
Source File: Level2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private String getDataBlockStringValue(RandomAccessFile raf, short offset, int skip, int size) throws IOException {
  long off = offset + message_offset + MESSAGE_HEADER_SIZE;
  raf.seek(off);
  raf.skipBytes(skip);
  return raf.readString(size);
}
 
Example 9
Source File: Cinrad2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void readCCHeader(RandomAccessFile din) throws IOException {

    message_offset = 0;
    din.seek(message_offset);
    // site info 170
    din.skipBytes(66);

    // Message Header
    String stationId = din.readString(40);
    String stationNbr = din.readString(10);

    din.skipBytes(20);

    String clon = din.readString(16);
    String clat = din.readString(16);
    // latlon
    int lon = din.readInt();
    int lat = din.readInt();
    int hhh = din.readInt();

    din.skipBytes(4);

    // ObservationInfo

    short syear1 = convertunsignedByte2Short(din.readByte());
    short syear2 = convertunsignedByte2Short(din.readByte());
    short syear = (short) (syear1 * 100 + syear2);
    short smm = convertunsignedByte2Short(din.readByte());
    short sdd = convertunsignedByte2Short(din.readByte());
    short shh = convertunsignedByte2Short(din.readByte());
    short smi = convertunsignedByte2Short(din.readByte());
    short sss = convertunsignedByte2Short(din.readByte());

    dateTime0 = CalendarDate.of(null, syear, smm, sdd, shh, smi, sss);

    din.skipBytes(1);

    syear1 = convertunsignedByte2Short(din.readByte());
    syear2 = convertunsignedByte2Short(din.readByte());
    syear = (short) (syear1 * 100 + syear2);
    smm = convertunsignedByte2Short(din.readByte());
    sdd = convertunsignedByte2Short(din.readByte());
    shh = convertunsignedByte2Short(din.readByte());
    smi = convertunsignedByte2Short(din.readByte());
    sss = convertunsignedByte2Short(din.readByte());

    dateTimeE = CalendarDate.of(null, syear, smm, sdd, shh, smi, sss);
    short scanMode = convertunsignedByte2Short(din.readByte());
    if (scanMode == 10) {
      sweepN = 1;
    } else if (scanMode >= 100) {
      sweepN = scanMode - 100;
    } else {
      throw new IOException("Error reading CINRAD CC data: Unsupported product: RHI/FFT");
    }

    elev = new int[sweepN];
    din.skipBytes(4);
    short sRHIA = (short) din.readUnsignedShort();

    din.skipBytes(4);

    echoType = din.readUnsignedShort();
    if (echoType != 0x408a) // only support vppi at this moment
      throw new IOException("Error reading CINRAD CC data: Unsupported level 2 data");

    int prodCode = din.readUnsignedShort();

    if (prodCode != 0x8003) // only support vppi at this moment
      throw new IOException("Error reading CINRAD CC data: Unsupported product: RHI/FFT");
    din.skipBytes(4);

    // remain2[660]
    for (int i = 0; i < sweepN; i++) {
      int maxV = din.readUnsignedShort();
      int maxL = din.readUnsignedShort();
      int binWidth = din.readUnsignedShort();
      int binNum = din.readUnsignedShort();
      int recordTotalNum = din.readUnsignedShort();
      din.skipBytes(8);
      elev[i] = din.readUnsignedShort();
      din.skipBytes(2);
      // System.out.println("bin num: " + binNum + " maxL " + maxL + " totalRNumber " + recordTotalNum);
    }
    // din.seek(1020);
    // int doffset = din.readInt();
    // System.out.println(" Offset: " + doffset);
  }
 
Example 10
Source File: Cinrad2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void readCC20Header(RandomAccessFile din) throws IOException {

    message_offset = 0;
    din.seek(message_offset);
    // site info 170
    din.skipBytes(62);

    // Message Header
    String stationId = din.readString(40);
    String stationNbr = din.readString(10);

    din.skipBytes(20);

    String clon = din.readString(16);
    String clat = din.readString(16);
    // latlon
    int lon = din.readInt();
    int lat = din.readInt();
    int hhh = din.readInt();

    din.skipBytes(40);

    // ObservationInfo
    short scanMode = convertunsignedByte2Short(din.readByte());
    if (scanMode == 10) {
      sweepN = 1;
    } else if (scanMode >= 100) {
      sweepN = scanMode - 100;
    } else {
      throw new IOException("Error reading CINRAD CC data: Unsupported product: RHI/FFT");
    }

    short syear = (short) din.readUnsignedShort();
    short smm = convertunsignedByte2Short(din.readByte());
    short sdd = convertunsignedByte2Short(din.readByte());
    short shh = convertunsignedByte2Short(din.readByte());
    short smi = convertunsignedByte2Short(din.readByte());
    short sss = convertunsignedByte2Short(din.readByte());

    dateTime0 = CalendarDate.of(null, syear, smm, sdd, shh, smi, sss);

    din.skipBytes(14); // 14+ (35 - 21)

    // remain2[660]
    elev = new int[sweepN];
    recordNum = new int[sweepN];
    for (int i = 0; i < sweepN; i++) {
      din.skipBytes(14);
      int zbinWidth = din.readUnsignedShort();
      int vbinWidth = din.readUnsignedShort();
      int sbinWidth = din.readUnsignedShort();
      int zbinNum = din.readUnsignedShort();
      int vbinNum = din.readUnsignedShort();
      int sbinNum = din.readUnsignedShort();
      recordNum[i] = din.readUnsignedShort();
      // if(i > 0)
      // recordNum[i] = recordNum[i] + recordNum[i-1];
      elev[i] = din.readShort();
      cDataForm = din.readByte();
      if (cDataForm != 22 && cDataForm != 23 && cDataForm != 24)
        throw new IOException("Unsupported CC data format");
      int dataP = din.readInt();
      // din.skipBytes(2);
      // System.out.println("zbin num: " + zbinNum + " vbin num: " + vbinNum + " sbin num: " + sbinNum + " dataForm " +
      // cDataForm);
    }

    for (int i = sweepN; i < 32; i++) {
      din.skipBytes(35);
    }

    din.skipBytes(6);

    syear = (short) din.readUnsignedShort();
    smm = convertunsignedByte2Short(din.readByte());
    sdd = convertunsignedByte2Short(din.readByte());
    shh = convertunsignedByte2Short(din.readByte());
    smi = convertunsignedByte2Short(din.readByte());
    sss = convertunsignedByte2Short(din.readByte());

    dateTimeE = CalendarDate.of(null, syear, smm, sdd, shh, smi, sss);

  }
 
Example 11
Source File: Nldn.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Check if this is a valid file for this IOServiceProvider.
 * You must make this method thread safe, ie dont keep any state.
 *
 * @param raf RandomAccessFile
 * @return true if valid.
 * @throws IOException if read error
 */
public boolean isValidFile(RandomAccessFile raf) throws IOException {
  raf.seek(0);
  String test = raf.readString(MAGIC.length());
  return test.equals(MAGIC);
}