1 #include "jnistream.hxx"
6 static void jthrow(JNIEnv *env, const char *name, const char *msg)
8 jclass cls = env->FindClass(name);
9 /* if cls is NULL, an exception has already been thrown => remove it because it's from the JVM*/
12 env->ExceptionClear();
17 if(env->ThrowNew(cls, msg)<0)
19 //the throw exception failled
20 std::cout<<"#JNI throw exception failled !"<<std::endl;
21 std::cout<<name<<"\t : "<<msg<<std::endl;
23 /* free the local ref */
24 env->DeleteLocalRef(cls);
28 jnistreambuf::~jnistreambuf()
30 delete[] nativeBuffer;
33 jnistreambuf::jnistreambuf(JNIEnv * env, jobject jstream, int bufferSize):
34 env(env), jstream(jstream)
37 cerr << "jnistreambuf::jnistreambuf [start]" << endl;
39 nativeBuffer = new char[bufferSize];
40 fullNioBuffer=env->NewDirectByteBuffer(nativeBuffer, bufferSize);
42 jclass inputStreamClass = env->FindClass("java/nio/channels/ReadableByteChannel");
43 jclass outputStreamClass = env->FindClass("java/nio/channels/WritableByteChannel");
45 readMID = env->GetMethodID(inputStreamClass, "read","(Ljava/nio/ByteBuffer;)I");
46 writeMID = env->GetMethodID(outputStreamClass, "write","(Ljava/nio/ByteBuffer;)I");
48 bool in=env->IsInstanceOf(jstream, inputStreamClass)!=0;
49 bool out=env->IsInstanceOf(jstream, outputStreamClass)!=0;
54 cerr << "jnistreambuf::jnistreambuf: init reading buffer" << endl;
56 setg(nativeBuffer, // beginning of putback area
57 nativeBuffer, // read position
58 nativeBuffer); // end position
64 cerr << "jnistreambuf::jnistreambuf: init writting buffer [start]" << endl;
66 setp( nativeBuffer, nativeBuffer + bufferSize );
69 cerr << "jnistreambuf::jnistreambuf: init writting buffer [done]" << endl;
75 jthrow(env, "java/lang/ClassCastException", "InputStream or OutputStream expected");
79 cerr << "jnistreambuf::jnistreambuf [done]" << endl;
83 int jnistreambuf::underflow()
86 cerr << "jnistreambuf::underflow" << endl;
88 // used for input buffer only
89 if ( gptr() && ( gptr() < egptr()))
90 return * reinterpret_cast<unsigned char *>( gptr());
93 int num = env->CallIntMethod(jstream, readMID, fullNioBuffer);
95 if (num <= 0) // ERROR or EOF
98 // reset buffer pointers
99 setg( eback(), // beginning of putback area
100 eback(), // read position
101 eback() + num); // end of buffer
103 // return next character
104 return * reinterpret_cast<unsigned char *>( gptr());
107 int jnistreambuf::overflow( int c)
110 cerr << "jnistreambuf::overflow" << endl;
112 // used for output buffer only
118 if ( flush_buffer() == EOF)
123 int jnistreambuf::sync()
126 cerr << "jnistreambuf::sync()" << endl;
128 // Changed to use flush_buffer() instead of overflow( EOF)
129 // which caused improper behavior with std::endl and flush(),
130 // bug reported by Vincent Ricard.
131 if ( pptr() && pptr() > pbase()) {
132 if ( flush_buffer() == EOF)
138 int jnistreambuf::flush_buffer()
141 cerr << "jnistreambuf::flush_buffer" << endl;
142 //for(char * p=pbase(); p!=pptr(); p++) cerr << *p;
144 // Separate the writing of the buffer from overflow() and
146 int w = pptr() - pbase();
152 nioBuffer=fullNioBuffer;
154 nioBuffer=env->NewDirectByteBuffer(pbase(), w);
156 env->CallIntMethod(jstream, writeMID, nioBuffer);