Skip to content

Commit

Permalink
Memoizer: ask any ImageReader instances to remove all but the current…
Browse files Browse the repository at this point in the history
… reader
  • Loading branch information
melissalinkert committed Nov 13, 2024
1 parent 783bb54 commit 384153d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
18 changes: 18 additions & 0 deletions components/formats-api/src/loci/formats/ImageReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ public ImageReader(ClassList<IFormatReader> 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.
Expand Down
11 changes: 11 additions & 0 deletions components/formats-bsd/src/loci/formats/Memoizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 384153d

Please sign in to comment.