X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=sidebyside;f=src%2Fbe%2Fnikiroo%2Futils%2Fstreams%2FNextableInputStream.java;h=dcab472efa0569bf6763425567330a536ac3eeeb;hb=cd3d510fdeb0ad53ae39e1fda34acf328c70c345;hp=b8c3fe8c5baf45b0c8b3adffa5cda11c53b99c79;hpb=177e14c96d0c7318ae4a227351186735c9b5f1a6;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/streams/NextableInputStream.java b/src/be/nikiroo/utils/streams/NextableInputStream.java index b8c3fe8..dcab472 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 @@ -79,16 +81,14 @@ public class NextableInputStream extends BufferedInputStream { * It can only be called when the "current" stream is spent (i.e., you must * first process the stream until it is spent). *
- * We consider that when the under-laying {@link InputStream} is also spent, - * we cannot have a next sub-stream (it will thus return FALSE). - *
* {@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 + * @return TRUE if we unblocked the next sub-stream, FALSE if not (i.e., + * FALSE when there are no more sub-streams to fetch) * * @throws IOException * in case of I/O error or if the stream is closed @@ -120,7 +120,7 @@ public class NextableInputStream extends BufferedInputStream { * process). *
* Note: when the stream is divided into sub-streams, each sub-stream will - * report it is eof when emptied. + * report its own eof when spent. * * @return TRUE if it is * @@ -156,11 +156,6 @@ public class NextableInputStream extends BufferedInputStream { return false; } - /** - * We have more data available in the buffer or we can fetch more. - * - * @return TRUE if it is the case, FALSE if not - */ @Override protected boolean hasMoreData() { return started && super.hasMoreData(); @@ -203,7 +198,8 @@ public class NextableInputStream extends BufferedInputStream { * TRUE for {@link NextableInputStream#nextAll()}, FALSE for * {@link NextableInputStream#next()} * - * @return TRUE if we unblocked the next sub-stream, FALSE if not + * @return TRUE if we unblocked the next sub-stream, FALSE if not (i.e., + * FALSE when there are no more sub-streams to fetch) * * @throws IOException * in case of I/O error or if the stream is closed @@ -218,9 +214,17 @@ public class NextableInputStream extends BufferedInputStream { if (all) { step = null; } + + return true; + } + + // If started, must be stopped and no more data to continue + // i.e., sub-stream must be spent + if (!stopped || hasMoreData()) { + return false; } - if (step != null && !hasMoreData() && stopped) { + if (step != null) { stop = step.getResumeLen(); start += step.getResumeSkip(); eof = step.getResumeEof(); @@ -231,24 +235,44 @@ public class NextableInputStream extends BufferedInputStream { } checkBuffer(false); - } - // consider that if EOF, there is no next - if (start >= stop) { - // Make sure, block if necessary - preRead(); - - return hasMoreData(); + return true; } - return true; + return false; + + // // consider that if EOF, there is no next + // if (start >= stop) { + // // Make sure, block if necessary + // preRead(); + // + // return hasMoreData(); + // } + // + // return true; } - public String DEBUG() { + /** + * Display a DEBUG {@link String} representation of this object. + *
+ * Do not use for release code. + */ + @Override + public String toString() { + String data = ""; + if (stop > 0) { + try { + data = new String(Arrays.copyOfRange(buffer, 0, stop), "UTF-8"); + } catch (UnsupportedEncodingException e) { + } + if (data.length() > 200) { + data = data.substring(0, 197) + "..."; + } + } String rep = String.format( - "Nextable %s: %d -> %d [eof: %s] [more data: %s]", + "Nextable %s: %d -> %d [eof: %s] [more data: %s]: %s", (stopped ? "stopped" : "running"), start, stop, "" + eof, "" - + hasMoreData()); + + hasMoreData(), data); return rep; }