1 package be
.nikiroo
.utils
.streams
;
3 import java
.io
.IOException
;
4 import java
.io
.OutputStream
;
7 * This {@link OutputStream} will change some of its content by replacing it
12 public class ReplaceOutputStream
extends BufferedOutputStream
{
17 * Create a {@link ReplaceOutputStream} that will replace <tt>from</tt> with
21 * the under-laying {@link OutputStream}
23 * the {@link String} to replace
25 * the {@link String} to replace with
27 public ReplaceOutputStream(OutputStream out
, String from
, String to
) {
28 this(out
, StreamUtils
.bytes(from
), StreamUtils
.bytes(to
));
32 * Create a {@link ReplaceOutputStream} that will replace <tt>from</tt> with
36 * the under-laying {@link OutputStream}
38 * the value to replace
40 * the value to replace with
42 public ReplaceOutputStream(OutputStream out
, byte[] from
, byte[] to
) {
51 protected void flush(boolean includingSubStream
) throws IOException
{
52 // Note: very simple, not efficient implementation, sorry.
53 while (start
< stop
) {
55 && StreamUtils
.startsWith(from
, buffer
, start
, stop
)) {
57 bytesWritten
+= to
.length
;
60 out
.write(buffer
[start
++]);
68 if (includingSubStream
) {