From 3363a1ca285dfe7fa6e193bcd3a87a42aa8daa2f Mon Sep 17 00:00:00 2001 From: "tibor.digana" Date: Wed, 13 Apr 2022 01:08:56 +0200 Subject: [PATCH 1/2] Commits after SUREFIRE-2058 regarding AbstractStreamDecoder --- .../api/stream/AbstractStreamDecoder.java | 33 +++++++------------ 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/api/stream/AbstractStreamDecoder.java b/surefire-api/src/main/java/org/apache/maven/surefire/api/stream/AbstractStreamDecoder.java index facf30bcb5..6ddbb4514f 100644 --- a/surefire-api/src/main/java/org/apache/maven/surefire/api/stream/AbstractStreamDecoder.java +++ b/surefire-api/src/main/java/org/apache/maven/surefire/api/stream/AbstractStreamDecoder.java @@ -305,37 +305,26 @@ private String readString( @Nonnull final Memento memento, @Nonnegative final in memento.getDecoder().reset(); final CharBuffer output = memento.getCharBuffer(); ( (Buffer) output ).clear(); - final ByteBuffer input = memento.getByteBuffer(); final List strings = new ArrayList<>(); int countDecodedBytes = 0; for ( boolean endOfInput = false; !endOfInput; ) { - final int bytesToRead = totalBytes - countDecodedBytes; + final int bytesToRead = totalBytes - countDecodedBytes; // our wish to read bytes as much as possible read( memento, bytesToRead ); - int bytesToDecode = min( input.remaining(), bytesToRead ); + final ByteBuffer input = memento.getByteBuffer(); + int bytesToDecode = min( input.remaining(), bytesToRead ); // our guarantee of available bytes in buffer final boolean isLastChunk = bytesToDecode == bytesToRead; endOfInput = countDecodedBytes + bytesToDecode >= totalBytes; - do - { - boolean endOfChunk = output.remaining() >= bytesToRead; - boolean endOfOutput = isLastChunk && endOfChunk; - int readInputBytes = decodeString( memento.getDecoder(), input, output, bytesToDecode, endOfOutput, - memento.getLine().getPositionByteBuffer() ); - bytesToDecode -= readInputBytes; - countDecodedBytes += readInputBytes; - } - while ( isLastChunk && bytesToDecode > 0 && output.hasRemaining() ); - - if ( isLastChunk || !output.hasRemaining() ) - { - strings.add( ( (Buffer) output ).flip().toString() ); - ( (Buffer) output ).clear(); - } + boolean endOfChunk = output.remaining() >= bytesToRead; + boolean endOfOutput = isLastChunk && endOfChunk; + int readInputBytes = decodeString( memento.getDecoder(), input, output, bytesToDecode, endOfOutput, + memento.getLine().getPositionByteBuffer() ); + countDecodedBytes += readInputBytes; + strings.add( ( (Buffer) output ).flip().toString() ); + ( (Buffer) output ).clear(); + memento.getLine().setPositionByteBuffer( 0 ); } - memento.getDecoder().reset(); - ( (Buffer) output ).clear(); - return toString( strings ); } From 72b1841f5cecc5ff393ce75f6d42af423ddcb4ed Mon Sep 17 00:00:00 2001 From: "tibor.digana" Date: Wed, 13 Apr 2022 02:28:26 +0200 Subject: [PATCH 2/2] Refactored decode(Memento) and embedded Memento in the decoder instance --- .../surefire/extensions/EventConsumerThread.java | 4 +--- .../apache/maven/surefire/stream/EventDecoder.java | 11 ++++++++++- .../surefire/api/stream/AbstractStreamDecoder.java | 10 +++++++++- .../api/stream/MalformedChannelException.java | 2 +- .../api/stream/AbstractStreamDecoderTest.java | 2 +- .../surefire/booter/spi/CommandChannelDecoder.java | 11 +---------- .../maven/surefire/booter/stream/CommandDecoder.java | 10 +++++++++- 7 files changed, 32 insertions(+), 18 deletions(-) diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/EventConsumerThread.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/EventConsumerThread.java index 79d5c85ab1..2e5b5d3f20 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/EventConsumerThread.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/EventConsumerThread.java @@ -21,7 +21,6 @@ import org.apache.maven.surefire.api.event.Event; import org.apache.maven.surefire.api.fork.ForkNodeArguments; -import org.apache.maven.surefire.api.stream.AbstractStreamDecoder.Memento; import org.apache.maven.surefire.extensions.CloseableDaemonThread; import org.apache.maven.surefire.extensions.EventHandler; import org.apache.maven.surefire.extensions.util.CountdownCloseable; @@ -67,10 +66,9 @@ public void run() CountdownCloseable c = countdownCloseable; EventDecoder eventDecoder = decoder ) { - Memento memento = eventDecoder.new Memento(); do { - Event event = eventDecoder.decode( memento ); + Event event = eventDecoder.decode(); if ( event != null && !disabled ) { eventHandler.handleEvent( event ); diff --git a/maven-surefire-common/src/main/java/org/apache/maven/surefire/stream/EventDecoder.java b/maven-surefire-common/src/main/java/org/apache/maven/surefire/stream/EventDecoder.java index ea11889846..fa61d491ef 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/surefire/stream/EventDecoder.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/surefire/stream/EventDecoder.java @@ -140,6 +140,8 @@ public class EventDecoder extends AbstractStreamDecoder