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