Skip to content

Commit efec596

Browse files
committed
DataHandle: add ensureWritable method
In SCIFIO and Bio-Formats, this was present as the method AbstractNIOHandle#validateLength. But there is no reason to limit it to NIO-flavored handles only. We use the name ensureWritable for clarity, and for symmetry with a future ensureReadable method.
1 parent 05a0d2b commit efec596

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/main/java/org/scijava/io/handle/DataHandle.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,25 @@ public interface DataHandle<L extends Location> extends WrapperPlugin<L>,
7474
*/
7575
void setLength(long length) throws IOException;
7676

77+
/**
78+
* Ensures that the handle has the correct length to be written to and extends
79+
* it as required.
80+
*
81+
* @param count Number of bytes to write.
82+
* @return {@code true} if the handle's length was sufficient, or
83+
* {@code false} if the handle's length required an extension.
84+
* @throws IOException If something goes wrong with the check, or there is an
85+
* error changing the handle's length.
86+
*/
87+
default boolean ensureWritable(final long count) throws IOException {
88+
final long minLength = offset() + count;
89+
if (length() < minLength) {
90+
setLength(minLength);
91+
return false;
92+
}
93+
return true;
94+
}
95+
7796
/**
7897
* Returns the current order of the stream.
7998
*

0 commit comments

Comments
 (0)