You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/05/09 20:20:28 UTC
svn commit: r1101133 - in /commons/sandbox/runtime/trunk/src/main:
java/org/apache/commons/runtime/
java/org/apache/commons/runtime/platform/unix/ native/ native/include/acr/
native/os/unix/ native/os/win32/ native/shared/
test/org/apache/commons/runtime/
Author: mturk
Date: Mon May 9 18:20:27 2011
New Revision: 1101133
URL: http://svn.apache.org/viewvc?rev=1101133&view=rev
Log:
Replace output with input stream for exec
Added:
commons/sandbox/runtime/trunk/src/main/native/include/acr/bais.h (with props)
commons/sandbox/runtime/trunk/src/main/native/shared/bais.c (with props)
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ConstPointer.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Exec.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Properties.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixExec.java
commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
commons/sandbox/runtime/trunk/src/main/native/os/unix/exec.c
commons/sandbox/runtime/trunk/src/main/native/os/win32/exec.c
commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestExec.java
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ConstPointer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ConstPointer.java?rev=1101133&r1=1101132&r2=1101133&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ConstPointer.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ConstPointer.java Mon May 9 18:20:27 2011
@@ -25,6 +25,9 @@ package org.apache.commons.runtime;
final class ConstPointer extends Pointer
{
+ /**
+ * Creates a new immutable pointer.
+ */
public ConstPointer()
{
POINTER = 0L;
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Exec.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Exec.java?rev=1101133&r1=1101132&r2=1101133&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Exec.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Exec.java Mon May 9 18:20:27 2011
@@ -15,14 +15,17 @@
*/
package org.apache.commons.runtime;
-import java.io.ByteArrayOutputStream;
-import java.io.OutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
import java.io.File;
import java.util.List;
/**
- * Exec class.
+ * Execute a native process.
+ *
* <p>
+ * Exec allows to exectute other processes from JVM and collect its output
+ * without using multiple threads.
* </p>
*
* @since Runtime 1.0
@@ -30,6 +33,9 @@ import java.util.List;
public abstract class Exec
{
+ /**
+ * Creates a new Exec object.
+ */
protected Exec()
{
// No Instance
@@ -41,14 +47,23 @@ public abstract class Exec
impl = ExecImpl.getInstance();
}
+ /**
+ * Internal byte array containing the data that will
+ * be passed to a process standard input stream.
+ * See {@link #setInput(byte[]) setInput} method for more details.
+ */
protected byte[] inp = null;
- protected ByteArrayOutputStream err = null;
- protected ByteArrayOutputStream out = null;
+ protected ByteArrayInputStream err = null;
+ protected ByteArrayInputStream out = null;
protected String cwd = null;
protected String[] env = null;
protected int exitval;
+ protected int redirect;
+ /**
+ * Create a new {@code Exec} instance.
+ */
public static Exec newInstance()
throws OperationNotImplementedException,
SystemException
@@ -67,26 +82,32 @@ public abstract class Exec
public abstract int runCommand(String cmd, int timeout);
public abstract int runCommand(String cmd);
+ /**
+ * Return the process exit value.
+ * <p>
+ * Process exit value is valid only after the process has been
+ * executed. If detached process was created the exit value represents
+ * the Process Identifier {@code pid} of the running process.
+ * </p>
+ */
public int exitval()
{
return exitval;
}
+ /**
+ * Set the data that will be passed to a new process standard input stream.
+ * <p>
+ * If this method was not called, the new process will be created with
+ * {@code null} input stream, which depending on the operating system is
+ * usually either {@code /dev/null} or {@code NUL} pipe.
+ * </p>
+ */
public void setInput(byte[] buf)
{
inp = buf;
}
- public void setErrorStream(ByteArrayOutputStream stream)
- {
- err = stream;
- }
-
- public void setOutputStream(ByteArrayOutputStream stream)
- {
- out = stream;
- }
-
public void setWorkingDirectory(File directory)
{
cwd = directory.getPath();
@@ -97,50 +118,80 @@ public abstract class Exec
env = envp.toArray(new String[0]);
}
- public OutputStream getOutputStream()
+ public InputStream getOutputStream()
{
+ if (out == null)
+ out = new ByteArrayInputStream(new byte[0]);
return out;
}
- public OutputStream getErrorStream()
+ public InputStream getErrorStream()
{
+ if (err == null)
+ err = new ByteArrayInputStream(new byte[0]);
return err;
}
+ /**
+ * Indicates whether the standard error should be redirected.
+ * If not redirected, the {@link Exec#getOutputStream()} will always
+ * return {@code null} and standard output is written to
+ * {@code /dev/null}.
+ *
+ * @return {@code true} if the standard output is redirected; {@code false}
+ * otherwise.
+ */
public boolean redirectOutputStream()
{
- return out != null;
+ return (redirect & 1) != 0;
}
+ /**
+ * Indicates whether the standard error should be redirected.
+ *
+ * If not redirected, the {@link Exec#getErrorStream()} will always
+ * return {@code null} and standard error is written to
+ * {@link Exec#getOutputStream()}.
+ *
+ * @return {@code true} if the standard error is redirected; {@code false}
+ * otherwise.
+ */
public boolean redirectErrorStream()
{
- if (err == null || err == out)
- return false;
- else
- return true;
+ return (redirect & 2) != 0;
}
+ /**
+ * Changes the state of whether or not standard output is redirected.
+ *
+ * @param redirect
+ * {@code true} to redirect standard output, {@code false}
+ * otherwise.
+ * @return this exec instance.
+ */
public Exec redirectOutputStream(boolean redirect)
{
- if (redirect) {
- if (out == null)
- out = new ByteArrayOutputStream();
- }
- else {
- out = null;
- }
+ if (redirect)
+ this.redirect |= 1;
+ else
+ this.redirect &= 2;
return this;
}
+ /**
+ * Changes the state of whether or not standard error is redirected.
+ *
+ * @param redirect
+ * {@code true} to redirect standard error, {@code false}
+ * otherwise.
+ * @return this exec instance.
+ */
public Exec redirectErrorStream(boolean redirect)
{
- if (redirect) {
- if (err == null || err == out)
- err = new ByteArrayOutputStream();
- }
- else {
- err = null;
- }
+ if (redirect)
+ this.redirect |= 2;
+ else
+ this.redirect &= 1;
return this;
}
}
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java?rev=1101133&r1=1101132&r2=1101133&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java Mon May 9 18:20:27 2011
@@ -27,7 +27,17 @@ import org.apache.commons.runtime.util.U
public abstract class Pointer implements Comparable<Pointer>
{
+ /**
+ * Native representation of the allocated pointer.
+ * <p>
+ * This value is casted {@code void *} native C/C++ pointer. Long value
+ * is used so we can safely operate on both 32 and 64 bit systems.
+ * </p>
+ */
protected long POINTER;
+ /**
+ * Size of the allocated {@code Pointer} in bytes.
+ */
protected long PLENGTH;
/**
@@ -134,6 +144,8 @@ public abstract class Pointer implements
* @param other the {@code Pointer} to be Compared
* @return a negative integer, zero or positive integer as this object
* is less then, equal, or greater then the specified object.
+ * @throws ClassCastException if the attempt is made to compare
+ * null object.
*/
@Override
public final int compareTo(Pointer other)
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Properties.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Properties.java?rev=1101133&r1=1101132&r2=1101133&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Properties.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Properties.java Mon May 9 18:20:27 2011
@@ -20,9 +20,10 @@ import java.util.ArrayList;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
-/** The Properties class is used to obtain the various
- * Apache Commons Runtime settings. Their default values are
- * specified inside {@code Default.properties} file.
+/**
+ * The Properties class is used to obtain the various
+ * Apache Commons Runtime settings.
+ * Their default values are specified inside {@code Default.properties} file.
* @since Runtime 1.0
*
*/
@@ -54,6 +55,11 @@ public final class Properties
/**
* Get resource property value.
+ * <p>
+ * Properties are first read from root {@code Default.properties} file.
+ * I the {@code key} was not found then a platform {@code Default.properties}
+ * file is tried. If not found the system properties are checked.
+ * </p>
* @param key Resource name to get.
* @param def Default value in case {@code key} was not found.
*/
@@ -131,7 +137,7 @@ public final class Properties
private static String getS(String key)
{
- return get(key, "(error)");
+ return get(key, "(null)");
}
private static long getL(String key, long def)
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixExec.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixExec.java?rev=1101133&r1=1101132&r2=1101133&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixExec.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/unix/PosixExec.java Mon May 9 18:20:27 2011
@@ -21,6 +21,7 @@ import org.apache.commons.runtime.Errno;
import org.apache.commons.runtime.Status;
import org.apache.commons.runtime.InvalidArgumentException;
import org.apache.commons.runtime.SystemException;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.File;
@@ -47,17 +48,19 @@ final class PosixExec extends Exec
String[] envp,
String cwd,
byte[] inp,
- ByteArrayOutputStream out,
- ByteArrayOutputStream err,
+ ByteArrayInputStream[] out,
+ int redir,
int timeout);
@Override
public int run(List<String> argv, int timeout)
{
+ ByteArrayInputStream[] isa = new ByteArrayInputStream[2];
String[] args = argv.toArray(new String[0]);
- long rv = run0(args[0], args, env, cwd, inp, out, err, timeout);
+ long rv = run0(args[0], args, env, cwd, inp, isa, redirect, timeout);
exitval = (int)(rv & 0xffffffffL);
int res = (int)(rv >>> 32);
-
+ out = isa[0];
+ err = isa[1];
return res;
}
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=1101133&r1=1101132&r2=1101133&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Mon May 9 18:20:27 2011
@@ -105,6 +105,7 @@ LIBSOURCES=\
$(TOPDIR)\port\bsdstring.c \
$(TOPDIR)\port\bsdsys.c \
$(TOPDIR)\shared\array.c \
+ $(TOPDIR)\shared\bais.c \
$(TOPDIR)\shared\bzip2.c \
$(TOPDIR)\shared\callback.c \
$(TOPDIR)\shared\clazz.c \
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1101133&r1=1101132&r2=1101133&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Mon May 9 18:20:27 2011
@@ -98,6 +98,7 @@ LIBSOURCES=\
$(TOPDIR)/port/bsdstring.c \
$(TOPDIR)/port/bsdsys.c \
$(TOPDIR)/shared/array.c \
+ $(TOPDIR)/shared/bais.c \
$(TOPDIR)/shared/bzip2.c \
$(TOPDIR)/shared/callback.c \
$(TOPDIR)/shared/clazz.c \
Added: commons/sandbox/runtime/trunk/src/main/native/include/acr/bais.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/bais.h?rev=1101133&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/bais.h (added)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/bais.h Mon May 9 18:20:27 2011
@@ -0,0 +1,37 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _ACR_BAIS_H
+#define _ACR_BAIS_H
+
+#include "acr/jnitypes.h"
+#include "acr/error.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ACR_CLASS_CTOR(ByteArrayInputStream);
+ACR_CLASS_DTOR(ByteArrayInputStream);
+
+jobject
+AcrNewByteArrayInputStream(JNI_STDENV, const char *data, int len);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _ACR_BAIS_H */
Propchange: commons/sandbox/runtime/trunk/src/main/native/include/acr/bais.h
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/exec.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/exec.c?rev=1101133&r1=1101132&r2=1101133&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/exec.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/exec.c Mon May 9 18:20:27 2011
@@ -16,7 +16,7 @@
#include "acr/error.h"
#include "acr/clazz.h"
-#include "acr/ostream.h"
+#include "acr/bais.h"
#include "acr/iodefs.h"
#include "acr/port.h"
#include "acr/sbuf.h"
@@ -590,7 +590,7 @@ ACR_JNI_EXPORT(jobject, ExecImpl, init0)
ACR_UNX_EXPORT(jlong, PosixExec, run0)(JNI_STDARGS, jstring executable,
jobjectArray args, jobjectArray envp,
jstring cwd, jbyteArray inp,
- jobject out, jobject err,
+ jobjectArray out, jint redir,
jint timeout)
{
int rv = 0;
@@ -626,35 +626,40 @@ ACR_UNX_EXPORT(jlong, PosixExec, run0)(J
ib = &bb[0];
ib->len = (*env)->GetArrayLength(env, inpcopy);
ib->buf = (*env)->GetPrimitiveArrayCritical(env, inpcopy, 0);
- (*env)->ReleasePrimitiveArrayCritical(env, inpcopy, ib->buf, 0);
}
}
- if (IS_JOBJECT_VALID(out))
+ if (redir & 1)
ob = &bb[1];
- if (IS_JOBJECT_VALID(err) && JOBJECTS_NOTEQUAL(err, out))
+ if (redir & 2)
eb = &bb[2];
/* Call actual execute
*/
rv = _run_exec(J2S(executable), argp, envs, J2S(cwd),
ob, eb, ib, timeout, &rc);
- if (inpcopy != 0)
+ if (inpcopy != 0) {
+ (*env)->ReleasePrimitiveArrayCritical(env, inpcopy, ib->buf, 0);
(*env)->DeleteLocalRef(env, inpcopy);
+ }
if (rv != ACR_PARENT_ERROR) {
- if (ob != 0) {
- /* Write to the output stream */
- int r = AcrWriteToOutputStream(env, out, ob->buf, ob->len);
- if (r != 0) {
- rv = r;
+ if (ob != 0 && ob->len > 0) {
+ jobject oout = AcrNewByteArrayInputStream(env, ob->buf, ob->len);
+ if (oout == 0) {
+ rv = ACR_ENOMEM;
goto cleanup;
}
- }
- if (eb != 0) {
- /* Write to the error stream */
- int r = AcrWriteToOutputStream(env, err, eb->buf, eb->len);
- if (r != 0) {
- rv = r;
+ /* set the output stream */
+ (*env)->SetObjectArrayElement(env, out, 0, oout);
+ (*env)->DeleteLocalRef(env, oout);
+ }
+ if (eb != 0 && eb->len > 0) {
+ jobject oerr = AcrNewByteArrayInputStream(env, eb->buf, eb->len);
+ if (oerr == 0) {
+ rv = ACR_ENOMEM;
goto cleanup;
}
+ /* set the error stream */
+ (*env)->SetObjectArrayElement(env, out, 1, oerr);
+ (*env)->DeleteLocalRef(env, oerr);
}
}
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/exec.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/exec.c?rev=1101133&r1=1101132&r2=1101133&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/exec.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/exec.c Mon May 9 18:20:27 2011
@@ -16,7 +16,7 @@
#include "acr/error.h"
#include "acr/clazz.h"
-#include "acr/ostream.h"
+#include "acr/bais.h"
#include "acr/iodefs.h"
#include "acr/port.h"
#include "acr/sbuf.h"
@@ -535,7 +535,7 @@ ACR_JNI_EXPORT(jobject, ExecImpl, init0)
ACR_WIN_EXPORT(jlong, WindowsExec, run0)(JNI_STDARGS, jstring executable,
jstring args, jcharArray envb,
jstring cwd, jbyteArray inp,
- jobject out, jobject err,
+ jobjectArray out, jint redir,
jint timeout)
{
int rv = 0;
@@ -556,35 +556,40 @@ ACR_WIN_EXPORT(jlong, WindowsExec, run0)
ib = &bb[0];
ib->len = (*env)->GetArrayLength(env, inpcopy);
ib->buf = (*env)->GetPrimitiveArrayCritical(env, inpcopy, 0);
- (*env)->ReleasePrimitiveArrayCritical(env, inpcopy, ib->buf, 0);
}
}
- if (IS_JOBJECT_VALID(out))
+ if (redir & 1)
ob = &bb[1];
- if (IS_JOBJECT_VALID(err) && JOBJECTS_NOTEQUAL(err, out))
+ if (redir & 2)
eb = &bb[2];
/* Call actual execute
*/
rv = _run_exec(J2S(executable), J2S(args), J2S(envb), J2S(cwd),
ob, eb, ib, timeout, &rc);
- if (inpcopy != 0)
+ if (inpcopy != 0) {
+ (*env)->ReleasePrimitiveArrayCritical(env, inpcopy, ib->buf, 0);
(*env)->DeleteLocalRef(env, inpcopy);
+ }
if (rv != ACR_PARENT_ERROR) {
- if (ob != 0) {
- /* Write to the output stream */
- int r = AcrWriteToOutputStream(env, out, ob->buf, ob->len);
- if (r != 0) {
- rv = r;
+ if (ob != 0 && ob->len > 0) {
+ jobject oout = AcrNewByteArrayInputStream(env, ob->buf, ob->len);
+ if (oout == 0) {
+ rv = ACR_ENOMEM;
goto cleanup;
}
+ /* set the output stream */
+ (*env)->SetObjectArrayElement(env, out, 0, oout);
+ (*env)->DeleteLocalRef(env, oout);
}
- if (eb != 0) {
- /* Write to the error stream */
- int r = AcrWriteToOutputStream(env, err, eb->buf, eb->len);
- if (r != 0) {
- rv = r;
+ if (eb != 0 && eb->len > 0) {
+ jobject oerr = AcrNewByteArrayInputStream(env, eb->buf, eb->len);
+ if (oerr == 0) {
+ rv = ACR_ENOMEM;
goto cleanup;
}
+ /* set the error stream */
+ (*env)->SetObjectArrayElement(env, out, 1, oerr);
+ (*env)->DeleteLocalRef(env, oerr);
}
}
Added: commons/sandbox/runtime/trunk/src/main/native/shared/bais.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/bais.c?rev=1101133&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/bais.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/bais.c Mon May 9 18:20:27 2011
@@ -0,0 +1,66 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/clazz.h"
+#include "acr/bais.h"
+
+J_DECLARE_CLAZZ = {
+ INVALID_FIELD_BASE,
+ 0,
+ 0,
+ 0,
+ "java/io/ByteArrayInputStream"
+};
+
+J_DECLARE_M_ID(0000) = {
+ 0,
+ "<init>",
+ "([B)V"
+};
+
+ACR_CLASS_CTOR(ByteArrayInputStream)
+{
+
+ if (AcrLoadClass(env, &_clazzn, 0) == JNI_FALSE)
+ return JNI_FALSE;
+ J_LOAD_METHOD(0000);
+
+ _clazzn.u = 1;
+ return JNI_TRUE;
+}
+
+ACR_CLASS_DTOR(ByteArrayInputStream)
+{
+ AcrUnloadClass(env, &_clazzn);
+}
+
+jobject
+AcrNewByteArrayInputStream(JNI_STDENV, const char *data, int len)
+{
+ jobject bais = 0;
+
+ if (data == 0 || len < 1)
+ return 0;
+ if (CLAZZ_LOADED) {
+ jbyteArray ba = (*env)->NewByteArray(env, len);
+ if (ba == 0)
+ return 0;
+ (*env)->SetByteArrayRegion(env, ba, 0, len, (jbyte *)data);
+ bais = (*env)->NewObject(env, _clazzn.i, J4MID(0000), ba);
+ (*env)->DeleteLocalRef(env, ba);
+ }
+ return bais;
+}
Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/bais.c
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c?rev=1101133&r1=1101132&r2=1101133&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Mon May 9 18:20:27 2011
@@ -18,6 +18,7 @@
#include "acr/misc.h"
/* Runtime class includes */
#include "acr/ostream.h"
+#include "acr/bais.h"
#include "acr/object.h"
#include "acr/unsafe.h"
#include "acr/string.h"
@@ -214,6 +215,7 @@ AcrInitCoreClasses(JNI_STDENV)
ACR_CLASS_LOAD(System);
ACR_CLASS_LOAD(Observer);
ACR_CLASS_LOAD(OutputStream);
+ ACR_CLASS_LOAD(ByteArrayInputStream);
return JNI_TRUE;
}
@@ -249,6 +251,7 @@ AcrUnloadRuntimeClasses(JNI_STDENV)
ACR_CLASS_UNLOAD(Callback);
ACR_CLASS_UNLOAD(Unsafe);
ACR_CLASS_UNLOAD(OutputStream);
+ ACR_CLASS_UNLOAD(ByteArrayInputStream);
ACR_CLASS_UNLOAD(Observer);
ACR_CLASS_UNLOAD(System);
ACR_CLASS_UNLOAD(String);
Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestExec.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestExec.java?rev=1101133&r1=1101132&r2=1101133&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestExec.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestExec.java Mon May 9 18:20:27 2011
@@ -18,9 +18,8 @@ package org.apache.commons.runtime;
import org.testng.annotations.*;
import org.testng.Assert;
-import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
import java.io.IOException;
-import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
@@ -41,9 +40,11 @@ public class TestExec extends Assert
e.redirectErrorStream(true);
int r = e.run(args);
assertEquals(r, Status.CHILD_DONE);
- ByteArrayOutputStream os = (ByteArrayOutputStream)e.getOutputStream();
- System.out.println("Exec returned:");
- System.out.println(new String(os.toByteArray()));
+ InputStream stdout = e.getOutputStream();
+ assertNotNull(stdout);
+ assertTrue(stdout.available() > 0);
+ InputStream stderr = e.getErrorStream();
+ assertEquals(stderr.available(), 0);
}
@Test(groups = { "posix" })
@@ -58,8 +59,11 @@ public class TestExec extends Assert
e.redirectOutputStream(true);
int r = e.run(args);
assertEquals(r, Status.CHILD_DONE);
- ByteArrayOutputStream os = (ByteArrayOutputStream)e.getOutputStream();
- String rs = new String(os.toByteArray());
+ InputStream stdout = e.getOutputStream();
+ assertNotNull(stdout);
+ byte[] rv = new byte[32];
+ int readn = stdout.read(rv);
+ String rs = new String(rv, 0, readn);
assertEquals(rs, "hello world");
}
@@ -76,9 +80,9 @@ public class TestExec extends Assert
e.redirectErrorStream(true);
int r = e.run(args);
assertEquals(r, Status.CHILD_DONE);
- ByteArrayOutputStream os = (ByteArrayOutputStream)e.getOutputStream();
- System.out.println("Exec returned:");
- System.out.println(new String(os.toByteArray()));
+ InputStream stdout = e.getOutputStream();
+ assertNotNull(stdout);
+ assertTrue(stdout.available() > 0);
}
@Test(groups = { "core" })
@@ -106,9 +110,9 @@ public class TestExec extends Assert
int r = e.run(args);
assertEquals(r, Status.CHILD_DONE);
assertEquals(e.exitval(), 0);
- ByteArrayOutputStream os = (ByteArrayOutputStream)e.getOutputStream();
- String rv = new String(os.toByteArray());
- assertTrue(rv.startsWith("java version"));
+ InputStream stdout = e.getOutputStream();
+ assertNotNull(stdout);
+ assertTrue(stdout.available() > 0);
}
@Test(groups = { "core" })
@@ -121,9 +125,9 @@ public class TestExec extends Assert
int r = e.runCommand("echo foo");
assertEquals(r, Status.CHILD_DONE);
assertEquals(e.exitval(), 0);
- ByteArrayOutputStream os = (ByteArrayOutputStream)e.getOutputStream();
- String rv = new String(os.toByteArray());
- assertTrue(rv.startsWith("foo"));
+ InputStream stdout = e.getOutputStream();
+ assertNotNull(stdout);
+ assertTrue(stdout.available() >= 3);
}
}