Current Page: NeXtMidas Training Primitives Files in a Primitive Reading Frame-Based Data
- - - | Reading frame based data. All data is read into a `Data` buffer. The `Data` buffer is created in `open()` just after the file has been opened. `dataBuffer = inFile.getDataBuffer()` Primitives have a `xfer` variable (provided by `Primitive`) that holds the transfer length. Many primitives allow the value of `xfer` to be overridden by the `/TL=` switch using:
          xfer = MA.getL("/TL", xfer);
  - - Inside `process()` the data is read in from the file. `int numRead = inFile.read(dataBuffer)` `numRead` is set to:
      `numRead > 0` | `numRead` elements have been read.
      `numRead = 0` | No elements are ready for reading.
      `numRead < 0` | End of file has been reached.
    - The value of `numRead` usually indicates the return value from `process()`:
      `numRead > 0` | `NORMAL`
      `numRead = 0` | `NOOP`
      `numRead < 0` | `FINISH`
- Example:
      public class myprim extends Primitive {
        private DataFile inFile;      // The input file
        private Data     dataBuffer;  // The data buffer
        int xfer = 1024;

        public int open() {
          inFile  = MA.getDataFile("IN");
          xfer    = MA.getL("/TL", xfer);

          inFile.open();

          // This creates a new buffer and only uses the DataFile object to initialize the buffer's create parameters.
          // Alternate method is createDataBuffer(xfer)
          dataBuffer = inFile.getDataBuffer(xfer);

          return NORMAL;
        }

        public int process() {
          int status  = NORMAL;
          int numRead = inFile.read(dataBuffer);

          if (numRead < 0) {
            status = FINISH; // End of file
          } else if (numRead == 0) {
            status = NOOP;   // No data ready, wait for pipe.
          } else {
            // Process the data
          }

          return status;
        }

        public int close() {
          inFile.close();
          return NORMAL;
        }
      }
Don't Make This Mistake:
Do not forget to open your input/output files in your primitive's `open` method before reading from it, otherwise you will get an Exception.
        public int open() {
          inFile = MA.getFile("IN");
          inFile.open(); // do this before reading in the next line

          outFile = MA.getFile("OUT);
          outFile.open();
          ...
        }