X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fstreams%2FReplaceOutputStream.java;h=c6679cc3b3eef4ad15e50e022013663c3b75dade;hb=919bbc354cd2555eb0955be0ef2dcf338047d022;hp=ff871eb815d4b5a8672cfd746449a09524684968;hpb=12784931c8ae440fec10dfd6ea97e7b16ba64988;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/streams/ReplaceOutputStream.java b/src/be/nikiroo/utils/streams/ReplaceOutputStream.java deleted file mode 100644 index ff871eb..0000000 --- a/src/be/nikiroo/utils/streams/ReplaceOutputStream.java +++ /dev/null @@ -1,125 +0,0 @@ -package be.nikiroo.utils.streams; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * This {@link OutputStream} will change some of its content by replacing it - * with something else. - * - * @author niki - */ -public class ReplaceOutputStream extends BufferedOutputStream { - private byte[][] froms; - private byte[][] tos; - - /** - * Create a {@link ReplaceOutputStream} that will replace from with - * to. - * - * @param out - * the under-laying {@link OutputStream} - * @param from - * the {@link String} to replace - * @param to - * the {@link String} to replace with - */ - public ReplaceOutputStream(OutputStream out, String from, String to) { - this(out, StreamUtils.bytes(from), StreamUtils.bytes(to)); - } - - /** - * Create a {@link ReplaceOutputStream} that will replace from with - * to. - * - * @param out - * the under-laying {@link OutputStream} - * @param from - * the value to replace - * @param to - * the value to replace with - */ - public ReplaceOutputStream(OutputStream out, byte[] from, byte[] to) { - this(out, new byte[][] { from }, new byte[][] { to }); - } - - /** - * Create a {@link ReplaceOutputStream} that will replace all froms - * with tos. - *

- * Note that they will be replaced in order, and that for each from - * a to must correspond. - * - * @param out - * the under-laying {@link OutputStream} - * @param froms - * the values to replace - * @param tos - * the values to replace with - */ - public ReplaceOutputStream(OutputStream out, String[] froms, String[] tos) { - this(out, StreamUtils.bytes(froms), StreamUtils.bytes(tos)); - } - - /** - * Create a {@link ReplaceOutputStream} that will replace all froms - * with tos. - *

- * Note that they will be replaced in order, and that for each from - * a to must correspond. - * - * @param out - * the under-laying {@link OutputStream} - * @param froms - * the values to replace - * @param tos - * the values to replace with - */ - public ReplaceOutputStream(OutputStream out, byte[][] froms, byte[][] tos) { - super(out); - bypassFlush = false; - - if (froms.length != tos.length) { - throw new IllegalArgumentException( - "For replacing, each FROM must have a corresponding TO"); - } - - this.froms = froms; - this.tos = tos; - } - - @Override - 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++) { - if (froms[i] != null - && froms[i].length > 0 - && StreamUtils - .startsWith(froms[i], buffer, start, stop)) { - if (tos[i] != null && tos[i].length > 0) { - out.write(tos[i]); - bytesWritten += tos[i].length; - } - - start += froms[i].length; - replaced = true; - break; - } - } - - if (!replaced) { - out.write(buffer[start++]); - bytesWritten++; - } - } - - start = 0; - stop = 0; - - if (includingSubStream) { - out.flush(); - } - } -}