Skip to content

Commit

Permalink
Added more things.
Browse files Browse the repository at this point in the history
  • Loading branch information
MTrop committed May 11, 2020
1 parent 5bbb844 commit 47ab542
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 29 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Changed in 1.7.0

- `Added` System functions.
- `Added` Added STRSPLIT().
- `Changed` Some function documentation.


Changed in 1.6.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ protected Usage usage()
{
return ScriptFunctionUsage.create()
.instructions(
"Opens a data input stream for reading from a GZIP stream (and registers this resource as an open resource)."
"Wraps a data input stream for reading a GZIP stream (and registers this resource as " +
"an open resource, but only if the underlying stream is registered - it is deregistered before " +
"registering this one)."
)
.parameter("instream",
type(Type.OBJECTREF, "InputStream", "A valid open input stream.")
Expand Down Expand Up @@ -430,13 +432,15 @@ protected Usage usage()
{
return ScriptFunctionUsage.create()
.instructions(
"Opens a data output stream for writing to a GZIP stream (and registers this resource as an open resource)."
"Wraps a data output stream for writing a GZIP stream (and registers this resource as " +
"an open resource, but only if the underlying stream is registered - it is deregistered before " +
"registering this one)."
)
.parameter("instream",
.parameter("outstream",
type(Type.OBJECTREF, "OutputStream", "A valid open output stream.")
)
.returns(
type(Type.OBJECTREF, "DataOutput", "An open data output stream to read from."),
type(Type.OBJECTREF, "DataOutputStream", "An open data output stream to write to."),
type(Type.ERROR, "BadParameter", "If an open output stream was not provided."),
type(Type.ERROR, "IOError", "If a write error occurs, or the provided stream is not open.")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
*/
public enum StreamingIOFunctions implements ScriptFunctionType
{
/** @since 1.7.0, accepts InputStreams. */
SKIP(2)
{
@Override
Expand All @@ -62,7 +63,8 @@ protected Usage usage()
"current cursor position. The file's cursor will be advanced."
)
.parameter("input",
type(Type.OBJECTREF, "DataInput", "A readable data input.")
type(Type.OBJECTREF, "DataInput", "A readable data input."),
type(Type.OBJECTREF, "InputStream", "An input stream.")
)
.parameter("length",
type(Type.NULL, "Use 0."),
Expand All @@ -85,36 +87,59 @@ public boolean execute(ScriptInstance scriptInstance, ScriptValue returnValue)
scriptInstance.popStackValue(temp);
int length = temp.asInt();
scriptInstance.popStackValue(temp);
if (!temp.isObjectRef(DataInput.class))

long totalSkipped = 0;
if (temp.isObjectRef(DataInput.class))
{
returnValue.setError("BadParameter", "First parameter is not a readable data input");
DataInput in = temp.asObjectType(DataInput.class);
try {
// some inputstreams are buffered - skip() will only skip up to the buffer's length if data is buffered.
// this ensures the maximum possible/desired, and breaks out when it truly cannot skip any more.
long buf = 0;
long lastlength = length;
while (length > 0)
{
buf = in.skipBytes(length);
length -= buf;
totalSkipped += buf;
if (lastlength == length)
break;
lastlength = length;
}
returnValue.set(totalSkipped);
} catch (IOException e) {
returnValue.setError("IOError", e.getMessage(), e.getLocalizedMessage());
}
return true;
}

DataInput in = temp.asObjectType(DataInput.class);

long totalSkipped = 0;
try {
// some inputstreams are buffered - skip() will only skip up to the buffer's length if data is buffered.
// this ensures the maximum possible/desired, and breaks out when it truly cannot skip any more.
long buf = 0;
long lastlength = length;
while (length > 0)
{
buf = in.skipBytes(length);
length -= buf;
totalSkipped += buf;
if (lastlength == length)
break;
lastlength = length;
else if (temp.isObjectRef(InputStream.class))
{
InputStream in = temp.asObjectType(InputStream.class);
try {
// some inputstreams are buffered - skip() will only skip up to the buffer's length if data is buffered.
// this ensures the maximum possible/desired, and breaks out when it truly cannot skip any more.
long buf = 0;
long lastlength = length;
while (length > 0)
{
buf = in.skip(length);
length -= buf;
totalSkipped += buf;
if (lastlength == length)
break;
lastlength = length;
}
returnValue.set(totalSkipped);
} catch (IOException e) {
returnValue.setError("IOError", e.getMessage(), e.getLocalizedMessage());
}
} catch (IOException e) {
returnValue.setError("IOError", e.getMessage(), e.getLocalizedMessage());
return true;
}

returnValue.set(totalSkipped);
return true;
else
{
returnValue.setError("BadParameter", "First parameter is not a valid input.");
return true;
}
}
finally
{
Expand Down

0 comments on commit 47ab542

Please sign in to comment.