Skip to content

Commit

Permalink
Improve InputStream and OutputStream copying (#1753)
Browse files Browse the repository at this point in the history
* refactor: Improve InputStream and OutputStream copying

* Refactor: Reorder 'try' blocks and fix unit tests
  • Loading branch information
achhibi authored Jan 31, 2024
1 parent 82c2656 commit 10dac18
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ static void copy(final InputStream from, final OutputStream to) throws IOExcepti
Objects.requireNonNull(from);
Objects.requireNonNull(to);
final byte[] buf = new byte[4096];
while (true) {
final int r = from.read(buf);
if (r == -1) {
break;
}
to.write(buf, 0, r);
int bytesRead;

while ((bytesRead = from.read(buf)) != -1) {
to.write(buf, 0, bytesRead);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ static void copy(final InputStream from, final OutputStream to) throws IOExcepti
Objects.requireNonNull(from);
Objects.requireNonNull(to);
final byte[] buf = new byte[4096];
while (true) {
final int r = from.read(buf);
if (r == -1) {
break;
}
to.write(buf, 0, r);
int bytesRead;

while ((bytesRead = from.read(buf)) != -1) {
to.write(buf, 0, bytesRead);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@Slf4j
public class JacksonJsonFieldBodyFilter implements BodyFilter {

private final static StringReplaceJsonCompactor fallbackCompactor = new StringReplaceJsonCompactor();
private static final StringReplaceJsonCompactor fallbackCompactor = new StringReplaceJsonCompactor();

private final String replacement;
private final Set<String> fields;
Expand All @@ -46,18 +46,13 @@ public String filter(@Nullable final String contentType, final String body) {
}

public String filter(final String body) {
try {
final JsonParser parser = factory.createParser(body);
try ( final CharArrayWriter writer = new CharArrayWriter(body.length() * 2) ){ // rough estimate of final size)

final CharArrayWriter writer = new CharArrayWriter(body.length() * 2); // rough estimate of final size
try (final JsonParser parser = factory.createParser(body);
final JsonGenerator generator = factory.createGenerator(writer)){

final JsonGenerator generator = factory.createGenerator(writer);
try {
while (true) {
JsonToken nextToken = parser.nextToken();
if (nextToken == null) {
break;
}
JsonToken nextToken;
while ((nextToken = parser.nextToken()) != null) {

generator.copyCurrentEvent(parser);
if (nextToken == JsonToken.FIELD_NAME && fields.contains(parser.getCurrentName())) {
Expand All @@ -68,12 +63,7 @@ public String filter(final String body) {
}
}
}
} finally {
parser.close();

generator.close();
}

return writer.toString();
} catch (final Exception e) {
log.trace("Unable to filter body for fields {}, compacting result. `{}`", fields, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;

final class ByteStreams {
private ByteStreams() {
Expand All @@ -16,13 +17,13 @@ static byte[] toByteArray(final InputStream in) throws IOException {
}

static void copy(final InputStream from, final OutputStream to) throws IOException {
Objects.requireNonNull(from);
Objects.requireNonNull(to);
final byte[] buf = new byte[4096];
while (true) {
final int r = from.read(buf);
if (r == -1) {
break;
}
to.write(buf, 0, r);
int bytesRead;

while ((bytesRead = from.read(buf)) != -1) {
to.write(buf, 0, bytesRead);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ static void copy(final InputStream from, final OutputStream to) throws IOExcepti
Objects.requireNonNull(from);
Objects.requireNonNull(to);
final byte[] buf = new byte[4096];
while (true) {
final int r = from.read(buf);
if (r == -1) {
break;
}
to.write(buf, 0, r);
int bytesRead;

while ((bytesRead = from.read(buf)) != -1) {
to.write(buf, 0, bytesRead);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ public void readByte(final HttpServletRequest request, final HttpServletResponse
final ServletInputStream input = request.getInputStream();
final ServletOutputStream output = response.getOutputStream();

while (true) {
final int read = input.read();
if (read == -1) {
break;
}
int read;
while ((read = input.read()) != -1) {
output.write(read);
}
}
Expand All @@ -76,11 +73,7 @@ public void readBytes(final HttpServletRequest request, final HttpServletRespons

final byte[] buffer = new byte[1];

while (true) {
final int read = input.read(buffer);
if (read == -1) {
break;
}
while (input.read(buffer) != -1) {
output.write(buffer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;

final class ByteStreams {

Expand All @@ -18,13 +19,14 @@ static byte[] toByteArray(final InputStream in) throws IOException {
}

static void copy(final InputStream from, final OutputStream to) throws IOException {
Objects.requireNonNull(from);
Objects.requireNonNull(to);

final byte[] buf = new byte[4096];
while (true) {
final int r = from.read(buf);
if (r == -1) {
break;
}
to.write(buf, 0, r);
int bytesRead;

while ((bytesRead = from.read(buf)) != -1) {
to.write(buf, 0, bytesRead);
}
}

Expand Down

0 comments on commit 10dac18

Please sign in to comment.