Update ooo320-m1
[ooovba.git] / connectivity / source / drivers / hsqldb / HStorageAccess.cxx
blobdf209ff418367b03c523f63de7f6e7850b333701
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: HStorageAccess.cxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "hsqldb/HStorageAccess.hxx"
34 #include <comphelper/processfactory.hxx>
35 #include <com/sun/star/embed/XStorage.hpp>
36 #include <com/sun/star/embed/ElementModes.hpp>
37 #include <com/sun/star/io/XStream.hpp>
38 #include "hsqldb/HStorageMap.hxx"
39 #include "hsqldb/StorageNativeInputStream.h"
40 #include "accesslog.hxx"
41 #include "diagnose_ex.h"
43 #include <string.h>
45 using namespace ::com::sun::star::container;
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::embed;
48 using namespace ::com::sun::star::io;
49 using namespace ::com::sun::star::lang;
50 using namespace ::connectivity::hsqldb;
52 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
53 #define ThrowException(env, type, msg) { \
54 env->ThrowNew(env->FindClass(type), msg); }
57 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
58 * Method: openStream
59 * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
61 SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_openStream
62 (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key, jint mode)
64 #ifdef HSQLDB_DBG
66 OperationLogFile( env, name, "data" ).logOperation( "openStream" );
67 LogFile( env, name, "data" ).create();
69 #endif
71 StorageContainer::registerStream(env,name,key,mode);
73 // -----------------------------------------------------------------------------
75 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
76 * Method: close
77 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
79 SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_close
80 (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key)
82 #ifdef HSQLDB_DBG
84 ::rtl::OUString sKey = StorageContainer::jstring2ustring(env,key);
85 ::rtl::OUString sName = StorageContainer::jstring2ustring(env,name);
87 #endif
88 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
89 Reference< XOutputStream> xFlush = pHelper.get() ? pHelper->getOutputStream() : Reference< XOutputStream>();
90 if ( xFlush.is() )
91 try
93 xFlush->flush();
95 catch(Exception&)
97 OSL_ENSURE( false, "NativeStorageAccess::close: caught an exception while flushing!" );
99 #ifdef HSQLDB_DBG
101 OperationLogFile aOpLog( env, name, "data" );
102 aOpLog.logOperation( "close" );
103 aOpLog.close();
105 LogFile aDataLog( env, name, "data" );
106 aDataLog.close();
108 #endif
110 StorageContainer::revokeStream(env,name,key);
112 // -----------------------------------------------------------------------------
114 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
115 * Method: getFilePointer
116 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
118 SAL_DLLPUBLIC_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_getFilePointer
119 (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key)
121 #ifdef HSQLDB_DBG
122 OperationLogFile aOpLog( env, name, "data" );
123 aOpLog.logOperation( "getFilePointer" );
124 #endif
126 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
127 OSL_ENSURE(pHelper.get(),"No stream helper!");
129 jlong nReturn = pHelper.get() ? pHelper->getSeek()->getPosition() : jlong(0);
130 #ifdef HSQLDB_DBG
131 aOpLog.logReturn( nReturn );
132 #endif
133 return nReturn;
135 // -----------------------------------------------------------------------------
138 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
139 * Method: length
140 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
142 SAL_DLLPUBLIC_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_length
143 (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key)
145 #ifdef HSQLDB_DBG
146 OperationLogFile aOpLog( env, name, "data" );
147 aOpLog.logOperation( "length" );
148 #endif
150 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
151 OSL_ENSURE(pHelper.get(),"No stream helper!");
153 jlong nReturn = pHelper.get() ? pHelper->getSeek()->getLength() :jlong(0);
154 #ifdef HSQLDB_DBG
155 aOpLog.logReturn( nReturn );
156 #endif
157 return nReturn;
160 // -----------------------------------------------------------------------------
162 jint read_from_storage_stream( JNIEnv * env, jobject /*obj_this*/, jstring name, jstring key, DataLogFile* logger )
164 OSL_UNUSED( logger );
165 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
166 Reference< XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference< XInputStream>();
167 OSL_ENSURE(xIn.is(),"Input stream is NULL!");
168 if ( xIn.is() )
170 Sequence< ::sal_Int8 > aData(1);
171 sal_Int32 nBytesRead = -1;
174 nBytesRead = xIn->readBytes(aData,1);
176 catch(Exception& e)
178 StorageContainer::throwJavaException(e,env);
179 return -1;
182 if (nBytesRead <= 0)
184 return (-1);
186 else
188 sal_Int32 tmpInt = aData[0];
189 if (tmpInt < 0 )
190 tmpInt = 256 +tmpInt;
192 #ifdef HSQLDB_DBG
193 if ( logger )
194 logger->write( tmpInt );
195 #endif
196 return(tmpInt);
199 return -1;
202 // -----------------------------------------------------------------------------
205 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
206 * Method: read
207 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
209 SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_read__Ljava_lang_String_2Ljava_lang_String_2
210 (JNIEnv* env, jobject obj_this, jstring name, jstring key)
212 #ifdef HSQLDB_DBG
213 OperationLogFile aOpLog( env, name, "data" );
214 aOpLog.logOperation( "read" );
216 DataLogFile aDataLog( env, name, "data" );
217 return read_from_storage_stream( env, obj_this, name, key, &aDataLog );
218 #else
219 return read_from_storage_stream( env, obj_this, name, key );
220 #endif
223 // -----------------------------------------------------------------------------
225 jint read_from_storage_stream_into_buffer( JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key, jbyteArray buffer, jint off, jint len, DataLogFile* logger )
227 OSL_UNUSED( logger );
228 #ifdef HSQLDB_DBG
230 ::rtl::OUString sKey = StorageContainer::jstring2ustring(env,key);
231 ::rtl::OUString sName = StorageContainer::jstring2ustring(env,name);
233 #endif
234 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
235 Reference< XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference< XInputStream>();
236 OSL_ENSURE(xIn.is(),"Input stream is NULL!");
237 if ( xIn.is() )
239 jsize nLen = env->GetArrayLength(buffer);
240 if ( nLen < len )
242 ThrowException( env,
243 "java/io/IOException",
244 "len is greater or equal to the buffer size");
245 return -1;
247 sal_Int32 nBytesRead = -1;
249 Sequence< ::sal_Int8 > aData(nLen);
252 nBytesRead = xIn->readBytes(aData, len);
254 catch(Exception& e)
256 StorageContainer::throwJavaException(e,env);
257 return -1;
260 if (nBytesRead <= 0)
261 return -1;
262 env->SetByteArrayRegion(buffer,off,nBytesRead,&aData[0]);
264 #ifdef HSQLDB_DBG
265 if ( logger )
266 logger->write( aData.getConstArray(), nBytesRead );
267 #endif
268 return nBytesRead;
270 ThrowException( env,
271 "java/io/IOException",
272 "Stream is not valid");
273 return -1;
275 // -----------------------------------------------------------------------------
278 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
279 * Method: read
280 * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)I
282 SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_read__Ljava_lang_String_2Ljava_lang_String_2_3BII
283 (JNIEnv * env, jobject obj_this,jstring name, jstring key, jbyteArray buffer, jint off, jint len)
285 #ifdef HSQLDB_DBG
286 OperationLogFile aOpLog( env, name, "data" );
287 aOpLog.logOperation( "read( byte[], int, int )" );
289 DataLogFile aDataLog( env, name, "data" );
290 return read_from_storage_stream_into_buffer( env, obj_this, name, key, buffer, off, len, &aDataLog );
291 #else
292 return read_from_storage_stream_into_buffer( env, obj_this, name, key, buffer, off, len );
293 #endif
296 // -----------------------------------------------------------------------------
299 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
300 * Method: readInt
301 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
303 SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_readInt
304 (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key)
306 #ifdef HSQLDB_DBG
307 OperationLogFile aOpLog( env, name, "data" );
308 aOpLog.logOperation( "readInt" );
309 #endif
311 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
312 Reference< XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference< XInputStream>();
313 OSL_ENSURE(xIn.is(),"Input stream is NULL!");
314 if ( xIn.is() )
316 Sequence< ::sal_Int8 > aData(4);
317 sal_Int32 nBytesRead = -1;
320 nBytesRead = xIn->readBytes(aData, 4);
322 catch(Exception& e)
324 StorageContainer::throwJavaException(e,env);
325 return -1;
328 if ( nBytesRead != 4 ) {
329 ThrowException( env,
330 "java/io/IOException",
331 "Bytes read != 4");
332 return -1;
335 Sequence< sal_Int32 > ch(4);
336 for(sal_Int32 i = 0;i < 4; ++i)
338 ch[i] = aData[i];
339 if (ch[i] < 0 )
340 ch[i] = 256 + ch[i];
343 if ((ch[0] | ch[1] | ch[2] | ch[3]) < 0)
345 ThrowException( env,
346 "java/io/IOException",
347 "One byte is < 0");
348 return -1;
350 jint nRet = ((ch[0] << 24) + (ch[1] << 16) + (ch[2] << 8) + (ch[3] << 0));
351 #ifdef HSQLDB_DBG
352 DataLogFile aDataLog( env, name, "data" );
353 aDataLog.write( nRet );
355 aOpLog.logReturn( nRet );
356 #endif
357 return nRet;
359 ThrowException( env,
360 "java/io/IOException",
361 "No InputStream");
362 return -1;
364 // -----------------------------------------------------------------------------
367 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
368 * Method: seek
369 * Signature: (Ljava/lang/String;Ljava/lang/String;J)V
371 SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_seek
372 (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key, jlong position)
374 #ifdef HSQLDB_DBG
375 OperationLogFile aOpLog( env, name, "data" );
376 aOpLog.logOperation( "seek", position );
377 #endif
379 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
380 Reference< XSeekable> xSeek = pHelper.get() ? pHelper->getSeek() : Reference< XSeekable>();
382 OSL_ENSURE(xSeek.is(),"No Seekable stream!");
383 if ( xSeek.is() )
385 #ifdef HSQLDB_DBG
386 DataLogFile aDataLog( env, name, "data" );
387 #endif
389 ::sal_Int64 nLen = xSeek->getLength();
390 if ( nLen < position)
392 static ::sal_Int64 BUFFER_SIZE = 9192;
393 #ifdef HSQLDB_DBG
394 aDataLog.seek( nLen );
395 #endif
396 xSeek->seek(nLen);
397 Reference< XOutputStream> xOut = pHelper->getOutputStream();
398 OSL_ENSURE(xOut.is(),"No output stream!");
400 ::sal_Int64 diff = position - nLen;
401 sal_Int32 n;
402 while( diff != 0 )
404 if ( BUFFER_SIZE < diff )
406 n = static_cast<sal_Int32>(BUFFER_SIZE);
407 diff = diff - BUFFER_SIZE;
409 else
411 n = static_cast<sal_Int32>(diff);
412 diff = 0;
414 Sequence< ::sal_Int8 > aData(n);
415 memset(aData.getArray(),0,n);
416 xOut->writeBytes(aData);
417 #ifdef HSQLDB_DBG
418 aDataLog.write( aData.getConstArray(), n );
419 #endif
422 xSeek->seek(position);
423 OSL_ENSURE(xSeek->getPosition() == position,"Wrong position after seeking the stream");
425 #ifdef HSQLDB_DBG
426 aDataLog.seek( position );
427 OSL_ENSURE( xSeek->getPosition() == aDataLog.tell(), "Wrong position after seeking the stream" );
428 #endif
431 // -----------------------------------------------------------------------------
433 void write_to_storage_stream_from_buffer( JNIEnv* env, jobject /*obj_this*/, jstring name, jstring key, jbyteArray buffer, jint off, jint len, DataLogFile* logger )
435 OSL_UNUSED( logger );
436 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
437 Reference< XOutputStream> xOut = pHelper.get() ? pHelper->getOutputStream() : Reference< XOutputStream>();
438 OSL_ENSURE(xOut.is(),"Stream is NULL");
442 if ( xOut.is() )
444 jbyte *buf = env->GetByteArrayElements(buffer,NULL);
445 #ifdef HSQLDB_DBG
446 OSL_ENSURE(len <= env->GetArrayLength(buffer),"Length is greater than the buffer!");
447 #endif
449 if (JNI_FALSE != env->ExceptionCheck())
451 env->ExceptionClear();
452 OSL_ENSURE(0,"ExceptionClear");
454 OSL_ENSURE(buf,"buf is NULL");
455 if ( buf )
457 Sequence< ::sal_Int8 > aData(buf + off,len);
458 xOut->writeBytes(aData);
459 env->ReleaseByteArrayElements(buffer, buf, JNI_ABORT);
460 #ifdef HSQLDB_DBG
461 if ( logger )
462 logger->write( aData.getConstArray(), len );
463 #endif
466 else
468 ThrowException( env,
469 "java/io/IOException",
470 "No OutputStream");
473 catch(Exception& e)
475 OSL_ENSURE(0,"Exception caught! : write [BII)V");
476 StorageContainer::throwJavaException(e,env);
480 // -----------------------------------------------------------------------------
483 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
484 * Method: write
485 * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)V
487 SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_write
488 (JNIEnv * env, jobject obj_this,jstring name, jstring key, jbyteArray buffer, jint off, jint len)
490 #ifdef HSQLDB_DBG
491 OperationLogFile aOpLog( env, name, "data" );
492 aOpLog.logOperation( "write( byte[], int, int )" );
494 DataLogFile aDataLog( env, name, "data" );
495 write_to_storage_stream_from_buffer( env, obj_this, name, key, buffer, off, len, &aDataLog );
496 #else
497 write_to_storage_stream_from_buffer( env, obj_this, name, key, buffer, off, len );
498 #endif
500 // -----------------------------------------------------------------------------
502 void write_to_storage_stream( JNIEnv* env, jobject /*obj_this*/, jstring name, jstring key, jint v, DataLogFile* logger )
504 OSL_UNUSED( logger );
506 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
507 Reference< XOutputStream> xOut = pHelper.get() ? pHelper->getOutputStream() : Reference< XOutputStream>();
508 OSL_ENSURE(xOut.is(),"Stream is NULL");
511 if ( xOut.is() )
513 Sequence< ::sal_Int8 > oneByte(4);
514 oneByte[0] = (sal_Int8) ((v >> 24) & 0xFF);
515 oneByte[1] = (sal_Int8) ((v >> 16) & 0xFF);
516 oneByte[2] = (sal_Int8) ((v >> 8) & 0xFF);
517 oneByte[3] = (sal_Int8) ((v >> 0) & 0xFF);
519 xOut->writeBytes(oneByte);
520 #ifdef HSQLDB_DBG
521 if ( logger )
522 logger->write( oneByte.getConstArray(), 4 );
523 #endif
525 else
527 ThrowException( env,
528 "java/io/IOException",
529 "No OutputStream");
532 catch(Exception& e)
534 OSL_ENSURE(0,"Exception catched! : writeBytes(aData);");
535 StorageContainer::throwJavaException(e,env);
539 // -----------------------------------------------------------------------------
542 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
543 * Method: writeInt
544 * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
546 SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_writeInt
547 (JNIEnv * env, jobject obj_this,jstring name, jstring key, jint v)
549 #ifdef HSQLDB_DBG
550 OperationLogFile aOpLog( env, name, "data" );
551 aOpLog.logOperation( "writeInt" );
553 DataLogFile aDataLog( env, name, "data" );
554 write_to_storage_stream( env, obj_this, name, key, v, &aDataLog );
555 #else
556 write_to_storage_stream( env, obj_this, name, key, v );
557 #endif