Skip to content

Commit d204f7a

Browse files
committed
🐛 Fixed bug in ReplacingInputStream when used with ByteArrayInputStream
The code that was reading didn't handle reads with offsets properly.
1 parent 3cc4ea0 commit d204f7a

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

rest-services/client/src/main/java/com/_4point/aem/docservices/rest_services/client/helpers/ReplacingInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public int read(byte[] ba) throws IOException {
110110
public int read(byte[] ba, int off, int len) throws IOException {
111111
for (int i = off; i < len; i++) {
112112
int b = read();
113-
if (b == -1) return i == 0 ? -1 : i; // If we haven't read anything, return -1 otherwise, return the number of bytes we have read.
113+
if (b == -1) return (i - off) == 0 ? -1 : i - off; // If we haven't read anything, return -1 otherwise, return the number of bytes we have read.
114114
ba[i] = (byte)b;
115115
}
116116
return len;

rest-services/client/src/test/java/com/_4point/aem/docservices/rest_services/client/helpers/ReplacingInputStreamTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.stream.Stream;
1111

1212
import org.apache.commons.io.IOUtils;
13+
import org.junit.jupiter.api.Test;
1314
import org.junit.jupiter.params.ParameterizedTest;
1415
import org.junit.jupiter.params.provider.Arguments;
1516
import org.junit.jupiter.params.provider.MethodSource;
@@ -74,4 +75,28 @@ void shouldReplaceUsingStringConstructor(String input, String pattern, String re
7475
}
7576
}
7677
}
78+
79+
private static final String MODIFICATION_TARGETS_FORMAT_STR = """
80+
'contextPath = %sresult[1];'
81+
'"%s/etc.clientlibs/toggles.json"'
82+
""";
83+
84+
@Test
85+
void shouldWorkWithByteArrayInputStream() throws IOException {
86+
String aemResponseText = "Value should be modified. " + MODIFICATION_TARGETS_FORMAT_STR.formatted("", "");
87+
String expectedResult = "Value should be modified. " + MODIFICATION_TARGETS_FORMAT_STR.formatted("\" + /aem\" + ", "");
88+
89+
byte[] aemResponseBytes = aemResponseText.getBytes();
90+
byte[] expectedResultBytes = expectedResult.getBytes();
91+
String target = "contextPath = result[1];";
92+
93+
String replacement = "contextPath = \" + /aem\" + result[1];";
94+
try (ReplacingInputStream replacingInputStream = new ReplacingInputStream(new ByteArrayInputStream(aemResponseBytes), target, replacement)) {
95+
byte[] result = replacingInputStream.readAllBytes();
96+
97+
assertArrayEquals(aemResponseBytes, new ByteArrayInputStream(aemResponseBytes).readAllBytes());
98+
assertArrayEquals(expectedResultBytes, result);
99+
}
100+
}
101+
77102
}

rest-services/client/src/test/java/com/_4point/aem/docservices/rest_services/client/helpers/ReplacingOutputStreamTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.stream.Stream;
1111

1212
import org.apache.commons.io.IOUtils;
13+
import org.junit.jupiter.api.Test;
1314
import org.junit.jupiter.params.ParameterizedTest;
1415
import org.junit.jupiter.params.provider.Arguments;
1516
import org.junit.jupiter.params.provider.MethodSource;
@@ -73,4 +74,29 @@ void shouldReplaceUsingStringConstructor(String input, String pattern, String re
7374
assertEquals(expected, bos.toString());
7475
}
7576
}
77+
78+
79+
private static final String MODIFICATION_TARGETS_FORMAT_STR = """
80+
'contextPath = %sresult[1];'
81+
'"%s/etc.clientlibs/toggles.json"'
82+
""";
83+
84+
@Test
85+
void shouldWorkWithByteArrayOutputStream() throws IOException {
86+
String aemResponseText = "Value should be modified. " + MODIFICATION_TARGETS_FORMAT_STR.formatted("", "");
87+
String expectedResult = "Value should be modified. " + MODIFICATION_TARGETS_FORMAT_STR.formatted("\" + /aem\" + ", "");
88+
89+
byte[] aemResponseBytes = aemResponseText.getBytes();
90+
byte[] expectedResultBytes = expectedResult.getBytes();
91+
String target = "contextPath = result[1];";
92+
93+
String replacement = "contextPath = \" + /aem\" + result[1];";
94+
var os = new ByteArrayOutputStream();
95+
try (os; ReplacingOutputStream replacingOutputStream = new ReplacingOutputStream(os , target, replacement)) {
96+
replacingOutputStream.write(aemResponseBytes);
97+
98+
}
99+
assertArrayEquals(expectedResultBytes, os.toByteArray());
100+
}
101+
76102
}

0 commit comments

Comments
 (0)