Here we will make the necessary updates so that getSize() is correct and so
that the seek(), seek(..), and avail() methods work too.
The default implementations of these methods in BaseFile assume that everything
is measured in bytes and that there is no file header. We need to update these methods
so that they exclude the header and are measured in elements rather than bytes.
public double getSize () {
// your code goes here
}
public int seek (double off) {
double boff = ...; // <-- your code goes here
// The code below this line comes from BaseFile
offset = boff;
if (io!=null) io.seek((long)boff);
return 0; // <-- always returns 0
}
public double seek() {
// your code goes here
}
public double avail() {
// your code goes here
}
getSize()
This returns the size of the file in number of elements.
The getLength()
method in I/O Resource will tell us the total size of the file in bytes.
From this we need to subtract the size of the header (HEADER_SIZE) and then
convert from bytes to elements.
Here we use the constant BPE which is the number of bytes-per-element for
our data. In an ORANGE file all data is either SL or SF data which is always 4
bytes-per-element. If we had a more complicated file, we would probably have a
variable or method that held the current number of bytes-per-element.
seek() and seek(..)
The seek methods move the read/write pointer within the data portion of the file.
I/O resource provides methods to set/get the seek value
(seek() and
seek(long))
however (like getLength()) they are measured in bytes and include the
size of the header.
avail()
The avail method tells us how many elemets are available (starting at the current
read/write pointer) before the end of the file.
Here, if we use the seek() and getSize() we just wrote, we end
up with a simple subtraction problem.
Trying it out
Now now when you status the file you will see the correct size.
nM> status samplefile.orange
OrangeFile : file:///home/student/samplefile.orange
Size : 16
Format : SF
XStart : 0.0
XDelta : 1.0
After building the class, we need to add an entry to the file handlers table that
will tell NeXtMidas to use your new file handler when it sees an ORANGE file:
nM> set REG.HANDLERS.FILE.ORANGE "nxm.train.lib.OrangeFile"
Save the sample file samplefile.orange
to your local disk and try it out.
nM> status samplefile.orange
Solution
After completing Part 2, your class will look similar to
this.