From aaef3a5165f7cd1dc1a5babb104e328d13859408 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Tue, 19 May 2020 21:14:27 +0200 Subject: [PATCH] eplaceInputStream: fix more-than-one replaces --- .../utils/streams/ReplaceInputStream.java | 4 ++-- .../test_code/ReplaceInputStreamTest.java | 21 ++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/be/nikiroo/utils/streams/ReplaceInputStream.java b/src/be/nikiroo/utils/streams/ReplaceInputStream.java index 1cc5139b..9f733504 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 e6e21121..d08a91ed 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, -- 2.27.0