Previous Next        Current Page: NeXtMidas Training / Primitives / Files in a Primitive / Reading Frame-Based Data
back
Start Here   
Background   
Common Midas Concepts   
Getting Started - Part 1   
Getting Started - Part 2   
Working with Files   
Option Trees   
Macros - Part 1 (Basics)   
Macros - Part 2 (Graphics)   
NetBeans - Part 1 (Setup)   
NetBeans - Part 2 (GUIs)   
NetBeans - Part 3 (Profiler)   
Eclipse - Part 1 (Setup)   
Eclipse - Part 2 (GUIs)   
Primitives   
   + Overview   
   + Open, Process, Close   
   + Building   
   + The NeXtMidas API   
   + Lab 1   
   + Special Variables   
   + Lab 2   
   + Lab 3*   
   - Files in a Primitive   
      - File Categories   
      - Getting Files   
      - Reading Frame-Based Data   
      - Writing Frame-Based Data   
      - Reading Record-Based Data   
      - Writing Record-Based Data   
      - A Note About Files   
   + Lab 4   
   + Lab 5*   
   + Test Macros   
   + Lab 6   
   + Real Time Controls   
   + Lab 7   
   + Working with Messages   
   + Lab 8   
   + Primitive Restarts   
   + Introduction to DSP   
   + Lab 9   
WebStart   
Maps & Imagery   
X-Midas Interoperability   
RMIF & Remoting   
Installing NeXtMidas   
Support & Maintenance   
File Handlers   


  • 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();
  ...
}

back