Skip to content
Merged
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
11 changes: 9 additions & 2 deletions src/main/java/loci/common/NIOFileHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public class NIOFileHandle extends AbstractNIOHandle {
/** The original length of the file. */
private Long defaultLength;

private long effectiveLength;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I correct in assuming that the addition of a private field means this would break serialization when propagated up to the Bio-Formats stack

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily - readers shouldn't be caching open file handles, so I wouldn't think instances of NIOFileHandle would be present in memo files. For example:

https://github.com/openmicroscopy/bioformats/blob/develop/components/formats-api/src/loci/formats/FormatReader.java#L127

Probably merits a test before bumping the ome-common version number in Bio-Formats though, to be safe.


// -- Constructors --

/**
Expand All @@ -121,6 +123,7 @@ public NIOFileHandle(File file, String mode, int bufferSize)
mapMode = FileChannel.MapMode.READ_WRITE;
}
raf = new RandomAccessFile(file, mode);
effectiveLength = raf.length();
channel = raf.getChannel();
byteBufferProvider = new NIOByteBufferProvider(channel, mapMode);
buffer(position, 0);
Expand Down Expand Up @@ -199,8 +202,9 @@ public int getBufferSize() {
@Override
public void setLength(long length) throws IOException {
if (raf.length() < length) {
raf.setLength(length);
raf.setLength(length + getBufferSize());
}
effectiveLength = length;
raf.seek(length - 1);
buffer = null;
}
Expand All @@ -210,6 +214,9 @@ public void setLength(long length) throws IOException {
/* @see IRandomAccess.close() */
@Override
public void close() throws IOException {
if (isReadWrite && channel.isOpen() && raf.length() != length()) {
raf.setLength(length());
}
raf.close();
}

Expand All @@ -225,7 +232,7 @@ public long length() throws IOException {
if (defaultLength != null) {
return defaultLength;
}
return raf.length();
return effectiveLength;
}

/* @see IRandomAccess.getOrder() */
Expand Down