fix limit in replace for BufferedInputStream
[nikiroo-utils.git] / src / be / nikiroo / utils / streams / ReplaceInputStream.java
CommitLineData
8e76f6ab 1package be.nikiroo.utils.streams;
6ef54b36
NR
2
3import java.io.IOException;
4import java.io.InputStream;
6ef54b36 5
f8147a0e
NR
6import be.nikiroo.utils.StringUtils;
7
876dbf8b 8/**
c8ce09c4
NR
9 * This {@link InputStream} will change some of its content by replacing it with
10 * something else.
876dbf8b
NR
11 *
12 * @author niki
13 */
6ef54b36 14public class ReplaceInputStream extends BufferedInputStream {
7194ac50
NR
15 /**
16 * The minimum size of the internal buffer (could be more if at least one of
17 * the 'FROM' bytes arrays is > 2048 bytes — in that case the
18 * buffer will be twice the largest size of the 'FROM' bytes arrays).
19 * <p>
20 * This is a different buffer than the one from the inherited class.
21 */
22 static private final int MIN_BUFFER_SIZE = 4096;
23
627f866e
NR
24 private byte[][] froms;
25 private byte[][] tos;
7194ac50 26 private int maxFromSize;
627f866e 27 private int maxToSize;
6ef54b36
NR
28
29 private byte[] source;
30 private int spos;
31 private int slen;
32
876dbf8b
NR
33 /**
34 * Create a {@link ReplaceInputStream} that will replace <tt>from</tt> with
35 * <tt>to</tt>.
36 *
37 * @param in
38 * the under-laying {@link InputStream}
39 * @param from
40 * the {@link String} to replace
41 * @param to
42 * the {@link String} to replace with
43 */
6ef54b36 44 public ReplaceInputStream(InputStream in, String from, String to) {
f8147a0e 45 this(in, StringUtils.getBytes(from), StringUtils.getBytes(to));
6ef54b36
NR
46 }
47
876dbf8b
NR
48 /**
49 * Create a {@link ReplaceInputStream} that will replace <tt>from</tt> with
50 * <tt>to</tt>.
51 *
52 * @param in
53 * the under-laying {@link InputStream}
54 * @param from
55 * the value to replace
56 * @param to
57 * the value to replace with
58 */
6ef54b36 59 public ReplaceInputStream(InputStream in, byte[] from, byte[] to) {
627f866e
NR
60 this(in, new byte[][] { from }, new byte[][] { to });
61 }
62
63 /**
64 * Create a {@link ReplaceInputStream} that will replace all <tt>froms</tt>
65 * with <tt>tos</tt>.
66 * <p>
67 * Note that they will be replaced in order, and that for each <tt>from</tt>
68 * a <tt>to</tt> must correspond.
69 *
70 * @param in
71 * the under-laying {@link InputStream}
72 * @param froms
73 * the values to replace
74 * @param tos
75 * the values to replace with
76 */
77 public ReplaceInputStream(InputStream in, String[] froms, String[] tos) {
f8147a0e 78 this(in, StreamUtils.getBytes(froms), StreamUtils.getBytes(tos));
627f866e
NR
79 }
80
81 /**
82 * Create a {@link ReplaceInputStream} that will replace all <tt>froms</tt>
83 * with <tt>tos</tt>.
84 * <p>
85 * Note that they will be replaced in order, and that for each <tt>from</tt>
86 * a <tt>to</tt> must correspond.
87 *
88 * @param in
89 * the under-laying {@link InputStream}
90 * @param froms
91 * the values to replace
92 * @param tos
93 * the values to replace with
94 */
95 public ReplaceInputStream(InputStream in, byte[][] froms, byte[][] tos) {
6ef54b36 96 super(in);
627f866e
NR
97
98 if (froms.length != tos.length) {
99 throw new IllegalArgumentException(
100 "For replacing, each FROM must have a corresponding TO");
101 }
102
103 this.froms = froms;
104 this.tos = tos;
105
7194ac50
NR
106 maxFromSize = 0;
107 for (int i = 0; i < froms.length; i++) {
108 maxFromSize = Math.max(maxFromSize, froms[i].length);
109 }
110
111 maxToSize = 0;
627f866e
NR
112 for (int i = 0; i < tos.length; i++) {
113 maxToSize = Math.max(maxToSize, tos[i].length);
114 }
6ef54b36 115
7194ac50 116 // We need at least maxFromSize so we can iterate and replace
6d765e48
NR
117 source = new byte[Math.max(2 * Math.max(maxToSize, maxFromSize),
118 MIN_BUFFER_SIZE)];
6ef54b36
NR
119 spos = 0;
120 slen = 0;
121 }
122
123 @Override
028ff7c2
NR
124 protected int read(InputStream in, byte[] buffer, int off, int len)
125 throws IOException {
126 if (len < maxToSize || source.length < maxToSize * 2) {
6ef54b36 127 throw new IOException(
627f866e 128 "An underlaying buffer is too small for these replace values");
6ef54b36
NR
129 }
130
7194ac50
NR
131 // We need at least one byte of data to process
132 if (available() < Math.max(maxFromSize, 1) && !eof) {
6ef54b36
NR
133 spos = 0;
134 slen = in.read(source);
135 }
136
7194ac50 137 // Note: very simple, not efficient implementation; sorry.
6ef54b36 138 int count = 0;
028ff7c2 139 while (spos < slen && count < len - maxToSize) {
627f866e
NR
140 boolean replaced = false;
141 for (int i = 0; i < froms.length; i++) {
142 if (froms[i] != null && froms[i].length > 0
143 && StreamUtils.startsWith(froms[i], source, spos, slen)) {
144 if (tos[i] != null && tos[i].length > 0) {
aaef3a51 145 System.arraycopy(tos[i], 0, buffer, off + count,
028ff7c2 146 tos[i].length);
627f866e
NR
147 count += tos[i].length;
148 }
aaef3a51 149
627f866e
NR
150 spos += froms[i].length;
151 replaced = true;
152 break;
153 }
154 }
155
156 if (!replaced) {
028ff7c2 157 buffer[off + count++] = source[spos++];
6ef54b36
NR
158 }
159 }
160
161 return count;
162 }
6ef54b36 163}