Skip to content

Commit 47f58af

Browse files
Add delegates to WrappedInputStream
WrappedInputStream now delegates all methods ti ServletInputStream. Note, that SerlevtInputStream does not support `mark`, so there are no tests for that feature. Implementation was taken from WrappedInputReader.
1 parent 17b768c commit 47f58af

File tree

3 files changed

+172
-44
lines changed

3 files changed

+172
-44
lines changed
Lines changed: 90 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sap.hcp.cf.logging.servlet.filter;
22

33
import java.io.IOException;
4+
import java.io.OutputStream;
45

56
import javax.servlet.ServletInputStream;
67

@@ -9,54 +10,100 @@
910
*/
1011
public class WrappedInputStream extends ServletInputStream {
1112

12-
private int contentLength = -1;
13-
private final ServletInputStream wrappedStream;
13+
private int contentLength = -1;
14+
private int markContentLength = -1;
15+
private final ServletInputStream wrappedStream;
1416

15-
public int getContentLength() {
16-
return contentLength;
17-
}
17+
public int getContentLength() {
18+
return contentLength;
19+
}
1820

19-
protected WrappedInputStream(ServletInputStream out) {
20-
wrappedStream = out;
21-
}
21+
protected WrappedInputStream(ServletInputStream in) {
22+
wrappedStream = in;
23+
}
2224

23-
@Override
24-
public int read() throws IOException {
25-
int c = wrappedStream.read();
25+
@Override
26+
public int read() throws IOException {
27+
int c = wrappedStream.read();
28+
if (c != -1) {
29+
incrContentLength(1);
30+
}
31+
return c;
32+
}
33+
34+
@Override
35+
public int readLine(byte[] b, int off, int len) throws IOException {
36+
int retLen = wrappedStream.readLine(b, off, len);
37+
if (retLen != -1) {
38+
incrContentLength(retLen);
39+
}
40+
return retLen;
41+
}
42+
43+
@Override
44+
public int read(byte[] b) throws IOException {
45+
int len = wrappedStream.read(b);
46+
if (len != -1) {
47+
incrContentLength(len);
48+
}
49+
return len;
50+
}
51+
52+
@Override
53+
public int read(byte[] b, int off, int len) throws IOException {
54+
int c = wrappedStream.read(b, off, len);
2655
if (c != -1) {
27-
incrContentLength(1);
56+
incrContentLength(c);
2857
}
2958
return c;
30-
}
59+
}
3160

32-
@Override
33-
public int readLine(byte[] b, int off, int len) throws IOException {
34-
int retLen = wrappedStream.readLine(b, off, len);
35-
if (retLen != -1) {
36-
incrContentLength(retLen);
37-
}
38-
return retLen;
39-
}
40-
41-
@Override
42-
public int read(byte[] b) throws IOException {
43-
int len = wrappedStream.read(b);
44-
if (len != -1) {
45-
incrContentLength(len);
46-
}
47-
return len;
48-
}
49-
50-
@Override
51-
public int read(byte[] b, int off, int len) throws IOException {
52-
return wrappedStream.read(b, off, len);
53-
}
54-
55-
private void incrContentLength(int i) {
56-
if (contentLength == -1) {
57-
contentLength = i;
58-
} else {
59-
contentLength += i;
60-
}
61-
}
61+
private void incrContentLength(int i) {
62+
if (contentLength == -1) {
63+
contentLength = i;
64+
} else {
65+
contentLength += i;
66+
}
67+
}
68+
69+
@Override
70+
public long skip(long n) throws IOException {
71+
long skipped = wrappedStream.skip(n);
72+
incrContentLength(toIntExact(skipped));
73+
return skipped;
74+
}
75+
76+
private static int toIntExact(long value) {
77+
if ((int) value != value) {
78+
throw new ArithmeticException("integer overflow");
79+
}
80+
return (int) value;
81+
}
82+
83+
@Override
84+
public int available() throws IOException {
85+
return wrappedStream.available();
86+
}
87+
88+
@Override
89+
public void close() throws IOException {
90+
wrappedStream.close();
91+
}
92+
93+
@Override
94+
public synchronized void mark(int readAheadLimit) {
95+
wrappedStream.mark(readAheadLimit);
96+
this.markContentLength = this.contentLength;
97+
}
98+
99+
@Override
100+
public synchronized void reset() throws IOException {
101+
wrappedStream.reset();
102+
this.contentLength = this.markContentLength;
103+
}
104+
105+
@Override
106+
public boolean markSupported() {
107+
return wrappedStream.markSupported();
108+
}
62109
}

cf-java-logging-support-servlet/src/test/java/com/sap/hcp/cf/logging/servlet/filter/WrappedInputReaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.sap.hcp.cf.logging.servlet.filter;
22

3+
import static org.hamcrest.MatcherAssert.assertThat;
34
import static org.hamcrest.Matchers.equalTo;
45
import static org.hamcrest.Matchers.is;
5-
import static org.junit.Assert.assertThat;
66

77
import java.io.BufferedReader;
88
import java.io.IOException;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.sap.hcp.cf.logging.servlet.filter;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.is;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.IOException;
9+
import java.nio.charset.StandardCharsets;
10+
11+
import javax.servlet.ServletInputStream;
12+
13+
import org.junit.Test;
14+
15+
public class WrappedInputStreamTest {
16+
17+
private static final String MESSAGE = "ABCDEFGH";
18+
19+
private static WrappedInputStream wrap(String text) {
20+
ByteArrayInputStream in = new ByteArrayInputStream(MESSAGE.getBytes(StandardCharsets.UTF_8));
21+
return new WrappedInputStream(new ServletInputStream() {
22+
23+
@Override
24+
public int read() throws IOException {
25+
return in.read();
26+
}
27+
});
28+
}
29+
30+
@Test
31+
public void unreadInputGivesMinusOne() throws Exception {
32+
WrappedInputStream input = wrap(MESSAGE);
33+
34+
assertThat(input.getContentLength(), is(equalTo(-1)));
35+
}
36+
37+
@Test
38+
public void readSingleCharacter() throws Exception {
39+
WrappedInputStream input = wrap(MESSAGE);
40+
41+
int read = input.read();
42+
43+
assertThat((char) read, is(equalTo('A')));
44+
assertThat(input.getContentLength(), is(equalTo(1)));
45+
}
46+
47+
@Test
48+
public void readCharacterArray() throws Exception {
49+
WrappedInputStream input = wrap(MESSAGE);
50+
byte[] cbuf = new byte[3];
51+
52+
input.read(cbuf);
53+
54+
assertThat(new String(cbuf), is(equalTo("ABC")));
55+
assertThat(input.getContentLength(), is(equalTo(3)));
56+
}
57+
58+
@Test
59+
public void readCharacterArrayWithOffset() throws Exception {
60+
WrappedInputStream input = wrap(MESSAGE);
61+
62+
byte[] cbuf = new byte[5];
63+
input.read(cbuf, 1, 4);
64+
65+
byte[] expected = new byte[5];
66+
System.arraycopy(MESSAGE.getBytes(StandardCharsets.UTF_8), 0, expected, 1, 4);
67+
68+
assertThat(cbuf, is(equalTo(expected)));
69+
assertThat(input.getContentLength(), is(equalTo(4)));
70+
}
71+
72+
@Test
73+
public void skipCharacters() throws Exception {
74+
WrappedInputStream input = wrap(MESSAGE);
75+
76+
long skipped = input.skip(3);
77+
78+
assertThat(input.getContentLength(), is(equalTo((int) skipped)));
79+
}
80+
81+
}

0 commit comments

Comments
 (0)