summaryrefslogtreecommitdiff
path: root/subversion/bindings/javahl/native/InputStream.cpp
blob: ac10f9e2589905bfd747673c9b748d7339e43567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
 * @copyright
 * ====================================================================
 *    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.
 * ====================================================================
 * @endcopyright
 *
 * @file InputStream.cpp
 * @brief Implementation of the class InputStream
 */

#include "InputStream.h"
#include "JNIUtil.h"
#include "JNIByteArray.h"

/**
 * Create an InputStream object.
 * @param jthis the Java object to be stored
 */
InputStream::InputStream(jobject jthis)
{
  m_jthis = jthis;
}

InputStream::~InputStream()
{
  // The m_jthis does not need to be destroyed, because it is the
  // passed in parameter to the Java method.
}

/**
 * Create a svn_stream_t structure for this object. This will be used
 * as an input stream by Subversion.
 * @param pool  the pool, from which the structure is allocated
 * @return the input stream
 */
svn_stream_t *InputStream::getStream(const SVN::Pool &pool)
{
  // Create a stream with this as the baton and set the read and
  // close functions.
  svn_stream_t *ret = svn_stream_create(this, pool.getPool());
  svn_stream_set_read2(ret, InputStream::read,
                       NULL /* only partial read support */);
  svn_stream_set_close(ret, InputStream::close);
  return ret;
}

/**
 * Implements svn_read_fn_t to read to data into Subversion.
 * @param baton     an InputStream object for the callback
 * @param buffer    the buffer for the read data
 * @param len       on input the buffer len, on output the number of read bytes
 * @return a subversion error or SVN_NO_ERROR
 */
svn_error_t *InputStream::read(void *baton, char *buffer, apr_size_t *len)
{
  if (0 == *len)
    return SVN_NO_ERROR;

  JNIEnv *env = JNIUtil::getEnv();
  // An object of our class is passed in as the baton.
  InputStream *that = static_cast<InputStream *>(baton);

  // The method id will not change during the time this library is
  // loaded, so it can be cached.
  static jmethodID mid = 0;
  if (mid == 0)
    {
      jclass clazz = env->FindClass("java/io/InputStream");
      if (JNIUtil::isJavaExceptionThrown())
        return SVN_NO_ERROR;

      mid = env->GetMethodID(clazz, "read", "([B)I");
      if (JNIUtil::isJavaExceptionThrown() || mid == 0)
        return SVN_NO_ERROR;

      env->DeleteLocalRef(clazz);
    }

  // Allocate a Java byte array to read the data.
  jbyteArray data = JNIUtil::makeJByteArray(buffer, static_cast<int>(*len));
  if (JNIUtil::isJavaExceptionThrown())
    return SVN_NO_ERROR;

  // Read the data.
  jint jread = env->CallIntMethod(that->m_jthis, mid, data);
  if (JNIUtil::isJavaExceptionThrown())
    return SVN_NO_ERROR;

  /*
   * Convert -1 from InputStream.read that means EOF, 0 which is subversion equivalent
   */
  if(jread == -1)
    {
      jread = 0;
    }

  // Put the Java byte array into a helper object to retrieve the
  // data bytes.
  JNIByteArray outdata(data, true);
  if (JNIUtil::isJavaExceptionThrown())
    return SVN_NO_ERROR;

  // Catch when the Java method tells us it read too much data.
  if (jread > (jint) *len)
    jread = 0;

  // In the case of success copy the data back to the Subversion
  // buffer.
  if (jread > 0)
    memcpy(buffer, outdata.getBytes(), jread);

  // Copy the number of read bytes back to Subversion.
  *len = jread;

  return SVN_NO_ERROR;
}

/**
 * Implements svn_close_fn_t to close the input stream.
 * @param baton     an InputStream object for the callback
 * @return a subversion error or SVN_NO_ERROR
 */
svn_error_t *InputStream::close(void *baton)
{
  JNIEnv *env = JNIUtil::getEnv();

  // An object of our class is passed in as the baton
  InputStream *that = reinterpret_cast<InputStream*>(baton);

  // The method id will not change during the time this library is
  // loaded, so it can be cached.
  static jmethodID mid = 0;
  if (mid == 0)
    {
      jclass clazz = env->FindClass("java/io/InputStream");
      if (JNIUtil::isJavaExceptionThrown())
        return SVN_NO_ERROR;

      mid = env->GetMethodID(clazz, "close", "()V");
      if (JNIUtil::isJavaExceptionThrown() || mid == 0)
        return SVN_NO_ERROR;

      env->DeleteLocalRef(clazz);
    }

  // Call the Java object, to close the stream.
  env->CallVoidMethod(that->m_jthis, mid);
  // We don't need to check for an exception here because we return anyway.

  return SVN_NO_ERROR;
}