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