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 #include "hsqldb/HStorageAccess.hxx"
27cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
28cdf0e10cSrcweir #include <com/sun/star/embed/XStorage.hpp>
29cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
30cdf0e10cSrcweir #include <com/sun/star/io/XStream.hpp>
31cdf0e10cSrcweir #include "hsqldb/HStorageMap.hxx"
32cdf0e10cSrcweir #include "hsqldb/StorageNativeInputStream.h"
33cdf0e10cSrcweir #include "accesslog.hxx"
34cdf0e10cSrcweir #include "diagnose_ex.h"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <string.h>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace ::com::sun::star::container;
39cdf0e10cSrcweir using namespace ::com::sun::star::uno;
40cdf0e10cSrcweir using namespace ::com::sun::star::embed;
41cdf0e10cSrcweir using namespace ::com::sun::star::io;
42cdf0e10cSrcweir using namespace ::com::sun::star::lang;
43cdf0e10cSrcweir using namespace ::connectivity::hsqldb;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
46cdf0e10cSrcweir #define ThrowException(env, type, msg) { \
47cdf0e10cSrcweir 	env->ThrowNew(env->FindClass(type), msg); }
48cdf0e10cSrcweir 
49cdf0e10cSrcweir /*
50cdf0e10cSrcweir  * Class:     com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
51cdf0e10cSrcweir  * Method:    openStream
52cdf0e10cSrcweir  * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
53cdf0e10cSrcweir  */
Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_openStream(JNIEnv * env,jobject,jstring name,jstring key,jint mode)54cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_openStream
55cdf0e10cSrcweir   (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key, jint mode)
56cdf0e10cSrcweir {
57cdf0e10cSrcweir #ifdef HSQLDB_DBG
58cdf0e10cSrcweir     {
59cdf0e10cSrcweir         OperationLogFile( env, name, "data" ).logOperation( "openStream" );
60cdf0e10cSrcweir         LogFile( env, name, "data" ).create();
61cdf0e10cSrcweir     }
62cdf0e10cSrcweir #endif
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	StorageContainer::registerStream(env,name,key,mode);
65cdf0e10cSrcweir }
66cdf0e10cSrcweir // -----------------------------------------------------------------------------
67cdf0e10cSrcweir /*
68cdf0e10cSrcweir  * Class:     com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
69cdf0e10cSrcweir  * Method:    close
70cdf0e10cSrcweir  * Signature: (Ljava/lang/String;Ljava/lang/String;)V
71cdf0e10cSrcweir  */
Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_close(JNIEnv * env,jobject,jstring name,jstring key)72cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_close
73cdf0e10cSrcweir   (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key)
74cdf0e10cSrcweir {
75cdf0e10cSrcweir #ifdef HSQLDB_DBG
76cdf0e10cSrcweir 	{
77cdf0e10cSrcweir 		::rtl::OUString sKey = StorageContainer::jstring2ustring(env,key);
78cdf0e10cSrcweir 		::rtl::OUString sName = StorageContainer::jstring2ustring(env,name);
79cdf0e10cSrcweir 	}
80cdf0e10cSrcweir #endif
81cdf0e10cSrcweir     ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
82cdf0e10cSrcweir     Reference< XOutputStream> xFlush = pHelper.get() ? pHelper->getOutputStream() : Reference< XOutputStream>();
83cdf0e10cSrcweir     if ( xFlush.is() )
84cdf0e10cSrcweir         try
85cdf0e10cSrcweir         {
86cdf0e10cSrcweir             xFlush->flush();
87cdf0e10cSrcweir         }
88cdf0e10cSrcweir         catch(Exception&)
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir             OSL_ENSURE( false, "NativeStorageAccess::close: caught an exception while flushing!" );
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir #ifdef HSQLDB_DBG
93cdf0e10cSrcweir     {
94cdf0e10cSrcweir         OperationLogFile aOpLog( env, name, "data" );
95cdf0e10cSrcweir         aOpLog.logOperation( "close" );
96cdf0e10cSrcweir         aOpLog.close();
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         LogFile aDataLog( env, name, "data" );
99cdf0e10cSrcweir         aDataLog.close();
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir #endif
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	StorageContainer::revokeStream(env,name,key);
104cdf0e10cSrcweir }
105cdf0e10cSrcweir // -----------------------------------------------------------------------------
106cdf0e10cSrcweir /*
107cdf0e10cSrcweir  * Class:     com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
108cdf0e10cSrcweir  * Method:    getFilePointer
109cdf0e10cSrcweir  * Signature: (Ljava/lang/String;Ljava/lang/String;)J
110cdf0e10cSrcweir  */
Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_getFilePointer(JNIEnv * env,jobject,jstring name,jstring key)111cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_getFilePointer
112cdf0e10cSrcweir   (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key)
113cdf0e10cSrcweir {
114cdf0e10cSrcweir #ifdef HSQLDB_DBG
115cdf0e10cSrcweir     OperationLogFile aOpLog( env, name, "data" );
116cdf0e10cSrcweir     aOpLog.logOperation( "getFilePointer" );
117cdf0e10cSrcweir #endif
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
120cdf0e10cSrcweir 	OSL_ENSURE(pHelper.get(),"No stream helper!");
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     jlong nReturn = pHelper.get() ? pHelper->getSeek()->getPosition() : jlong(0);
123cdf0e10cSrcweir #ifdef HSQLDB_DBG
124cdf0e10cSrcweir     aOpLog.logReturn( nReturn );
125cdf0e10cSrcweir #endif
126cdf0e10cSrcweir     return nReturn;
127cdf0e10cSrcweir }
128cdf0e10cSrcweir // -----------------------------------------------------------------------------
129cdf0e10cSrcweir 
130cdf0e10cSrcweir /*
131cdf0e10cSrcweir  * Class:     com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
132cdf0e10cSrcweir  * Method:    length
133cdf0e10cSrcweir  * Signature: (Ljava/lang/String;Ljava/lang/String;)J
134cdf0e10cSrcweir  */
Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_length(JNIEnv * env,jobject,jstring name,jstring key)135cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_length
136cdf0e10cSrcweir   (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key)
137cdf0e10cSrcweir {
138cdf0e10cSrcweir #ifdef HSQLDB_DBG
139cdf0e10cSrcweir     OperationLogFile aOpLog( env, name, "data" );
140cdf0e10cSrcweir     aOpLog.logOperation( "length" );
141cdf0e10cSrcweir #endif
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
144cdf0e10cSrcweir 	OSL_ENSURE(pHelper.get(),"No stream helper!");
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     jlong nReturn = pHelper.get() ? pHelper->getSeek()->getLength() :jlong(0);
147cdf0e10cSrcweir #ifdef HSQLDB_DBG
148cdf0e10cSrcweir     aOpLog.logReturn( nReturn );
149cdf0e10cSrcweir #endif
150cdf0e10cSrcweir     return nReturn;
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir // -----------------------------------------------------------------------------
154cdf0e10cSrcweir 
read_from_storage_stream(JNIEnv * env,jobject,jstring name,jstring key,DataLogFile * logger)155cdf0e10cSrcweir jint read_from_storage_stream( JNIEnv * env, jobject /*obj_this*/, jstring name, jstring key, DataLogFile* logger )
156cdf0e10cSrcweir {
157cdf0e10cSrcweir     OSL_UNUSED( logger );
158cdf0e10cSrcweir 	::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
159cdf0e10cSrcweir     Reference< XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference< XInputStream>();
160cdf0e10cSrcweir 	OSL_ENSURE(xIn.is(),"Input stream is NULL!");
161cdf0e10cSrcweir 	if ( xIn.is() )
162cdf0e10cSrcweir 	{
163cdf0e10cSrcweir 		Sequence< ::sal_Int8 > aData(1);
164cdf0e10cSrcweir         sal_Int32 nBytesRead = -1;
165cdf0e10cSrcweir         try
166cdf0e10cSrcweir         {
167cdf0e10cSrcweir 		    nBytesRead = xIn->readBytes(aData,1);
168cdf0e10cSrcweir         }
169cdf0e10cSrcweir         catch(Exception& e)
170cdf0e10cSrcweir 	    {
171cdf0e10cSrcweir             StorageContainer::throwJavaException(e,env);
172cdf0e10cSrcweir             return -1;
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 	    }
175cdf0e10cSrcweir 		if (nBytesRead <= 0)
176cdf0e10cSrcweir         {
177cdf0e10cSrcweir 			return (-1);
178cdf0e10cSrcweir 		}
179cdf0e10cSrcweir         else
180cdf0e10cSrcweir         {
181cdf0e10cSrcweir 			sal_Int32 tmpInt = aData[0];
182cdf0e10cSrcweir 			if (tmpInt < 0 )
183cdf0e10cSrcweir 				tmpInt = 256 +tmpInt;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir #ifdef HSQLDB_DBG
186cdf0e10cSrcweir             if ( logger )
187cdf0e10cSrcweir                 logger->write( tmpInt );
188cdf0e10cSrcweir #endif
189cdf0e10cSrcweir 			return(tmpInt);
190cdf0e10cSrcweir 		}
191cdf0e10cSrcweir 	}
192cdf0e10cSrcweir 	return -1;
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir // -----------------------------------------------------------------------------
196cdf0e10cSrcweir 
197cdf0e10cSrcweir /*
198cdf0e10cSrcweir  * Class:     com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
199cdf0e10cSrcweir  * Method:    read
200cdf0e10cSrcweir  * Signature: (Ljava/lang/String;Ljava/lang/String;)I
201cdf0e10cSrcweir  */
Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_read__Ljava_lang_String_2Ljava_lang_String_2(JNIEnv * env,jobject obj_this,jstring name,jstring key)202cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_read__Ljava_lang_String_2Ljava_lang_String_2
203cdf0e10cSrcweir   (JNIEnv* env, jobject obj_this, jstring name, jstring key)
204cdf0e10cSrcweir {
205cdf0e10cSrcweir #ifdef HSQLDB_DBG
206cdf0e10cSrcweir     OperationLogFile aOpLog( env, name, "data" );
207cdf0e10cSrcweir     aOpLog.logOperation( "read" );
208cdf0e10cSrcweir 
209cdf0e10cSrcweir     DataLogFile aDataLog( env, name, "data" );
210cdf0e10cSrcweir     return read_from_storage_stream( env, obj_this, name, key, &aDataLog );
211cdf0e10cSrcweir #else
212cdf0e10cSrcweir     return read_from_storage_stream( env, obj_this, name, key );
213cdf0e10cSrcweir #endif
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir // -----------------------------------------------------------------------------
217cdf0e10cSrcweir 
read_from_storage_stream_into_buffer(JNIEnv * env,jobject,jstring name,jstring key,jbyteArray buffer,jint off,jint len,DataLogFile * logger)218cdf0e10cSrcweir jint read_from_storage_stream_into_buffer( JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key, jbyteArray buffer, jint off, jint len, DataLogFile* logger )
219cdf0e10cSrcweir {
220cdf0e10cSrcweir     OSL_UNUSED( logger );
221cdf0e10cSrcweir #ifdef HSQLDB_DBG
222cdf0e10cSrcweir 	{
223cdf0e10cSrcweir 		::rtl::OUString sKey = StorageContainer::jstring2ustring(env,key);
224cdf0e10cSrcweir 		::rtl::OUString sName = StorageContainer::jstring2ustring(env,name);
225cdf0e10cSrcweir 	}
226cdf0e10cSrcweir #endif
227cdf0e10cSrcweir 	::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
228cdf0e10cSrcweir 	Reference< XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference< XInputStream>();
229cdf0e10cSrcweir 	OSL_ENSURE(xIn.is(),"Input stream is NULL!");
230cdf0e10cSrcweir 	if ( xIn.is() )
231cdf0e10cSrcweir 	{
232cdf0e10cSrcweir 		jsize nLen = env->GetArrayLength(buffer);
233cdf0e10cSrcweir         if ( nLen < len || len <= 0 )
234cdf0e10cSrcweir         {
235cdf0e10cSrcweir             ThrowException(	env,
236cdf0e10cSrcweir 					"java/io/IOException",
237cdf0e10cSrcweir 					"len is greater or equal to the buffer size");
238cdf0e10cSrcweir 	        return -1;
239cdf0e10cSrcweir         }
240cdf0e10cSrcweir 		sal_Int32 nBytesRead = -1;
241cdf0e10cSrcweir 
242cdf0e10cSrcweir         Sequence< ::sal_Int8 > aData(nLen);
243cdf0e10cSrcweir         try
244cdf0e10cSrcweir         {
245cdf0e10cSrcweir             nBytesRead = xIn->readBytes(aData, len);
246cdf0e10cSrcweir         }
247cdf0e10cSrcweir         catch(Exception& e)
248cdf0e10cSrcweir         {
249cdf0e10cSrcweir             StorageContainer::throwJavaException(e,env);
250cdf0e10cSrcweir             return -1;
251cdf0e10cSrcweir         }
252cdf0e10cSrcweir 
253cdf0e10cSrcweir         if (nBytesRead <= 0)
254cdf0e10cSrcweir             return -1;
255cdf0e10cSrcweir         env->SetByteArrayRegion(buffer,off,nBytesRead,&aData[0]);
256cdf0e10cSrcweir 
257cdf0e10cSrcweir #ifdef HSQLDB_DBG
258cdf0e10cSrcweir         if ( logger )
259cdf0e10cSrcweir             logger->write( aData.getConstArray(), nBytesRead );
260cdf0e10cSrcweir #endif
261cdf0e10cSrcweir 	    return nBytesRead;
262cdf0e10cSrcweir 	}
263cdf0e10cSrcweir 	ThrowException(	env,
264cdf0e10cSrcweir 					"java/io/IOException",
265cdf0e10cSrcweir 					"Stream is not valid");
266cdf0e10cSrcweir 	return -1;
267cdf0e10cSrcweir }
268cdf0e10cSrcweir // -----------------------------------------------------------------------------
269cdf0e10cSrcweir 
270cdf0e10cSrcweir /*
271cdf0e10cSrcweir  * Class:     com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
272cdf0e10cSrcweir  * Method:    read
273cdf0e10cSrcweir  * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)I
274cdf0e10cSrcweir  */
Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_read__Ljava_lang_String_2Ljava_lang_String_2_3BII(JNIEnv * env,jobject obj_this,jstring name,jstring key,jbyteArray buffer,jint off,jint len)275cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_read__Ljava_lang_String_2Ljava_lang_String_2_3BII
276cdf0e10cSrcweir   (JNIEnv * env, jobject obj_this,jstring name, jstring key, jbyteArray buffer, jint off, jint len)
277cdf0e10cSrcweir {
278cdf0e10cSrcweir #ifdef HSQLDB_DBG
279cdf0e10cSrcweir     OperationLogFile aOpLog( env, name, "data" );
280cdf0e10cSrcweir     aOpLog.logOperation( "read( byte[], int, int )" );
281cdf0e10cSrcweir 
282cdf0e10cSrcweir     DataLogFile aDataLog( env, name, "data" );
283cdf0e10cSrcweir     return read_from_storage_stream_into_buffer( env, obj_this, name, key, buffer, off, len, &aDataLog );
284cdf0e10cSrcweir #else
285cdf0e10cSrcweir     return read_from_storage_stream_into_buffer( env, obj_this, name, key, buffer, off, len );
286cdf0e10cSrcweir #endif
287cdf0e10cSrcweir }
288cdf0e10cSrcweir 
289cdf0e10cSrcweir // -----------------------------------------------------------------------------
290cdf0e10cSrcweir 
291cdf0e10cSrcweir /*
292cdf0e10cSrcweir  * Class:     com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
293cdf0e10cSrcweir  * Method:    readInt
294cdf0e10cSrcweir  * Signature: (Ljava/lang/String;Ljava/lang/String;)I
295cdf0e10cSrcweir  */
Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_readInt(JNIEnv * env,jobject,jstring name,jstring key)296cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_readInt
297cdf0e10cSrcweir   (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key)
298cdf0e10cSrcweir {
299cdf0e10cSrcweir #ifdef HSQLDB_DBG
300cdf0e10cSrcweir     OperationLogFile aOpLog( env, name, "data" );
301cdf0e10cSrcweir     aOpLog.logOperation( "readInt" );
302cdf0e10cSrcweir #endif
303cdf0e10cSrcweir 
304cdf0e10cSrcweir 	::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
305cdf0e10cSrcweir 	Reference< XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference< XInputStream>();
306cdf0e10cSrcweir 	OSL_ENSURE(xIn.is(),"Input stream is NULL!");
307cdf0e10cSrcweir 	if ( xIn.is() )
308cdf0e10cSrcweir     {
309cdf0e10cSrcweir         Sequence< ::sal_Int8 > aData(4);
310cdf0e10cSrcweir         sal_Int32 nBytesRead = -1;
311cdf0e10cSrcweir         try
312cdf0e10cSrcweir         {
313cdf0e10cSrcweir             nBytesRead = xIn->readBytes(aData, 4);
314cdf0e10cSrcweir         }
315cdf0e10cSrcweir         catch(Exception& e)
316cdf0e10cSrcweir         {
317cdf0e10cSrcweir             StorageContainer::throwJavaException(e,env);
318cdf0e10cSrcweir             return -1;
319cdf0e10cSrcweir         }
320cdf0e10cSrcweir 
321cdf0e10cSrcweir         if ( nBytesRead != 4 ) {
322cdf0e10cSrcweir             ThrowException( env,
323cdf0e10cSrcweir                             "java/io/IOException",
324cdf0e10cSrcweir                             "Bytes read != 4");
325cdf0e10cSrcweir             return -1;
326cdf0e10cSrcweir         }
327cdf0e10cSrcweir 
328cdf0e10cSrcweir         Sequence< sal_Int32 > ch(4);
329cdf0e10cSrcweir         for(sal_Int32 i = 0;i < 4; ++i)
330cdf0e10cSrcweir         {
331cdf0e10cSrcweir             ch[i] = aData[i];
332cdf0e10cSrcweir             if (ch[i] < 0 )
333cdf0e10cSrcweir                 ch[i] = 256 + ch[i];
334cdf0e10cSrcweir         }
335cdf0e10cSrcweir 
336cdf0e10cSrcweir         if ((ch[0] | ch[1] | ch[2] | ch[3]) < 0)
337cdf0e10cSrcweir         {
338cdf0e10cSrcweir             ThrowException( env,
339cdf0e10cSrcweir                             "java/io/IOException",
340cdf0e10cSrcweir                             "One byte is < 0");
341cdf0e10cSrcweir             return -1;
342cdf0e10cSrcweir         }
343cdf0e10cSrcweir         jint nRet = ((ch[0] << 24) + (ch[1] << 16) + (ch[2] << 8) + (ch[3] << 0));
344cdf0e10cSrcweir #ifdef HSQLDB_DBG
345cdf0e10cSrcweir         DataLogFile aDataLog( env, name, "data" );
346cdf0e10cSrcweir         aDataLog.write( nRet );
347cdf0e10cSrcweir 
348cdf0e10cSrcweir         aOpLog.logReturn( nRet );
349cdf0e10cSrcweir #endif
350cdf0e10cSrcweir 		return nRet;
351cdf0e10cSrcweir 	}
352cdf0e10cSrcweir 	ThrowException(	env,
353cdf0e10cSrcweir 					"java/io/IOException",
354cdf0e10cSrcweir 					"No InputStream");
355cdf0e10cSrcweir 	return -1;
356cdf0e10cSrcweir }
357cdf0e10cSrcweir // -----------------------------------------------------------------------------
358cdf0e10cSrcweir 
359cdf0e10cSrcweir /*
360cdf0e10cSrcweir  * Class:     com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
361cdf0e10cSrcweir  * Method:    seek
362cdf0e10cSrcweir  * Signature: (Ljava/lang/String;Ljava/lang/String;J)V
363cdf0e10cSrcweir  */
Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_seek(JNIEnv * env,jobject,jstring name,jstring key,jlong position)364cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_seek
365cdf0e10cSrcweir   (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key, jlong position)
366cdf0e10cSrcweir {
367cdf0e10cSrcweir #ifdef HSQLDB_DBG
368cdf0e10cSrcweir     OperationLogFile aOpLog( env, name, "data" );
369cdf0e10cSrcweir     aOpLog.logOperation( "seek", position );
370cdf0e10cSrcweir #endif
371cdf0e10cSrcweir 
372cdf0e10cSrcweir     ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
373cdf0e10cSrcweir 	Reference< XSeekable> xSeek = pHelper.get() ? pHelper->getSeek() : Reference< XSeekable>();
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 	OSL_ENSURE(xSeek.is(),"No Seekable stream!");
376cdf0e10cSrcweir 	if ( xSeek.is() )
377cdf0e10cSrcweir 	{
378cdf0e10cSrcweir     #ifdef HSQLDB_DBG
379cdf0e10cSrcweir         DataLogFile aDataLog( env, name, "data" );
380cdf0e10cSrcweir     #endif
381cdf0e10cSrcweir 
382cdf0e10cSrcweir 		::sal_Int64 nLen = xSeek->getLength();
383cdf0e10cSrcweir 		if ( nLen < position)
384cdf0e10cSrcweir 		{
385cdf0e10cSrcweir 			static ::sal_Int64 BUFFER_SIZE = 9192;
386cdf0e10cSrcweir         #ifdef HSQLDB_DBG
387cdf0e10cSrcweir             aDataLog.seek( nLen );
388cdf0e10cSrcweir         #endif
389cdf0e10cSrcweir 			xSeek->seek(nLen);
390cdf0e10cSrcweir 			Reference< XOutputStream> xOut = pHelper->getOutputStream();
391cdf0e10cSrcweir 			OSL_ENSURE(xOut.is(),"No output stream!");
392cdf0e10cSrcweir 
393cdf0e10cSrcweir 			::sal_Int64 diff = position - nLen;
394cdf0e10cSrcweir 			sal_Int32 n;
395cdf0e10cSrcweir 			while( diff != 0 )
396cdf0e10cSrcweir 			{
397cdf0e10cSrcweir 				if ( BUFFER_SIZE < diff )
398cdf0e10cSrcweir 				{
399cdf0e10cSrcweir 					n = static_cast<sal_Int32>(BUFFER_SIZE);
400cdf0e10cSrcweir 					diff = diff - BUFFER_SIZE;
401cdf0e10cSrcweir 				}
402cdf0e10cSrcweir 				else
403cdf0e10cSrcweir 				{
404cdf0e10cSrcweir 					n = static_cast<sal_Int32>(diff);
405cdf0e10cSrcweir 					diff = 0;
406cdf0e10cSrcweir 				}
407cdf0e10cSrcweir 				Sequence< ::sal_Int8 > aData(n);
408cdf0e10cSrcweir 				memset(aData.getArray(),0,n);
409cdf0e10cSrcweir 				xOut->writeBytes(aData);
410cdf0e10cSrcweir             #ifdef HSQLDB_DBG
411cdf0e10cSrcweir                 aDataLog.write( aData.getConstArray(), n );
412cdf0e10cSrcweir             #endif
413cdf0e10cSrcweir 			}
414cdf0e10cSrcweir 		}
415cdf0e10cSrcweir         xSeek->seek(position);
416cdf0e10cSrcweir         OSL_ENSURE(xSeek->getPosition() == position,"Wrong position after seeking the stream");
417cdf0e10cSrcweir 
418cdf0e10cSrcweir     #ifdef HSQLDB_DBG
419cdf0e10cSrcweir         aDataLog.seek( position );
420cdf0e10cSrcweir         OSL_ENSURE( xSeek->getPosition() == aDataLog.tell(), "Wrong position after seeking the stream" );
421cdf0e10cSrcweir     #endif
422cdf0e10cSrcweir 	}
423cdf0e10cSrcweir }
424cdf0e10cSrcweir // -----------------------------------------------------------------------------
425cdf0e10cSrcweir 
write_to_storage_stream_from_buffer(JNIEnv * env,jobject,jstring name,jstring key,jbyteArray buffer,jint off,jint len,DataLogFile * logger)426cdf0e10cSrcweir void write_to_storage_stream_from_buffer( JNIEnv* env, jobject /*obj_this*/, jstring name, jstring key, jbyteArray buffer, jint off, jint len, DataLogFile* logger )
427cdf0e10cSrcweir {
428cdf0e10cSrcweir     OSL_UNUSED( logger );
429cdf0e10cSrcweir     ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
430cdf0e10cSrcweir 	Reference< XOutputStream> xOut = pHelper.get() ? pHelper->getOutputStream() : Reference< XOutputStream>();
431cdf0e10cSrcweir 	OSL_ENSURE(xOut.is(),"Stream is NULL");
432cdf0e10cSrcweir 
433cdf0e10cSrcweir 	try
434cdf0e10cSrcweir 	{
435cdf0e10cSrcweir 		if ( xOut.is() )
436cdf0e10cSrcweir 		{
437cdf0e10cSrcweir 			jbyte *buf = env->GetByteArrayElements(buffer,NULL);
438cdf0e10cSrcweir 			if (JNI_FALSE != env->ExceptionCheck())
439cdf0e10cSrcweir             {
440cdf0e10cSrcweir                 env->ExceptionClear();
441cdf0e10cSrcweir 				OSL_ENSURE(0,"ExceptionClear");
442cdf0e10cSrcweir             }
443cdf0e10cSrcweir 			OSL_ENSURE(buf,"buf is NULL");
444cdf0e10cSrcweir 			if ( buf && len > 0 && len <= env->GetArrayLength(buffer))
445cdf0e10cSrcweir 			{
446cdf0e10cSrcweir 				Sequence< ::sal_Int8 > aData(buf + off,len);
447cdf0e10cSrcweir                 env->ReleaseByteArrayElements(buffer, buf, JNI_ABORT);
448cdf0e10cSrcweir 				xOut->writeBytes(aData);
449cdf0e10cSrcweir #ifdef HSQLDB_DBG
450cdf0e10cSrcweir                 if ( logger )
451cdf0e10cSrcweir                     logger->write( aData.getConstArray(), len );
452cdf0e10cSrcweir #endif
453cdf0e10cSrcweir 			}
454cdf0e10cSrcweir 		}
455cdf0e10cSrcweir 		else
456cdf0e10cSrcweir 		{
457cdf0e10cSrcweir 			ThrowException(	env,
458cdf0e10cSrcweir 					"java/io/IOException",
459cdf0e10cSrcweir 					"No OutputStream");
460cdf0e10cSrcweir 		}
461cdf0e10cSrcweir 	}
462cdf0e10cSrcweir 	catch(Exception& e)
463cdf0e10cSrcweir 	{
464cdf0e10cSrcweir 		OSL_ENSURE(0,"Exception caught! : write [BII)V");
465cdf0e10cSrcweir         StorageContainer::throwJavaException(e,env);
466cdf0e10cSrcweir 	}
467cdf0e10cSrcweir }
468cdf0e10cSrcweir 
469cdf0e10cSrcweir // -----------------------------------------------------------------------------
470cdf0e10cSrcweir 
471cdf0e10cSrcweir /*
472cdf0e10cSrcweir  * Class:     com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
473cdf0e10cSrcweir  * Method:    write
474cdf0e10cSrcweir  * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)V
475cdf0e10cSrcweir  */
Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_write(JNIEnv * env,jobject obj_this,jstring name,jstring key,jbyteArray buffer,jint off,jint len)476cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_write
477cdf0e10cSrcweir   (JNIEnv * env, jobject obj_this,jstring name, jstring key, jbyteArray buffer, jint off, jint len)
478cdf0e10cSrcweir {
479cdf0e10cSrcweir #ifdef HSQLDB_DBG
480cdf0e10cSrcweir     OperationLogFile aOpLog( env, name, "data" );
481cdf0e10cSrcweir     aOpLog.logOperation( "write( byte[], int, int )" );
482cdf0e10cSrcweir 
483cdf0e10cSrcweir     DataLogFile aDataLog( env, name, "data" );
484cdf0e10cSrcweir     write_to_storage_stream_from_buffer( env, obj_this, name, key, buffer, off, len, &aDataLog );
485cdf0e10cSrcweir #else
486cdf0e10cSrcweir     write_to_storage_stream_from_buffer( env, obj_this, name, key, buffer, off, len );
487cdf0e10cSrcweir #endif
488cdf0e10cSrcweir }
489cdf0e10cSrcweir // -----------------------------------------------------------------------------
490cdf0e10cSrcweir 
write_to_storage_stream(JNIEnv * env,jobject,jstring name,jstring key,jint v,DataLogFile * logger)491cdf0e10cSrcweir void write_to_storage_stream( JNIEnv* env, jobject /*obj_this*/, jstring name, jstring key, jint v, DataLogFile* logger )
492cdf0e10cSrcweir {
493cdf0e10cSrcweir     OSL_UNUSED( logger );
494cdf0e10cSrcweir 
495cdf0e10cSrcweir 	::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
496cdf0e10cSrcweir 	Reference< XOutputStream> xOut = pHelper.get() ? pHelper->getOutputStream() : Reference< XOutputStream>();
497cdf0e10cSrcweir 	OSL_ENSURE(xOut.is(),"Stream is NULL");
498cdf0e10cSrcweir 	try
499cdf0e10cSrcweir 	{
500cdf0e10cSrcweir 		if ( xOut.is() )
501cdf0e10cSrcweir 		{
502cdf0e10cSrcweir 			Sequence< ::sal_Int8 > oneByte(4);
503cdf0e10cSrcweir 			oneByte[0] = (sal_Int8) ((v >> 24) & 0xFF);
504cdf0e10cSrcweir 			oneByte[1] = (sal_Int8) ((v >> 16) & 0xFF);
505cdf0e10cSrcweir 			oneByte[2] = (sal_Int8) ((v >>  8) & 0xFF);
506cdf0e10cSrcweir 			oneByte[3] = (sal_Int8) ((v >>  0) & 0xFF);
507cdf0e10cSrcweir 
508cdf0e10cSrcweir 			xOut->writeBytes(oneByte);
509cdf0e10cSrcweir #ifdef HSQLDB_DBG
510cdf0e10cSrcweir             if ( logger )
511cdf0e10cSrcweir                 logger->write( oneByte.getConstArray(), 4 );
512cdf0e10cSrcweir #endif
513cdf0e10cSrcweir 		}
514cdf0e10cSrcweir 		else
515cdf0e10cSrcweir 		{
516cdf0e10cSrcweir 			ThrowException(	env,
517cdf0e10cSrcweir 					"java/io/IOException",
518cdf0e10cSrcweir 					"No OutputStream");
519cdf0e10cSrcweir 		}
520cdf0e10cSrcweir 	}
521cdf0e10cSrcweir 	catch(Exception& e)
522cdf0e10cSrcweir 	{
523cdf0e10cSrcweir 		OSL_ENSURE(0,"Exception catched! : writeBytes(aData);");
524cdf0e10cSrcweir         StorageContainer::throwJavaException(e,env);
525cdf0e10cSrcweir 	}
526cdf0e10cSrcweir }
527cdf0e10cSrcweir 
528cdf0e10cSrcweir // -----------------------------------------------------------------------------
529cdf0e10cSrcweir 
530cdf0e10cSrcweir /*
531cdf0e10cSrcweir  * Class:     com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
532cdf0e10cSrcweir  * Method:    writeInt
533cdf0e10cSrcweir  * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
534cdf0e10cSrcweir  */
Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_writeInt(JNIEnv * env,jobject obj_this,jstring name,jstring key,jint v)535cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_writeInt
536cdf0e10cSrcweir   (JNIEnv * env, jobject obj_this,jstring name, jstring key, jint v)
537cdf0e10cSrcweir {
538cdf0e10cSrcweir #ifdef HSQLDB_DBG
539cdf0e10cSrcweir     OperationLogFile aOpLog( env, name, "data" );
540cdf0e10cSrcweir     aOpLog.logOperation( "writeInt" );
541cdf0e10cSrcweir 
542cdf0e10cSrcweir     DataLogFile aDataLog( env, name, "data" );
543cdf0e10cSrcweir     write_to_storage_stream( env, obj_this, name, key, v, &aDataLog );
544cdf0e10cSrcweir #else
545cdf0e10cSrcweir     write_to_storage_stream( env, obj_this, name, key, v );
546cdf0e10cSrcweir #endif
547cdf0e10cSrcweir }
548