Skip to content

Commit 995d6af

Browse files
ctruedengab1one
authored andcommitted
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 2fd0e37 commit 995d6af

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

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

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

0 commit comments

Comments
 (0)