diff options
author | Morgan Deters <mdeters@cs.nyu.edu> | 2013-03-01 18:31:10 -0500 |
---|---|---|
committer | Morgan Deters <mdeters@cs.nyu.edu> | 2013-03-25 20:35:54 -0400 |
commit | ef189453232a4dff9d3cfebafc6101bf8416b549 (patch) | |
tree | 3940f9d503cfc69431a3ac85b717fd4871241553 /src/bindings | |
parent | 30333c667c982a3ce4c4efcec705724d22f12ec2 (diff) |
java input stream adapters working
Diffstat (limited to 'src/bindings')
-rw-r--r-- | src/bindings/Makefile.am | 2 | ||||
-rw-r--r-- | src/bindings/java_output_stream_adapter.h | 50 | ||||
-rw-r--r-- | src/bindings/java_stream_adapters.h | 113 |
3 files changed, 114 insertions, 51 deletions
diff --git a/src/bindings/Makefile.am b/src/bindings/Makefile.am index 301150b12..dcc4bc858 100644 --- a/src/bindings/Makefile.am +++ b/src/bindings/Makefile.am @@ -163,7 +163,7 @@ CLEANFILES = \ EXTRA_DIST = \ swig.h \ java_iterator_adapter.h \ - java_output_stream_adapter.h + java_stream_adapters.h MOSTLYCLEANFILES = \ .swig_deps \ diff --git a/src/bindings/java_output_stream_adapter.h b/src/bindings/java_output_stream_adapter.h deleted file mode 100644 index 6830e508d..000000000 --- a/src/bindings/java_output_stream_adapter.h +++ /dev/null @@ -1,50 +0,0 @@ -/********************* */ -/*! \file java_output_stream_adapter.h - ** \verbatim - ** Original author: mdeters - ** Major contributors: none - ** Minor contributors (to current version): none - ** This file is part of the CVC4 prototype. - ** Copyright (c) 2009-2012 New York University and The University of Iowa - ** See the file COPYING in the top-level source directory for licensing - ** information.\endverbatim - ** - ** \brief An OutputStream adapter for the Java bindings - ** - ** An OutputStream adapter for the Java bindings. This works with a lot - ** of help from SWIG, and custom typemaps in the ".i" SWIG interface files - ** for CVC4. The basic idea is that, when a CVC4 function with a - ** std::ostream& parameter is called, a Java-side binding is generated - ** taking a java.io.OutputStream. Now, the problem is that std::ostream - ** has no Java equivalent, and java.io.OutputStream has no C++ equivalent, - ** so we use this class (which exists on both sides) as the go-between. - ** The wrapper connecting the Java function (taking an OutputStream) and - ** the C++ function (taking an ostream) creates a JavaOutputStreamAdapter, - ** and call the C++ function with the stringstream inside. After the call, - ** the generated stream material is collected and output to the Java-side - ** OutputStream. - **/ - -// private to the bindings layer -#ifndef SWIGJAVA -# error This should only be included from the Java bindings layer. -#endif /* SWIGJAVA */ - -#ifndef __CVC4__BINDINGS__JAVA_OUTPUT_STREAM_ADAPTER_H -#define __CVC4__BINDINGS__JAVA_OUTPUT_STREAM_ADAPTER_H - -namespace CVC4 { - -class JavaOutputStreamAdapter { - std::stringstream d_ss; - -public: - JavaOutputStreamAdapter() { } - - std::string toString() { return d_ss.str(); } - -};/* class JavaOutputStreamAdapter */ - -}/* CVC4 namespace */ - -#endif /* __CVC4__BINDINGS__JAVA_OUTPUT_STREAM_ADAPTER_H */ diff --git a/src/bindings/java_stream_adapters.h b/src/bindings/java_stream_adapters.h new file mode 100644 index 000000000..daeb06e18 --- /dev/null +++ b/src/bindings/java_stream_adapters.h @@ -0,0 +1,113 @@ +/********************* */ +/*! \file java_stream_adapters.h + ** \verbatim + ** Original author: mdeters + ** Major contributors: none + ** Minor contributors (to current version): none + ** This file is part of the CVC4 prototype. + ** Copyright (c) 2009-2012 New York University and The University of Iowa + ** See the file COPYING in the top-level source directory for licensing + ** information.\endverbatim + ** + ** \brief An OutputStream adapter for the Java bindings + ** + ** An OutputStream adapter for the Java bindings. This works with a lot + ** of help from SWIG, and custom typemaps in the ".i" SWIG interface files + ** for CVC4. The basic idea is that, when a CVC4 function with a + ** std::ostream& parameter is called, a Java-side binding is generated + ** taking a java.io.OutputStream. Now, the problem is that std::ostream + ** has no Java equivalent, and java.io.OutputStream has no C++ equivalent, + ** so we use this class (which exists on both sides) as the go-between. + ** The wrapper connecting the Java function (taking an OutputStream) and + ** the C++ function (taking an ostream) creates a JavaOutputStreamAdapter, + ** and call the C++ function with the stringstream inside. After the call, + ** the generated stream material is collected and output to the Java-side + ** OutputStream. + **/ + +// private to the bindings layer +#ifndef SWIGJAVA +# error This should only be included from the Java bindings layer. +#endif /* SWIGJAVA */ + +#include <sstream> +#include <set> +#include <cassert> +#include <iostream> +#include <string> +#include <jni.h> + +#ifndef __CVC4__BINDINGS__JAVA_STREAM_ADAPTERS_H +#define __CVC4__BINDINGS__JAVA_STREAM_ADAPTERS_H + +namespace CVC4 { + +class JavaOutputStreamAdapter { + std::stringstream d_ss; + +public: + JavaOutputStreamAdapter() { } + + std::string toString() { return d_ss.str(); } + +};/* class JavaOutputStreamAdapter */ + +class JavaInputStreamAdapter : public std::stringstream { + static std::set<JavaInputStreamAdapter*> s_adapters; + jobject inputStream; + + JavaInputStreamAdapter& operator=(const JavaInputStreamAdapter&); + JavaInputStreamAdapter(const JavaInputStreamAdapter&); + +public: + JavaInputStreamAdapter(jobject inputStream) : inputStream(inputStream) { + s_adapters.insert(this); + } + + ~JavaInputStreamAdapter() { + s_adapters.erase(this); + } + + static void pullAdapters(JNIEnv* jenv) { + for(std::set<JavaInputStreamAdapter*>::iterator i = s_adapters.begin(); + i != s_adapters.end(); + ++i) { + (*i)->pull(jenv); + } + } + + jobject getInputStream() const { + return inputStream; + } + + void pull(JNIEnv* jenv) { + if(fail() || eof()) { + clear(); + } + jclass clazz = jenv->FindClass("java/io/InputStream"); + assert(clazz != NULL && jenv->ExceptionOccurred() == NULL); + jmethodID method = jenv->GetMethodID(clazz, "available", "()I"); + assert(method != NULL && jenv->ExceptionOccurred() == NULL); + jint available = jenv->CallIntMethod(inputStream, method); + assert(jenv->ExceptionOccurred() == NULL); + jbyteArray bytes = jenv->NewByteArray(available); + assert(bytes != NULL && jenv->ExceptionOccurred() == NULL); + method = jenv->GetMethodID(clazz, "read", "([B)I"); + assert(method != NULL && jenv->ExceptionOccurred() == NULL); + jint nread = jenv->CallIntMethod(inputStream, method, bytes); + assert(jenv->ExceptionOccurred() == NULL); + jbyte* bptr = jenv->GetByteArrayElements(bytes, NULL); + assert(jenv->ExceptionOccurred() == NULL); + std::copy(bptr, bptr + nread, std::ostream_iterator<char>(*this)); + *this << std::flush; + jenv->ReleaseByteArrayElements(bytes, bptr, 0); + assert(jenv->ExceptionOccurred() == NULL); + assert(good()); + assert(!eof()); + } + +};/* class JavaInputStreamAdapter */ + +}/* CVC4 namespace */ + +#endif /* __CVC4__BINDINGS__JAVA_STREAM_ADAPTERS_H */ |