Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
22 : #include <config.h>
23 : #endif
24 : #include <com/sun/star/io/XStream.hpp>
25 : #include <com/sun/star/container/XNameAccess.hpp>
26 : #include <com/sun/star/document/XDocumentSubStorageSupplier.hpp>
27 : #include <com/sun/star/embed/XStorage.hpp>
28 : #include <com/sun/star/embed/ElementModes.hpp>
29 : #include <comphelper/types.hxx>
30 : #include "hsqldb/HStorageAccess.hxx"
31 : #include "hsqldb/HStorageMap.hxx"
32 :
33 : #include <jvmaccess/virtualmachine.hxx>
34 : #include <com/sun/star/lang/XSingleComponentFactory.hpp>
35 : #include <osl/diagnose.h>
36 : #include "accesslog.hxx"
37 :
38 : #include <limits>
39 :
40 :
41 : using namespace ::com::sun::star::container;
42 : using namespace ::com::sun::star::uno;
43 : using namespace ::com::sun::star::document;
44 : using namespace ::com::sun::star::embed;
45 : using namespace ::com::sun::star::io;
46 : using namespace ::com::sun::star::lang;
47 : using namespace ::connectivity::hsqldb;
48 :
49 : /*****************************************************************************/
50 : /* exception macros */
51 :
52 : #define ThrowException(env, type, msg) { \
53 : env->ThrowNew(env->FindClass(type), msg); }
54 : /*
55 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
56 : * Method: openStream
57 : * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
58 : */
59 25 : extern "C" SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_openStream
60 : (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jint mode)
61 : {
62 : #ifdef HSQLDB_DBG
63 : {
64 : OperationLogFile( env, name, "input" ).logOperation( "openStream" );
65 : LogFile( env, name, "input" ).create();
66 : }
67 : #endif
68 25 : StorageContainer::registerStream(env,name,key,mode);
69 25 : }
70 :
71 :
72 : /*
73 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
74 : * Method: read
75 : * Signature: (Ljava/lang/String;Ljava/lang/String;)I
76 : */
77 0 : extern "C" SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2
78 : (JNIEnv * env, jobject obj_this,jstring key, jstring name)
79 : {
80 : #ifdef HSQLDB_DBG
81 : OperationLogFile( env, name, "input" ).logOperation( "read()" );
82 :
83 : DataLogFile aDataLog( env, name, "input" );
84 : return read_from_storage_stream( env, obj_this, name, key, &aDataLog );
85 : #else
86 0 : return read_from_storage_stream( env, obj_this, name, key );
87 : #endif
88 : }
89 :
90 :
91 : /*
92 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
93 : * Method: read
94 : * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)I
95 : */
96 45 : extern "C" SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2_3BII
97 : (JNIEnv * env, jobject obj_this,jstring key, jstring name, jbyteArray buffer, jint off, jint len)
98 : {
99 : #ifdef HSQLDB_DBG
100 : OperationLogFile( env, name, "input" ).logOperation( "read( byte[], int, int )" );
101 :
102 : DataLogFile aDataLog( env, name, "input" );
103 : return read_from_storage_stream_into_buffer( env, obj_this, name, key, buffer, off, len, &aDataLog );
104 : #else
105 45 : return read_from_storage_stream_into_buffer(env,obj_this,name,key,buffer,off,len);
106 : #endif
107 : }
108 :
109 :
110 : /*
111 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
112 : * Method: close
113 : * Signature: (Ljava/lang/String;Ljava/lang/String;)V
114 : */
115 25 : extern "C" SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_close
116 : (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name)
117 : {
118 : #ifdef HSQLDB_DBG
119 : OperationLogFile aOpLog( env, name, "input" );
120 : aOpLog.logOperation( "close" );
121 : aOpLog.close();
122 :
123 : LogFile aDataLog( env, name, "input" );
124 : aDataLog.close();
125 : #endif
126 25 : StorageContainer::revokeStream(env,name,key);
127 25 : }
128 :
129 :
130 : /*
131 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
132 : * Method: skip
133 : * Signature: (Ljava/lang/String;Ljava/lang/String;J)J
134 : */
135 0 : extern "C" SAL_JNI_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_skip
136 : (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jlong n)
137 : {
138 : #ifdef HSQLDB_DBG
139 : OperationLogFile( env, name, "input" ).logOperation( "skip()" );
140 : #endif
141 :
142 0 : if ( n < 0 )
143 0 : ThrowException( env,
144 : "java/io/IOException",
145 : "n < 0");
146 :
147 0 : ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
148 : OSL_ENSURE(pHelper.get(),"No stream helper!");
149 0 : if ( pHelper.get() )
150 : {
151 0 : Reference<XInputStream> xIn = pHelper->getInputStream();
152 0 : if ( xIn.is() )
153 : {
154 : try
155 : {
156 0 : sal_Int64 tmpLongVal = n;
157 : sal_Int32 tmpIntVal;
158 :
159 : try
160 : {
161 0 : do {
162 0 : if (tmpLongVal >= ::std::numeric_limits<sal_Int64>::max() )
163 0 : tmpIntVal = ::std::numeric_limits<sal_Int32>::max();
164 : else // Casting is safe here.
165 0 : tmpIntVal = static_cast<sal_Int32>(tmpLongVal);
166 :
167 0 : tmpLongVal -= tmpIntVal;
168 :
169 0 : xIn->skipBytes(tmpIntVal);
170 :
171 : } while (tmpLongVal > 0);
172 : }
173 0 : catch(const Exception&)
174 : {
175 : }
176 :
177 0 : return n - tmpLongVal;
178 : }
179 0 : catch(const Exception& e)
180 : {
181 : OSL_FAIL("Exception caught! : skip();");
182 0 : StorageContainer::throwJavaException(e,env);
183 : }
184 0 : }
185 : }
186 : else
187 : {
188 0 : ThrowException( env,
189 : "java/io/IOException",
190 : "Stream is not valid");
191 : }
192 0 : return 0;
193 : }
194 :
195 :
196 : /*
197 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
198 : * Method: available
199 : * Signature: (Ljava/lang/String;Ljava/lang/String;)I
200 : */
201 10 : extern "C" SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_available
202 : (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name)
203 : {
204 : #ifdef HSQLDB_DBG
205 : OperationLogFile aOpLog( env, name, "input" );
206 : aOpLog.logOperation( "available" );
207 : #endif
208 :
209 10 : ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
210 : OSL_ENSURE(pHelper.get(),"No stream helper!");
211 20 : Reference<XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference<XInputStream>();
212 10 : if ( xIn.is() )
213 : {
214 : try
215 : {
216 10 : jint nAvailable = xIn->available();
217 : #ifdef HSQLDB_DBG
218 : aOpLog.logReturn( nAvailable );
219 : #endif
220 10 : return nAvailable;
221 : }
222 0 : catch(const Exception& e)
223 : {
224 : OSL_FAIL("Exception caught! : available();");
225 0 : StorageContainer::throwJavaException(e,env);
226 : }
227 : }
228 : else
229 : {
230 0 : ThrowException( env,
231 : "java/io/IOException",
232 : "Stream is not valid");
233 : }
234 10 : return 0;
235 : }
236 :
237 :
238 : /*
239 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
240 : * Method: read
241 : * Signature: (Ljava/lang/String;Ljava/lang/String;[B)I
242 : */
243 9 : extern "C" SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2_3B
244 : (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jbyteArray buffer)
245 : {
246 : #ifdef HSQLDB_DBG
247 : OperationLogFile aOpLog( env, name, "input" );
248 : aOpLog.logOperation( "read( byte[] )" );
249 :
250 : DataLogFile aDataLog( env, name, "input" );
251 : #endif
252 :
253 9 : ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
254 18 : Reference< XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference< XInputStream>();
255 : OSL_ENSURE(xIn.is(),"Input stream is NULL!");
256 9 : jint nBytesRead = 0;
257 9 : if ( xIn.is() )
258 : {
259 9 : jsize nLen = env->GetArrayLength(buffer);
260 9 : Sequence< ::sal_Int8 > aData(nLen);
261 :
262 : try
263 : {
264 9 : nBytesRead = xIn->readBytes(aData,nLen);
265 : }
266 0 : catch(const Exception& e)
267 : {
268 : OSL_FAIL("Exception caught! : skip();");
269 0 : StorageContainer::throwJavaException(e,env);
270 : }
271 :
272 : // Casting bytesRead to an int is okay, since the user can
273 : // only pass in an integer length to read, so the bytesRead
274 : // must <= len.
275 :
276 9 : if (nBytesRead <= 0) {
277 : #ifdef HSQLDB_DBG
278 : aOpLog.logReturn( (jint)-1 );
279 : #endif
280 6 : return -1;
281 : }
282 : OSL_ENSURE(nLen >= nBytesRead,"Buffer is too small!");
283 : OSL_ENSURE(aData.getLength() >= nBytesRead,"Buffer is too small!");
284 3 : env->SetByteArrayRegion(buffer, 0, nBytesRead, reinterpret_cast<jbyte*>(&aData[0]));
285 : #ifdef HSQLDB_DBG
286 : aDataLog.write( &aData[0], nBytesRead );
287 : #endif
288 : }
289 : #ifdef HSQLDB_DBG
290 : aOpLog.logReturn( nBytesRead );
291 : #endif
292 12 : return nBytesRead;
293 : }
294 :
295 :
296 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|