~svn/ubuntu/raring/subversion/ppa

« back to all changes in this revision

Viewing changes to subversion/bindings/java/javahl/native/Outputer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-12-05 01:26:14 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051205012614-qom4xfypgtsqc2xq
Tags: 1.2.3dfsg1-3ubuntu1
Merge with the final Debian release of 1.2.3dfsg1-3, bringing in
fixes to the clean target, better documentation of the libdb4.3
upgrade and build fixes to work with swig1.3_1.3.27.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @copyright
 
3
 * ====================================================================
 
4
 * Copyright (c) 2003-2004 CollabNet.  All rights reserved.
 
5
 *
 
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.
 
11
 *
 
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
 * ====================================================================
 
16
 * @endcopyright
 
17
 *
 
18
 * @file Outputer.cpp
 
19
 * @brief Implementation of the class Outputer
 
20
 */
 
21
 
 
22
#include "Outputer.h"
 
23
#include "JNIUtil.h"
 
24
#include "JNIByteArray.h"
 
25
 
 
26
/**
 
27
 * create an Outputer object
 
28
 * @param jthis the java object to be stored
 
29
 */
 
30
Outputer::Outputer(jobject jthis)
 
31
{
 
32
    m_jthis = jthis;
 
33
}
 
34
 
 
35
/**
 
36
 * destroy an Inputer object
 
37
 */
 
38
Outputer::~Outputer()
 
39
{
 
40
    // the m_jthis does not need to be destroyed, because it is the passed
 
41
    // in parameter to the java method.
 
42
 
 
43
}
 
44
 
 
45
/**
 
46
 * create a svn_stream_t structure for this object. This will be used as an
 
47
 * output stream by subversion
 
48
 * @param pool  the pool, from which the structure is allocated
 
49
 * @return the output stream
 
50
 */
 
51
svn_stream_t *Outputer::getStream(const Pool & pool)
 
52
{
 
53
    // create a stream with this as the baton and set the write and close
 
54
    // functions
 
55
    svn_stream_t *ret = svn_stream_create(this, pool.pool());
 
56
    svn_stream_set_write(ret, Outputer::write);
 
57
    svn_stream_set_close(ret, Outputer::close);
 
58
    return ret;
 
59
}
 
60
 
 
61
/**
 
62
 * implements svn_write_fn_t to write data out from subversion
 
63
 * @param baton     an Outputer object for the callback
 
64
 * @param buffer    the buffer for the write data
 
65
 * @param len       on input the buffer len, on output the number of written 
 
66
 *                  bytes
 
67
 * @return a subversion error or SVN_NO_ERROR
 
68
 */
 
69
svn_error_t *Outputer::write(void *baton, const char *buffer, apr_size_t *len)
 
70
{
 
71
    JNIEnv *env = JNIUtil::getEnv();
 
72
    // an object of our class is passed in as the baton
 
73
    Outputer *that = (Outputer*)baton;
 
74
 
 
75
    // the method id will not change during
 
76
    // the time this library is loaded, so
 
77
    // it can be cached.
 
78
   static jmethodID mid = 0;
 
79
    if(mid == 0)
 
80
    {
 
81
        jclass clazz = env->FindClass(JAVA_PACKAGE"/OutputInterface");
 
82
        if(JNIUtil::isJavaExceptionThrown())
 
83
        {
 
84
            return SVN_NO_ERROR;
 
85
        }
 
86
        mid = env->GetMethodID(clazz, "write", "([B)I");
 
87
        if(JNIUtil::isJavaExceptionThrown() || mid == 0)
 
88
        {
 
89
            return SVN_NO_ERROR;
 
90
        }
 
91
        env->DeleteLocalRef(clazz);
 
92
        if(JNIUtil::isJavaExceptionThrown())
 
93
        {
 
94
            return SVN_NO_ERROR;
 
95
        }
 
96
    }
 
97
 
 
98
    // convert the data to a java byte array
 
99
    jbyteArray data = JNIUtil::makeJByteArray((const signed char*)buffer, *len);
 
100
    if(JNIUtil::isJavaExceptionThrown())
 
101
    {
 
102
        return SVN_NO_ERROR;
 
103
    }
 
104
 
 
105
    // write the data
 
106
    jint written = env->CallIntMethod(that->m_jthis, mid, data);
 
107
    if(JNIUtil::isJavaExceptionThrown())
 
108
    {
 
109
        return SVN_NO_ERROR;
 
110
    }
 
111
 
 
112
    // return the number of bytes written
 
113
    *len = written;
 
114
 
 
115
    return SVN_NO_ERROR;
 
116
}
 
117
/**
 
118
 * implements svn_close_fn_t to close the output stream
 
119
 * @param baton     an Outputer object for the callback
 
120
 * @return a subversion error or SVN_NO_ERROR
 
121
 */
 
122
svn_error_t *Outputer::close(void *baton)
 
123
{
 
124
    JNIEnv *env = JNIUtil::getEnv();
 
125
    // an object of our class is passed in as the baton
 
126
    Outputer *that = (Outputer*)baton;
 
127
 
 
128
    // the method id will not change during
 
129
    // the time this library is loaded, so
 
130
    // it can be cached.
 
131
    static jmethodID mid = 0;
 
132
    if(mid == 0)
 
133
    {
 
134
        jclass clazz = env->FindClass(JAVA_PACKAGE"/OutputInterface");
 
135
        if(JNIUtil::isJavaExceptionThrown())
 
136
        {
 
137
            return SVN_NO_ERROR;
 
138
        }
 
139
        mid = env->GetMethodID(clazz, "close", "()V");
 
140
        if(JNIUtil::isJavaExceptionThrown() || mid == 0)
 
141
        {
 
142
            return SVN_NO_ERROR;
 
143
        }
 
144
        env->DeleteLocalRef(clazz);
 
145
        if(JNIUtil::isJavaExceptionThrown())
 
146
        {
 
147
            return SVN_NO_ERROR;
 
148
        }
 
149
    }
 
150
 
 
151
    // call the java object, to close the stream
 
152
    env->CallVoidMethod(that->m_jthis, mid);
 
153
    if(JNIUtil::isJavaExceptionThrown())
 
154
    {
 
155
        return SVN_NO_ERROR;
 
156
    }
 
157
 
 
158
    return SVN_NO_ERROR;
 
159
}