Hello
I have a java application that opens and reads a SINGLE file. There is no problem reading small to medium sized files (1 - 100s Megabytes). However once the file get "Large" (>1Gig) i get the following exception:
java.io.FileNotFoundException: /nrims/home3/jconnell/data/MESTER/EXP1_090902/090909-5-1-4x999.nrrd (Too many open files)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:137)
at ij.io.FileOpener.createInputStream(FileOpener.java:437)
at ij.io.FileOpener.readPixels(FileOpener.java:490)
at ij.io.FileOpener.open(FileOpener.java:76)
at com.nrims.data.Nrrd_Reader.getPixels(Nrrd_Reader.java:293)
at com.nrims.MimsPlus.appendImage(MimsPlus.java:415)
at com.nrims.UI.loadMIMSFile(UI.java:529)
at com.nrims.UI.openFiles(UI.java:340)
at com.nrims.UI.loadMIMSFile(UI.java:325)
at com.nrims.UI.openMIMSImageMenuItemActionPerformed(UI.java:1685)
at com.nrims.UI.access$400(UI.java:58)
at com.nrims.UI$5.actionPerformed(UI.java:1222)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:374)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1688)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1732)
I do not trust the "Too many files open" error because I am only dealing with 1 file. And the exception only occurs if the file is large (> 1 Gig)
Here is the code for the methods i have access to that are referenced in the Exception...
The createInputStream method in the ij.io.FileOpener class:
/** Returns an InputStream for the image described by this FileInfo. */
public InputStream createInputStream(FileInfo fi) throws IOException, MalformedURLException {
InputStream is = null;
boolean gzip = fi.fileName!=null && (fi.fileName.endsWith(".gz")||fi.fileName.endsWith(".GZ"));
if (fi.inputStream!=null)
is = fi.inputStream;
else if (fi.url!=null && !fi.url.equals(""))
is = new URL(fi.url+fi.fileName).openStream();
else {
if (fi.directory.length()>0 && !fi.directory.endsWith(Prefs.separator))
fi.directory += Prefs.separator;
File f = new File(fi.directory + fi.fileName);
if (gzip) fi.compression = FileInfo.COMPRESSION_UNKNOWN;
if (f==null || f.isDirectory() || !validateFileInfo(f, fi))
is = null;
else
is = new FileInputStream(f);
}
if (is!=null) {
if (fi.compression>=FileInfo.LZW)
is = new RandomAccessStream(is);
else if (gzip)
is = new GZIPInputStream(is, 50000);
}
return is;
}
The getPixels method in the com.nrims.data.Nrrd_Reader class:
public short[] getPixels(int index) throws IndexOutOfBoundsException, IOException {
// Set up a temporary header to read the pixels from the file.
NrrdFileInfo fi;
try {
fi=getHeaderInfo();
} catch (IOException e) {
IJ.write("readHeader: "+ e.getMessage());
return null;
}
// Calculate offset
long offset = fi.longOffset + // move down header
(2 * index * fi.width * fi.height * fi.nImages) + // move down to correct channel
(2 * fi.width * fi.height * currentIndex); // move down to correct image within that channel
fi.longOffset = offset;
fi.nImages=1; // only going to read 1 image.
FileOpener fo = new FileOpener(fi);
// Get image from file.
ImagePlus imp = fo.open(false);
if(imp==null) return null;
return (short[])imp.getProcessor().getPixels();
}
Any idea on how to get around this error? Is there a fundamental limit in the java.io.FileInputStream.open() method?
Any input appreciated. Thanks.