Esempio n. 1
0
  /** Read a segment from an input stream. */
  static Segment read(java.io.BufferedReader is, char encoding[]) throws Exception {

    String line = is.readLine();
    Segment seg = null;

    seg = new Segment(line.substring(0, 3));
    char dlim = line.charAt(3);
    String data = line.substring(4);

    int start = 1;

    if (seg.name.equals("MSH")) {

      // get the encoding characters out first
      DelimitedString ds = new DelimitedString(data, dlim, ' ');
      String enc = ds.getValue();

      encoding[0] = dlim;
      encoding[1] = enc.charAt(0);
      encoding[2] = enc.charAt(1);
      if (enc.length() > 2) encoding[3] = enc.charAt(2);
      if (enc.length() > 3) encoding[4] = enc.charAt(3);

      data = line.substring(5 + enc.length());
      start = 3;
    }

    DelimitedString ds = new DelimitedString(data, encoding[0], encoding[3]);

    while (ds.hasValues()) {
      String val = ds.getValue();
      Field fld = new Field();
      fld.decode(val, encoding);
      seg.setField(start, fld);
      start++;
    }

    return seg;
  }
  /**
   * Method for reading in data file, in a given format. Namely:
   *
   * <pre>
   * 881003,0.0000,14.1944,13.9444,14.0832,2200050,0
   * 881004,0.0000,14.1668,14.0556,14.1668,1490850,0
   * ...
   * 990108,35.8125,36.7500,35.5625,35.8125,4381200,0
   * 990111,35.8125,35.8750,34.8750,35.1250,3920800,0
   * 990112,34.8750,34.8750,34.0000,34.0625,3577500,0
   * </pre>
   *
   * <p>Where the fields represent, one believes, the following:
   *
   * <ol>
   *   <li>The date in 'YYMMDD' format
   *   <li>Open
   *   <li>High
   *   <li>Low
   *   <li>Last
   *   <li>Volume
   *   <li>Open Interest
   * </ol>
   *
   * One will probably make use of the closing price, but this can be redefined via the class
   * variable <code>DATUMFIELD</code>. Note that since the read in data are then used to compute the
   * return, this would be a good place to trap for zero values in the data, which will cause all
   * sorts of problems.
   *
   * @param dirName the directory in which to search for the data file.
   * @param filename the data filename itself.
   * @exception DemoException thrown if there was a problem with the data file.
   */
  private void readRatesFile(String dirName, String filename) throws DemoException {
    java.io.File ratesFile = new File(filename);
    java.io.BufferedReader in;
    try {
      InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(filename);

      //    if( ! ratesFile.canRead() ) {
      //      throw new DemoException("Cannot read the file "+ratesFile.toString());
      //    }

      in = new BufferedReader(new InputStreamReader(inputStream));
    } catch (Exception fnfex) {
      throw new DemoException(fnfex.toString());
    }
    //
    // Proceed to read all the lines of data into a Vector object.
    int iLine = 0, initNlines = 100, nLines = 0;

    String aLine;
    java.util.Vector allLines = new Vector(initNlines);
    try {
      while ((aLine = in.readLine()) != null) {
        iLine++;
        //
        // Note, I'm not entirely sure whether the object passed in is copied
        // by value, or just its reference.
        allLines.addElement(aLine);
      }
    } catch (IOException ioex) {
      throw new DemoException("Problem reading data from the file " + ioex.toString());
    }
    nLines = iLine;
    //
    // Now create an array to store the rates data.
    this.pathValue = new double[nLines];
    this.pathDate = new int[nLines];
    nAcceptedPathValue = 0;
    iLine = 0;
    for (java.util.Enumeration enum1 = allLines.elements(); enum1.hasMoreElements(); ) {
      aLine = (String) enum1.nextElement();
      String[] field = Utilities.splitString(",", aLine);
      int aDate = Integer.parseInt("19" + field[0]);
      //
      // static double Double.parseDouble() method is a feature of JDK1.2!
      double aPathValue = Double.valueOf(field[DATUMFIELD]).doubleValue();
      if ((aDate <= MINIMUMDATE) || (Math.abs(aPathValue) < EPSILON)) {
        dbgPrintln("Skipped erroneous data in " + filename + " indexed by date=" + field[0] + ".");
      } else {
        pathDate[iLine] = aDate;
        pathValue[iLine] = aPathValue;
        iLine++;
      }
    }
    //
    // Record the actual number of accepted data points.
    nAcceptedPathValue = iLine;
    //
    // Now to fill in the structures from the 'PathId' class.
    set_name(ratesFile.getName());
    set_startDate(pathDate[0]);
    set_endDate(pathDate[nAcceptedPathValue - 1]);
    set_dTime((double) (1.0 / 365.0));
  }