X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fstreams%2FNextableInputStream.java;h=62156e3e4285f53bb1d0e72e4be95f0f91ea3452;hb=12784931c8ae440fec10dfd6ea97e7b16ba64988;hp=d023ad34482837b7896d75482d0bff85b98f9aa1;hpb=028ff7c29a8fb07c702ffd34913a5dd22b688211;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/streams/NextableInputStream.java b/src/be/nikiroo/utils/streams/NextableInputStream.java index d023ad3..62156e3 100644 --- a/src/be/nikiroo/utils/streams/NextableInputStream.java +++ b/src/be/nikiroo/utils/streams/NextableInputStream.java @@ -2,6 +2,8 @@ package be.nikiroo.utils.streams; import java.io.IOException; import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.util.Arrays; /** * This {@link InputStream} can be separated into sub-streams (you can process @@ -85,6 +87,8 @@ public class NextableInputStream extends BufferedInputStream { * {@link IOException}s can happen when we have no data available in the * buffer; in that case, we fetch more data to know if we can have a next * sub-stream or not. + *

+ * This is can be a blocking call when data need to be fetched. * * @return TRUE if we unblocked the next sub-stream, FALSE if not * @@ -101,6 +105,8 @@ public class NextableInputStream extends BufferedInputStream { *

* That is, the next stream, if any, will be the last one and will not be * subject to the {@link NextableInputStreamStep}. + *

+ * This is can be a blocking call when data need to be fetched. * * @return TRUE if we unblocked the next sub-stream, FALSE if not * @@ -192,6 +198,8 @@ public class NextableInputStream extends BufferedInputStream { /** * The implementation of {@link NextableInputStream#next()} and * {@link NextableInputStream#nextAll()}. + *

+ * This is can be a blocking call when data need to be fetched. * * @param all * TRUE for {@link NextableInputStream#nextAll()}, FALSE for @@ -212,8 +220,6 @@ public class NextableInputStream extends BufferedInputStream { if (all) { step = null; } - - return true; } if (step != null && !hasMoreData() && stopped) { @@ -227,11 +233,35 @@ public class NextableInputStream extends BufferedInputStream { } checkBuffer(false); + } + + // consider that if EOF, there is no next + if (start >= stop) { + // Make sure, block if necessary + preRead(); - // consider that if EOF, there is no next return hasMoreData(); } - return false; + return true; + } + + public String DEBUG() { + String data = ""; + if (stop > 0) { + try { + data = new String(Arrays.copyOfRange(buffer, 0, stop), "UTF-8"); + } catch (UnsupportedEncodingException e) { + } + if (data.length() > 50) { + data = data.substring(0, 47) + "..."; + } + } + String rep = String.format( + "Nextable %s: %d -> %d [eof: %s] [more data: %s]: %s", + (stopped ? "stopped" : "running"), start, stop, "" + eof, "" + + hasMoreData(), data); + + return rep; } }