Skip to content

Commit f0780ac

Browse files
committed
DataHandle: fix findString(..) and support handles with unknown length
- The findString(..) method used to require the exact length of the handle, this length can be unknown, e.g. for compressed handles. This is no longer the case
1 parent 6c094f7 commit f0780ac

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,10 @@ default String findString(final boolean saveString, final int blockSize,
366366
new DataHandleInputStream<>(this), getEncoding());
367367
final char[] buf = new char[blockSize];
368368
long loc = 0;
369-
while (loc < maxLen && offset() < length() - 1) {
369+
int r = 0;
370+
371+
// NB: we need at least 2 bytes to read a char
372+
while (loc < maxLen && ((r = in.read(buf, 0, blockSize)) > 1)) {
370373
// if we're not saving the string, drop any old, unnecessary output
371374
if (!saveString) {
372375
final int outLen = out.length();
@@ -378,16 +381,12 @@ default String findString(final boolean saveString, final int blockSize,
378381
bytesDropped += dropIndex;
379382
}
380383
}
381-
382-
// read block from stream
383-
final int r = in.read(buf, 0, blockSize);
384-
if (r <= 0) throw new IOException("Cannot read from stream: " + r);
385-
386384
// append block to output
387385
out.append(buf, 0, r);
388386

389387
// check output, returning smallest possible string
390-
int min = Integer.MAX_VALUE, tagLen = 0;
388+
int min = Integer.MAX_VALUE;
389+
int tagLen = 0;
391390
for (final String t : terminators) {
392391
final int len = t.length();
393392
final int start = (int) (loc - bytesDropped - len);

0 commit comments

Comments
 (0)