diff --git a/components/formats-api/src/loci/formats/ImageReader.java b/components/formats-api/src/loci/formats/ImageReader.java index 9a87aeec5c4..161d87888d6 100644 --- a/components/formats-api/src/loci/formats/ImageReader.java +++ b/components/formats-api/src/loci/formats/ImageReader.java @@ -150,6 +150,24 @@ public ImageReader(ClassList classList) { // -- ImageReader API methods -- + /** + * Intended for use prior to caching an initialized reader. + * This removes everything except the current reader from the + * list of possible readers to use. Be very careful, and do + * not call this method on an ImageReader that may be reused + * for multiple different files. + * + * If the current reader index is less than 0 (i.e. not initialized), + * this does nothing. + */ + protected void cleanupReaderList() { + if (current >= 0) { + IFormatReader currentReader = readers[current]; + current = 0; + readers = new IFormatReader[] {currentReader}; + } + } + /** * Toggles whether or not file system access is allowed when doing type * detection. By default, file system access is allowed. diff --git a/components/formats-bsd/src/loci/formats/Memoizer.java b/components/formats-bsd/src/loci/formats/Memoizer.java index edebb099a37..c312af980f0 100644 --- a/components/formats-bsd/src/loci/formats/Memoizer.java +++ b/components/formats-bsd/src/loci/formats/Memoizer.java @@ -208,6 +208,17 @@ public void saveRevision(String revision) { @Override public void saveReader(IFormatReader reader) { + // clean up reader list in any instances of ImageReader + IFormatReader r = reader; + while (r instanceof ReaderWrapper || r instanceof ImageReader) { + if (r instanceof ImageReader) { + ImageReader ir = (ImageReader) r; + ir.cleanupReaderList(); + r = ir.getReader(); + } + else r = ((ReaderWrapper) r).getReader(); + } + kryo.writeObject(output, reader.getClass()); kryo.writeObject(output, reader); }