From: Niki Roo Date: Tue, 19 May 2020 19:14:27 +0000 (+0200) Subject: eplaceInputStream: fix more-than-one replaces X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=aaef3a5165f7cd1dc1a5babb104e328d13859408;p=nikiroo-utils.git eplaceInputStream: fix more-than-one replaces --- diff --git a/src/be/nikiroo/utils/streams/ReplaceInputStream.java b/src/be/nikiroo/utils/streams/ReplaceInputStream.java index 1cc5139..9f73350 100644 --- a/src/be/nikiroo/utils/streams/ReplaceInputStream.java +++ b/src/be/nikiroo/utils/streams/ReplaceInputStream.java @@ -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; diff --git a/src/be/nikiroo/utils/test_code/ReplaceInputStreamTest.java b/src/be/nikiroo/utils/test_code/ReplaceInputStreamTest.java index e6e2112..d08a91e 100644 --- a/src/be/nikiroo/utils/test_code/ReplaceInputStreamTest.java +++ b/src/be/nikiroo/utils/test_code/ReplaceInputStreamTest.java @@ -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,