X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fstreams%2FReplaceOutputStream.java;h=c6679cc3b3eef4ad15e50e022013663c3b75dade;hb=6d765e484c2f50fd8f5cb3af425b59c51adaca0d;hp=dcb020770e003379cd88953d27d4d8bd4bc625bb;hpb=627f866e6cd474bd7494750dc3846c5ad898f2ec;p=fanfix.git diff --git a/src/be/nikiroo/utils/streams/ReplaceOutputStream.java b/src/be/nikiroo/utils/streams/ReplaceOutputStream.java index dcb0207..c6679cc 100644 --- a/src/be/nikiroo/utils/streams/ReplaceOutputStream.java +++ b/src/be/nikiroo/utils/streams/ReplaceOutputStream.java @@ -3,6 +3,8 @@ package be.nikiroo.utils.streams; import java.io.IOException; import java.io.OutputStream; +import be.nikiroo.utils.StringUtils; + /** * This {@link OutputStream} will change some of its content by replacing it * with something else. @@ -25,7 +27,7 @@ public class ReplaceOutputStream extends BufferedOutputStream { * the {@link String} to replace with */ public ReplaceOutputStream(OutputStream out, String from, String to) { - this(out, StreamUtils.bytes(from), StreamUtils.bytes(to)); + this(out, StringUtils.getBytes(from), StringUtils.getBytes(to)); } /** @@ -58,7 +60,7 @@ public class ReplaceOutputStream extends BufferedOutputStream { * the values to replace with */ public ReplaceOutputStream(OutputStream out, String[] froms, String[] tos) { - this(out, StreamUtils.bytes(froms), StreamUtils.bytes(tos)); + this(out, StreamUtils.getBytes(froms), StreamUtils.getBytes(tos)); } /** @@ -88,9 +90,30 @@ public class ReplaceOutputStream extends BufferedOutputStream { this.tos = tos; } + /** + * Flush the {@link BufferedOutputStream}, write the current buffered data + * to (and optionally also flush) the under-laying stream. + *

+ * If {@link BufferedOutputStream#bypassFlush} is false, all writes to the + * under-laying stream are done in this method. + *

+ * This can be used if you want to write some data in the under-laying + * stream yourself (in that case, flush this {@link BufferedOutputStream} + * with or without flushing the under-laying stream, then you can write to + * the under-laying stream). + *

+ * But be careful! If a replacement could be done with the end o the + * currently buffered data and the start of the data to come, we obviously + * will not be able to do it. + * + * @param includingSubStream + * also flush the under-laying stream + * @throws IOException + * in case of I/O error + */ @Override - protected void flush(boolean includingSubStream) throws IOException { - // Note: very simple, not efficient implementation, sorry. + public void flush(boolean includingSubStream) throws IOException { + // Note: very simple, not efficient implementation; sorry. while (start < stop) { boolean replaced = false; for (int i = 0; i < froms.length; i++) {