Skip to content

Commit

Permalink
Eliminate unnecessary flushes
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <jan.supol@oracle.com>
  • Loading branch information
jansupol committed Oct 28, 2024
1 parent 6c8f770 commit 6095590
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2024 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 @@ -130,6 +130,8 @@ private void clientCloseTest(int readTimeout) throws IOException {
inputStream.close();
// trigger sending another 'A' to the stream; it should fail
// (indicating that the streaming has been terminated on the server)
// But only the second flush causes the Exception
assertEquals("OK", sendTarget.request().get().readEntity(String.class));
assertEquals("NOK", sendTarget.request().get().readEntity(String.class));
assertEquals(0, counter.get());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024 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 @@ -129,6 +129,8 @@ private void clientCloseTest(int readTimeout) throws IOException {
inputStream.close();
// trigger sending another 'A' to the stream; it should fail
// (indicating that the streaming has been terminated on the server)
// But only the second flush causes the Exception
assertEquals("OK", sendTarget.request().get().readEntity(String.class));
assertEquals("NOK", sendTarget.request().get().readEntity(String.class));
assertEquals(0, counter.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public void setStreamProvider(OutboundMessageContext.StreamProvider streamProvid
this.streamProvider = streamProvider;
}

/* package */ boolean hasStreamProvider() {
return streamProvider != null;
}

/**
* Enable buffering of the serialized entity.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,15 @@ public boolean isCommitted() {
public void close() {
if (hasEntity()) {
try {
if (CommittingOutputStream.class.isInstance(getEntityStream())) {
// This invokes the ContainerResponseWriter#writeResponseStatusAndHeaders
// which allows to set the proper entityStream
CommittingOutputStream cos = (CommittingOutputStream) getEntityStream();
if (cos.hasStreamProvider()) {
((CommittingOutputStream) getEntityStream()).commit();
}
}

final OutputStream es = getEntityStream();
if (!FlushedCloseable.class.isInstance(es)) {
es.flush();
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, 2024 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 @@ -65,7 +65,7 @@ public final class ReaderWriter {
public static final int BUFFER_SIZE = getBufferSize();

/**
* Whether {@linkplain BUFFER_SIZE} is to be ignored in favor of JRE's own decision.
* Whether {@linkplain #BUFFER_SIZE} is to be ignored in favor of JRE's own decision.
*/
public static final boolean AUTOSIZE_BUFFER = getAutosizeBuffer();

Expand Down Expand Up @@ -269,9 +269,7 @@ private static byte[] readAllBytes(InputStream inputStream) throws IOException {
* @throws IOException in case of a write failure.
*/
public static void writeToAsString(String s, OutputStream out, MediaType type) throws IOException {
Writer osw = new OutputStreamWriter(out, getCharset(type));
osw.write(s);
osw.flush();
out.write(s.getBytes(getCharset(type)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024 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 @@ -60,6 +60,8 @@ public void clientCloseCloseResponseFirstTest() throws IOException {
inputStream.close();
// trigger sending another 'A' to the stream; it should fail
// (indicating that the streaming has been terminated on the server)
// But only the second flush causes the Exception
assertEquals("OK", sendTarget.request().get().readEntity(String.class));
assertEquals("NOK", sendTarget.request().get().readEntity(String.class));
}

Expand Down

0 comments on commit 6095590

Please sign in to comment.