Skip to content

Commit

Permalink
Keeping ReaderWriter.writeTo, can fallback to original custom code by…
Browse files Browse the repository at this point in the history
… explicitly setting org.glassfish.jersey.message.MessageProperties#IO_BUFFER_SIZE
  • Loading branch information
mkarg committed May 21, 2023
1 parent add7672 commit c118b65
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,8 @@ private static InputStream getInputStream(final CloseableHttpResponse response,
if (i.markSupported()) {
inputStream = i;
} else {
inputStream = new BufferedInputStream(i, ReaderWriter.BUFFER_SIZE);
inputStream = ReaderWriter.BUFFER_SIZE == -1 ? new BufferedInputStream(i)
: new BufferedInputStream(i, ReaderWriter.BUFFER_SIZE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@ private static InputStream getInputStream(final CloseableHttpResponse response,
if (i.markSupported()) {
inputStream = i;
} else {
inputStream = new BufferedInputStream(i, ReaderWriter.BUFFER_SIZE);
inputStream = ReaderWriter.BUFFER_SIZE == -1 ? new BufferedInputStream(i)
: new BufferedInputStream(i, ReaderWriter.BUFFER_SIZE);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -67,18 +67,12 @@ public final class MessageProperties {
* on byte and character streams. The property value is expected to be a positive
* integer otherwise it will be ignored.
* <p />
* The default value is <code>{@value #IO_DEFAULT_BUFFER_SIZE}</code>.
* The default value is decided by the JRE.
* <p />
* The name of the configuration property is <code>{@value}</code>.
*/
public static final String IO_BUFFER_SIZE = "jersey.config.io.bufferSize";

/**
* The default buffer size ({@value}) for I/O operations on byte and character
* streams.
*/
public static final int IO_DEFAULT_BUFFER_SIZE = 8192;

/**
* If set to {@code true}, {@code DeflateEncoder deflate encoding interceptor} will use non-standard version
* of the deflate content encoding, skipping the zlib wrapper. Unfortunately, deflate encoding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public abstract class AbstractMessageReaderWriterProvider<T> implements MessageB
/**
* The UTF-8 Charset.
*
* @deprecated use {@code StandardCharsets.UTF8} instead.
* @deprecated use {@code StandardCharsets.UTF_8} instead.
*/
@Deprecated(forRemoval = true)
public static final Charset UTF8 = StandardCharsets.UTF_8;
Expand All @@ -56,11 +56,11 @@ public abstract class AbstractMessageReaderWriterProvider<T> implements MessageB
* @param out the output stream to write to.
* @throws IOException if there is an error reading or writing bytes.
*
* @deprecated use {@code in.transferTo(out)} instead.
* @deprecated use {@code ReaderWriter.writeTo(in, out)} instead.
*/
@Deprecated
public static void writeTo(InputStream in, OutputStream out) throws IOException {
in.transferTo(out);
ReaderWriter.writeTo(in, out);
}

/**
Expand All @@ -70,11 +70,11 @@ public static void writeTo(InputStream in, OutputStream out) throws IOException
* @param out the writer to write to.
* @throws IOException if there is an error reading or writing characters.
*
* @deprecated use {@code in.transferTo(out)} instead.
* @deprecated use {@code ReaderWriter.writeTo(in, out)} instead.
*/
@Deprecated
public static void writeTo(Reader in, Writer out) throws IOException {
in.transferTo(out);
ReaderWriter.writeTo(in, out);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -75,7 +75,7 @@ public int getCount() {

public ByteArrayDataSource(final InputStream is, final String type) throws IOException {
final DSByteArrayOutputStream os = new DSByteArrayOutputStream();
is.transferTo(os);
ReaderWriter.writeTo(is, os);
this.data = os.getBuf();
this.len = os.getCount();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -960,7 +960,7 @@ public boolean bufferEntity() throws ProcessingException {
final InputStream entityStream = entityContent.getWrappedStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
entityStream.transferTo(baos);
ReaderWriter.writeTo(entityStream, baos);
} finally {
// Workaround for JRFCAF-1344: the underlying stream close() implementation may be thread-unsafe
// and as such the close() may result in an IOException at the socket input stream level,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void writeTo(
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException {
try (t) {
t.transferTo(entityStream);
writeTo(t, entityStream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* A utility class for reading and writing using byte and character streams.
* <p>
* If a byte or character array is utilized then the size of the array
* is by default the value of {@value org.glassfish.jersey.message.MessageProperties#IO_DEFAULT_BUFFER_SIZE}.
* is by default decided by the JRE.
* This value can be set using the system property
* {@value org.glassfish.jersey.message.MessageProperties#IO_BUFFER_SIZE}.
*
Expand All @@ -59,6 +59,7 @@ public final class ReaderWriter {
public static final Charset UTF8 = StandardCharsets.UTF_8;
/**
* The buffer size for arrays of byte and character.
* A value of {@value -1} means that the JRE will decide the buffer size.
*/
public static final int BUFFER_SIZE = getBufferSize();

Expand All @@ -76,11 +77,11 @@ private static int getBufferSize() {
LOGGER.log(Level.CONFIG,
"Value of " + MessageProperties.IO_BUFFER_SIZE
+ " property is not a valid positive integer [" + value + "]."
+ " Reverting to default [" + MessageProperties.IO_DEFAULT_BUFFER_SIZE + "].",
+ " Reverting to default [at JRE's discretion].",
e);
}
}
return MessageProperties.IO_DEFAULT_BUFFER_SIZE;
return -1;
}

/**
Expand All @@ -89,12 +90,18 @@ private static int getBufferSize() {
* @param in the input stream to read from.
* @param out the output stream to write to.
* @throws IOException if there is an error reading or writing bytes.
*
* @deprecated use {@code in.transferTo(out)} instead
*/
@Deprecated(forRemoval = true)
public static void writeTo(InputStream in, OutputStream out) throws IOException {
in.transferTo(out);
if (BUFFER_SIZE == -1) {
in.transferTo(out);
return;
}

int read;
final byte[] data = new byte[BUFFER_SIZE];
while ((read = in.read(data)) != -1) {
out.write(data, 0, read);
}
}

/**
Expand All @@ -103,12 +110,18 @@ public static void writeTo(InputStream in, OutputStream out) throws IOException
* @param in the reader to read from.
* @param out the writer to write to.
* @throws IOException if there is an error reading or writing characters.
*
* @deprecated use {@code in.transferTo(out)} instead
*/
@Deprecated(forRemoval = true)
public static void writeTo(Reader in, Writer out) throws IOException {
in.transferTo(out);
if (BUFFER_SIZE == -1) {
in.transferTo(out);
return;
}

int read;
final char[] data = new char[BUFFER_SIZE];
while ((read = in.read(data)) != -1) {
out.write(data, 0, read);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void createTempFile() throws IOException {

try (final OutputStream stream = new BufferedOutputStream(Files.newOutputStream(file.toPath()))) {
final ByteArrayInputStream entityStream = new ByteArrayInputStream("Test stream byte input".getBytes());
entityStream.transferTo(stream);
ReaderWriter.writeTo(entityStream, stream);
}
Assertions.assertTrue(file.exists());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public void service(Request request, Response response) throws Exception {
} else {
response.setStatus(HttpStatus.OK_200);
response.setContentType(mediaType);
fileStream.transferTo(response.getOutputStream());
ReaderWriter.writeTo(fileStream, response.getOutputStream());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -153,7 +153,7 @@ protected static void _verify(byte[] in, byte[] out) {
protected static byte[] getEntityAsByteArray(Response r) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
r.readEntity(InputStream.class).transferTo(baos);
ReaderWriter.writeTo(r.readEntity(InputStream.class), baos);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -189,7 +189,7 @@ public void testInterruptedJerseyHttpUrlConnection() throws Exception {
connection.setDoOutput(true);
connection.setChunkedStreamingMode(1024);
OutputStream entityStream = connection.getOutputStream();
new ExceptionThrowingInputStream(BYTES_TO_SEND).transferTo(entityStream);
ReaderWriter.writeTo(new ExceptionThrowingInputStream(BYTES_TO_SEND), entityStream);
Assertions.fail("Expected ProcessingException has not been thrown.");
} catch (IOException expected) {
// so far so good
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ReaderWriterTest {
@Test
public void testToNeverAskToReadZeroBytes() throws IOException {
// Unnamed app server bug test
int size = ((ReaderWriter.BUFFER_SIZE + 1000) / 10) * 10;
int size = (((ReaderWriter.BUFFER_SIZE == -1 ? 8192 : ReaderWriter.BUFFER_SIZE) + 1000) / 10) * 10;
StringBuilder sb = new StringBuilder(size);
String shortMsg = "0123456789";
while (sb.length() < size) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -174,7 +174,7 @@ protected static void _verify(byte[] in, byte[] out) {
protected static byte[] getEntityAsByteArray(Response r) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
r.readEntity(InputStream.class).transferTo(baos);
ReaderWriter.writeTo(r.readEntity(InputStream.class), baos);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit c118b65

Please sign in to comment.