eplaceInputStream: fix more-than-one replaces
authorNiki Roo <niki@nikiroo.be>
Tue, 19 May 2020 19:14:27 +0000 (21:14 +0200)
committerNiki Roo <niki@nikiroo.be>
Tue, 19 May 2020 19:14:27 +0000 (21:14 +0200)
src/be/nikiroo/utils/streams/ReplaceInputStream.java
src/be/nikiroo/utils/test_code/ReplaceInputStreamTest.java

index 1cc5139beae1e012053f62dece957076add4dfee..9f733504117c314768603669b73cde0f7aa2ab40 100644 (file)
@@ -141,11 +141,11 @@ public class ReplaceInputStream extends BufferedInputStream {
                                if (froms[i] != null && froms[i].length > 0
                                                && StreamUtils.startsWith(froms[i], source, spos, slen)) {
                                        if (tos[i] != null && tos[i].length > 0) {
-                                               System.arraycopy(tos[i], 0, buffer, off + spos,
+                                               System.arraycopy(tos[i], 0, buffer, off + count,
                                                                tos[i].length);
                                                count += tos[i].length;
                                        }
-
+                                       
                                        spos += froms[i].length;
                                        replaced = true;
                                        break;
index e6e211215430485cfd0938b5fcb89f6fc5ee4a5e..d08a91ed779c89502ef8e7d65eca8e47b7fa9ddd 100644 (file)
@@ -48,7 +48,7 @@ class ReplaceInputStreamTest extends TestLauncher {
                        }
                });
 
-               addTest(new TestCase("Lnger replace") {
+               addTest(new TestCase("Longer replace") {
                        @Override
                        public void test() throws Exception {
                                byte[] data = new byte[] { 42, 12, 0, 127 };
@@ -79,17 +79,32 @@ class ReplaceInputStreamTest extends TestLauncher {
                                byte[] data = "I like red".getBytes("UTF-8");
                                ReplaceInputStream in = new ReplaceInputStream(
                                                new ByteArrayInputStream(data),
-                                               "red".getBytes("UTF-8"), "blue".getBytes("UTF-8"));
+                                               "red", "blue");
 
                                checkArrays(this, "FIRST", in, "I like blue".getBytes("UTF-8"));
 
                                data = "I like blue".getBytes("UTF-8");
                                in = new ReplaceInputStream(new ByteArrayInputStream(data),
-                                               "blue".getBytes("UTF-8"), "red".getBytes("UTF-8"));
+                                               "blue", "red");
 
                                checkArrays(this, "FIRST", in, "I like red".getBytes("UTF-8"));
                        }
                });
+               
+               addTest(new TestCase("Multiple replaces") {
+                       @Override
+                       public void test() throws Exception {
+                               byte[] data = "I like red".getBytes("UTF-8");
+                               ReplaceInputStream in = new ReplaceInputStream(
+                                               new ByteArrayInputStream(data), //
+                                               new String[] {"like", "red"}, //
+                                               new String[] {"dislike", "green"} //
+                               );
+
+                               String result = new String(IOUtils.toByteArray(in), "UTF-8");
+                               assertEquals("I dislike green", result);
+                       }
+               });
        }
 
        static void checkArrays(TestCase test, String prefix, InputStream in,