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/stl_types.hxx>
30 : #include <comphelper/types.hxx>
31 : #include "hsqldb/HStorageAccess.hxx"
32 : #include "hsqldb/HStorageMap.hxx"
33 : #include "hsqldb/StorageNativeInputStream.h"
34 :
35 : #include "jvmaccess/virtualmachine.hxx"
36 : #include <com/sun/star/lang/XSingleComponentFactory.hpp>
37 : #include "accesslog.hxx"
38 :
39 : #include <limits>
40 :
41 :
42 : using namespace ::com::sun::star::container;
43 : using namespace ::com::sun::star::uno;
44 : using namespace ::com::sun::star::document;
45 : using namespace ::com::sun::star::embed;
46 : using namespace ::com::sun::star::io;
47 : using namespace ::com::sun::star::lang;
48 : using namespace ::connectivity::hsqldb;
49 :
50 : /*****************************************************************************/
51 : /* exception macros */
52 :
53 : #define ThrowException(env, type, msg) { \
54 : env->ThrowNew(env->FindClass(type), msg); }
55 : /*
56 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
57 : * Method: openStream
58 : * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
59 : */
60 0 : SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_openStream
61 : (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jint mode)
62 : {
63 : #ifdef HSQLDB_DBG
64 : {
65 : OperationLogFile( env, name, "input" ).logOperation( "openStream" );
66 : LogFile( env, name, "input" ).create();
67 : }
68 : #endif
69 0 : StorageContainer::registerStream(env,name,key,mode);
70 0 : }
71 : // -----------------------------------------------------------------------------
72 :
73 : /*
74 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
75 : * Method: read
76 : * Signature: (Ljava/lang/String;Ljava/lang/String;)I
77 : */
78 0 : SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2
79 : (JNIEnv * env, jobject obj_this,jstring key, jstring name)
80 : {
81 : #ifdef HSQLDB_DBG
82 : OperationLogFile( env, name, "input" ).logOperation( "read()" );
83 :
84 : DataLogFile aDataLog( env, name, "input" );
85 : return read_from_storage_stream( env, obj_this, name, key, &aDataLog );
86 : #else
87 0 : return read_from_storage_stream( env, obj_this, name, key );
88 : #endif
89 : }
90 : // -----------------------------------------------------------------------------
91 :
92 : /*
93 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
94 : * Method: read
95 : * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)I
96 : */
97 0 : SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2_3BII
98 : (JNIEnv * env, jobject obj_this,jstring key, jstring name, jbyteArray buffer, jint off, jint len)
99 : {
100 : #ifdef HSQLDB_DBG
101 : OperationLogFile( env, name, "input" ).logOperation( "read( byte[], int, int )" );
102 :
103 : DataLogFile aDataLog( env, name, "input" );
104 : return read_from_storage_stream_into_buffer( env, obj_this, name, key, buffer, off, len, &aDataLog );
105 : #else
106 0 : return read_from_storage_stream_into_buffer(env,obj_this,name,key,buffer,off,len);
107 : #endif
108 : }
109 : // -----------------------------------------------------------------------------
110 :
111 : /*
112 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
113 : * Method: close
114 : * Signature: (Ljava/lang/String;Ljava/lang/String;)V
115 : */
116 0 : SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_close
117 : (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name)
118 : {
119 : #ifdef HSQLDB_DBG
120 : OperationLogFile aOpLog( env, name, "input" );
121 : aOpLog.logOperation( "close" );
122 : aOpLog.close();
123 :
124 : LogFile aDataLog( env, name, "input" );
125 : aDataLog.close();
126 : #endif
127 0 : StorageContainer::revokeStream(env,name,key);
128 0 : }
129 : // -----------------------------------------------------------------------------
130 :
131 : /*
132 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
133 : * Method: skip
134 : * Signature: (Ljava/lang/String;Ljava/lang/String;J)J
135 : */
136 0 : SAL_JNI_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_skip
137 : (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jlong n)
138 : {
139 : #ifdef HSQLDB_DBG
140 : OperationLogFile( env, name, "input" ).logOperation( "skip()" );
141 : #endif
142 :
143 0 : if ( n < 0 )
144 0 : ThrowException( env,
145 : "java/io/IOException",
146 : "n < 0");
147 :
148 0 : ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
149 : OSL_ENSURE(pHelper.get(),"No stream helper!");
150 0 : if ( pHelper.get() )
151 : {
152 0 : Reference<XInputStream> xIn = pHelper->getInputStream();
153 0 : if ( xIn.is() )
154 : {
155 : try
156 : {
157 0 : sal_Int64 tmpLongVal = n;
158 : sal_Int32 tmpIntVal;
159 :
160 : try
161 : {
162 0 : do {
163 0 : if (tmpLongVal >= ::std::numeric_limits<sal_Int64>::max() )
164 0 : tmpIntVal = ::std::numeric_limits<sal_Int32>::max();
165 : else // Casting is safe here.
166 0 : tmpIntVal = static_cast<sal_Int32>(tmpLongVal);
167 :
168 0 : tmpLongVal -= tmpIntVal;
169 :
170 0 : xIn->skipBytes(tmpIntVal);
171 :
172 : } while (tmpLongVal > 0);
173 : }
174 0 : catch(const Exception&)
175 : {
176 : }
177 :
178 0 : return n - tmpLongVal;
179 : }
180 0 : catch(const Exception& e)
181 : {
182 : OSL_FAIL("Exception caught! : skip();");
183 0 : StorageContainer::throwJavaException(e,env);
184 : }
185 0 : }
186 : }
187 : else
188 : {
189 0 : ThrowException( env,
190 : "java/io/IOException",
191 : "Stream is not valid");
192 : }
193 0 : return 0;
194 : }
195 : // -----------------------------------------------------------------------------
196 :
197 : /*
198 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
199 : * Method: available
200 : * Signature: (Ljava/lang/String;Ljava/lang/String;)I
201 : */
202 0 : SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_available
203 : (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name)
204 : {
205 : #ifdef HSQLDB_DBG
206 : OperationLogFile aOpLog( env, name, "input" );
207 : aOpLog.logOperation( "available" );
208 : #endif
209 :
210 0 : ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
211 : OSL_ENSURE(pHelper.get(),"No stream helper!");
212 0 : Reference<XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference<XInputStream>();
213 0 : if ( xIn.is() )
214 : {
215 : try
216 : {
217 0 : jint nAvailable = xIn->available();
218 : #ifdef HSQLDB_DBG
219 : aOpLog.logReturn( nAvailable );
220 : #endif
221 0 : return nAvailable;
222 : }
223 0 : catch(const Exception& e)
224 : {
225 : OSL_FAIL("Exception caught! : available();");
226 0 : StorageContainer::throwJavaException(e,env);
227 : }
228 : }
229 : else
230 : {
231 0 : ThrowException( env,
232 : "java/io/IOException",
233 : "Stream is not valid");
234 : }
235 0 : return 0;
236 : }
237 : // -----------------------------------------------------------------------------
238 :
239 : /*
240 : * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
241 : * Method: read
242 : * Signature: (Ljava/lang/String;Ljava/lang/String;[B)I
243 : */
244 0 : SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2_3B
245 : (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jbyteArray buffer)
246 : {
247 : #ifdef HSQLDB_DBG
248 : OperationLogFile aOpLog( env, name, "input" );
249 : aOpLog.logOperation( "read( byte[] )" );
250 :
251 : DataLogFile aDataLog( env, name, "input" );
252 : #endif
253 :
254 0 : ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
255 0 : Reference< XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference< XInputStream>();
256 : OSL_ENSURE(xIn.is(),"Input stream is NULL!");
257 0 : jint nBytesRead = 0;
258 0 : if ( xIn.is() )
259 : {
260 0 : jsize nLen = env->GetArrayLength(buffer);
261 0 : Sequence< ::sal_Int8 > aData(nLen);
262 :
263 : try
264 : {
265 0 : nBytesRead = xIn->readBytes(aData,nLen);
266 : }
267 0 : catch(const Exception& e)
268 : {
269 : OSL_FAIL("Exception caught! : skip();");
270 0 : StorageContainer::throwJavaException(e,env);
271 : }
272 :
273 : // Casting bytesRead to an int is okay, since the user can
274 : // only pass in an integer length to read, so the bytesRead
275 : // must <= len.
276 : //
277 0 : if (nBytesRead <= 0) {
278 : #ifdef HSQLDB_DBG
279 : aOpLog.logReturn( (jint)-1 );
280 : #endif
281 0 : return -1;
282 : }
283 : OSL_ENSURE(nLen >= nBytesRead,"Buffer is too small!");
284 : OSL_ENSURE(aData.getLength() >= nBytesRead,"Buffer is too small!");
285 0 : env->SetByteArrayRegion(buffer, 0, nBytesRead, (jbyte*) &aData[0]);
286 : #ifdef HSQLDB_DBG
287 : aDataLog.write( &aData[0], nBytesRead );
288 : #endif
289 : }
290 : #ifdef HSQLDB_DBG
291 : aOpLog.logReturn( nBytesRead );
292 : #endif
293 0 : return nBytesRead;
294 : }
295 : // -----------------------------------------------------------------------------
296 :
297 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|