Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memoizer: ask any ImageReader instances to remove all but the current reader #4254

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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