Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions src/main/java/org/scijava/script/ScriptInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -73,7 +74,7 @@ public class ScriptInfo extends AbstractModuleInfo implements Contextual {
private static final int PARAM_CHAR_MAX = 640 * 1024; // should be enough ;-)

private final String path;
private final BufferedReader reader;
private final String script;

@Parameter
private Context context;
Expand Down Expand Up @@ -128,8 +129,17 @@ public ScriptInfo(final Context context, final String path,
{
setContext(context);
this.path = path;
this.reader =
reader == null ? null : new BufferedReader(reader, PARAM_CHAR_MAX);

String script = null;
if (reader != null) {
try {
script = getReaderContentsAsString(reader);
}
catch (final IOException exc) {
log.error("Error reading script: " + path, exc);
}
}
this.script = script;
}

// -- ScriptInfo methods --
Expand All @@ -148,14 +158,17 @@ public String getPath() {
}

/**
* Gets the reader which delivers the script's content.
* Gets a reader which delivers the script's content.
* <p>
* This might be null, in which case the content is stored in a file on disk
* given by {@link #getPath()}.
* </p>
*/
public BufferedReader getReader() {
return reader;
if (script == null) {
return null;
}
return new BufferedReader(new StringReader(script), PARAM_CHAR_MAX);
}

/**
Expand Down Expand Up @@ -214,12 +227,11 @@ public void parseParameters() {

try {
final BufferedReader in;
if (reader == null) {
if (script == null) {
in = new BufferedReader(new FileReader(getPath()));
}
else {
in = reader;
in.mark(PARAM_CHAR_MAX);
in = getReader();
}
while (true) {
final String line = in.readLine();
Expand All @@ -234,8 +246,7 @@ public void parseParameters() {
}
else if (line.matches(".*\\w.*")) break;
}
if (reader == null) in.close();
else in.reset();
in.close();

if (!returnValueDeclared) addReturnValue();
}
Expand Down Expand Up @@ -478,4 +489,27 @@ else if ("value".equalsIgnoreCase(key)) {
}
}

/**
* Read entire contents of a Reader and return as String.
*
* @param reader {@link Reader} whose contents should be returned as String.
* Expected to never be <code>null</code>.
* @return contents of reader as String.
* @throws IOException If an I/O error occurs
* @throws NullPointerException If reader is <code>null</code>
*/
private static String getReaderContentsAsString(final Reader reader)
throws IOException, NullPointerException
{
final char[] buffer = new char[8192];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This number is pretty arbitrary. Any ideas on that? Use the probably also arbitrary (but alot bigger) PARAM_CHAR_MAX instead? :)

This code is based off of the code I wrote in scripting-java a while ago.

final StringBuilder builder = new StringBuilder();

int read;
while ((read = reader.read(buffer)) != -1) {
builder.append(buffer, 0, read);
}

return builder.toString();
}

}
21 changes: 21 additions & 0 deletions src/test/java/org/scijava/script/ScriptInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
Expand Down Expand Up @@ -124,6 +125,26 @@ public void testVersion() throws IOException {
FileUtils.deleteRecursively(tmpDir);
}

/**
* Ensures the ScriptInfos Reader can be reused for multiple executions of the
* script.
*/
@Test
public void testReaderSanity() throws Exception {
final String script = "" + //
"% @LogService log\n" + //
"% @OUTPUT Integer output";

ScriptInfo info = new ScriptInfo(context, "hello.bsizes", new StringReader(
script));
BufferedReader reader1 = info.getReader();
BufferedReader reader2 = info.getReader();

assertEquals("Readers are not independent.", reader1.read(), reader2
.read());

}

@Plugin(type = ScriptLanguage.class)
public static class BindingSizes extends AbstractScriptLanguage {

Expand Down