3
* ====================================================================
4
* Copyright (c) 2003-2004 CollabNet. All rights reserved.
6
* This software is licensed as described in the file COPYING, which
7
* you should have received as part of this distribution. The terms
8
* are also available at http://subversion.tigris.org/license-1.html.
9
* If newer versions of this license are posted there, you may use a
10
* newer version instead, at your option.
12
* This software consists of voluntary contributions made by many
13
* individuals. For exact contribution history, see the revision
14
* history and logs, available at http://subversion.tigris.org/.
15
* ====================================================================
19
* @brief Implementation of the class Inputer
24
#include "JNIByteArray.h"
26
* create an Inputer object
27
* @param jthis the java object to be stored
29
Inputer::Inputer(jobject jthis)
34
* destroy an Inputer object
38
// the m_jthis does not need to be destroyed, because it is the passed
39
// in parameter to the java method.
43
* create a svn_stream_t structure for this object. This will be used as an
44
* input stream by subversion
45
* @param pool the pool, from which the structure is allocated
46
* @return the input stream
48
svn_stream_t *Inputer::getStream(const Pool & pool)
50
// create a stream with this as the baton and set the read and close
52
svn_stream_t *ret = svn_stream_create(this, pool.pool());
53
svn_stream_set_read(ret, Inputer::read);
54
svn_stream_set_close(ret, Inputer::close);
58
* implements svn_read_fn_t to read to data into subversion
59
* @param baton an Inputer object for the callback
60
* @param buffer the buffer for the read data
61
* @param len on input the buffer len, on output the number of read bytes
62
* @return a subversion error or SVN_NO_ERROR
64
svn_error_t *Inputer::read(void *baton, char *buffer, apr_size_t *len)
66
JNIEnv *env = JNIUtil::getEnv();
67
// an object of our class is passed in as the baton
68
Inputer *that = (Inputer*)baton;
70
// the method id will not change during
71
// the time this library is loaded, so
73
static jmethodID mid = 0;
76
jclass clazz = env->FindClass(JAVA_PACKAGE"/InputInterface");
77
if(JNIUtil::isJavaExceptionThrown())
81
mid = env->GetMethodID(clazz, "read", "([B)I");
82
if(JNIUtil::isJavaExceptionThrown() || mid == 0)
86
env->DeleteLocalRef(clazz);
87
if(JNIUtil::isJavaExceptionThrown())
93
// allocate a java byte array to read the data
95
JNIUtil::makeJByteArray((const signed char*)buffer, *len);
96
if(JNIUtil::isJavaExceptionThrown())
102
jint jread = env->CallIntMethod(that->m_jthis, mid, data);
103
if(JNIUtil::isJavaExceptionThrown())
108
// put the java byte array into a helper object to retrieve the data bytes
109
JNIByteArray outdata(data, true);
110
if(JNIUtil::isJavaExceptionThrown())
115
// catch when the java method tells us, it read to much data.
119
// in the case of success, copy the data back to the subversion buffer
121
memcpy(buffer, outdata.getBytes(), jread);
123
// copy the number of read bytes back to subversion
129
* implements svn_close_fn_t to close the input stream
130
* @param baton an Inputer object for the callback
131
* @return a subversion error or SVN_NO_ERROR
133
svn_error_t *Inputer::close(void *baton)
135
JNIEnv *env = JNIUtil::getEnv();
136
// an object of our class is passed in as the baton
137
Inputer *that = (Inputer*)baton;
139
// the method id will not change during
140
// the time this library is loaded, so
142
static jmethodID mid = 0;
145
jclass clazz = env->FindClass(JAVA_PACKAGE"/InputInterface");
146
if(JNIUtil::isJavaExceptionThrown())
150
mid = env->GetMethodID(clazz, "close", "()V");
151
if(JNIUtil::isJavaExceptionThrown() || mid == 0)
155
env->DeleteLocalRef(clazz);
156
if(JNIUtil::isJavaExceptionThrown())
162
// call the java object, to close the stream
163
env->CallVoidMethod(that->m_jthis, mid);
164
if(JNIUtil::isJavaExceptionThrown())