From 8ed1242216e6cd4e4e357a6c3d9cc6adbec585e6 Mon Sep 17 00:00:00 2001 From: Jason Wells Date: Thu, 10 Aug 2023 16:04:21 -0700 Subject: [PATCH] buffer and flush --- .../nuix/innovation/enginewrapper/RubyScriptRunner.java | 9 +++++---- Nx/src/test/java/RubyScriptRunnerTests.java | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Nx/src/main/java/com/nuix/innovation/enginewrapper/RubyScriptRunner.java b/Nx/src/main/java/com/nuix/innovation/enginewrapper/RubyScriptRunner.java index 2d5badd..d75dc62 100644 --- a/Nx/src/main/java/com/nuix/innovation/enginewrapper/RubyScriptRunner.java +++ b/Nx/src/main/java/com/nuix/innovation/enginewrapper/RubyScriptRunner.java @@ -7,9 +7,7 @@ import org.jetbrains.annotations.NotNull; import org.jruby.embed.LocalVariableBehavior; import org.jruby.embed.ScriptingContainer; -import org.jruby.embed.internal.BiVariableMap; -import javax.swing.*; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -25,11 +23,12 @@ public class RubyScriptRunner { private static final Logger log = LogManager.getLogger(RubyScriptRunner.class); /*** - * Writer implementation which specializes in forwarding to to consumer. Used to forward + * Writer implementation which specializes in forwarding to a consumer. Used to forward * standard out and err to consumer from script container. */ static class EventedWriter extends Writer { private Consumer consumer; + private final StringBuilder buffer = new StringBuilder(); public EventedWriter(Consumer consumer) { this.consumer = consumer; @@ -42,13 +41,15 @@ public void write(@NotNull char[] cbuf, int off, int len) throws IOException { char[] subchars = new char[len]; System.arraycopy(cbuf, off, subchars, 0, len); String value = new String(subchars); - consumer.accept(value); + buffer.append(value); } } } @Override public void flush() throws IOException { + consumer.accept(buffer.toString()); + buffer.setLength(0); // Clear for reuse } @Override diff --git a/Nx/src/test/java/RubyScriptRunnerTests.java b/Nx/src/test/java/RubyScriptRunnerTests.java index 5c8802f..1e51a7c 100644 --- a/Nx/src/test/java/RubyScriptRunnerTests.java +++ b/Nx/src/test/java/RubyScriptRunnerTests.java @@ -13,7 +13,7 @@ public class RubyScriptRunnerTests { @Test public void RunScript() throws Exception { // Validate that script runs and output is captured - String script = "puts 'hello'"; + String script = "5.times{puts 'hello'}"; List output = new ArrayList<>(); RubyScriptRunner rubyScriptRunner = new RubyScriptRunner(); rubyScriptRunner.setStandardOutputConsumer(output::add); @@ -21,6 +21,7 @@ public void RunScript() throws Exception { rubyScriptRunner.runScriptAsync(script, "0.0.0.0", Map.of()); rubyScriptRunner.join(); assertTrue(output.size() > 0); + System.out.println(String.join("",output)); } @Test