1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: HStorageAccess.cxx,v $
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"
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
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
)
66 OperationLogFile( env
, name
, "data" ).logOperation( "openStream" );
67 LogFile( env
, name
, "data" ).create();
71 StorageContainer::registerStream(env
,name
,key
,mode
);
73 // -----------------------------------------------------------------------------
75 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
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
)
84 ::rtl::OUString sKey
= StorageContainer::jstring2ustring(env
,key
);
85 ::rtl::OUString sName
= StorageContainer::jstring2ustring(env
,name
);
88 ::boost::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
89 Reference
< XOutputStream
> xFlush
= pHelper
.get() ? pHelper
->getOutputStream() : Reference
< XOutputStream
>();
97 OSL_ENSURE( false, "NativeStorageAccess::close: caught an exception while flushing!" );
101 OperationLogFile
aOpLog( env
, name
, "data" );
102 aOpLog
.logOperation( "close" );
105 LogFile
aDataLog( env
, name
, "data" );
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
)
122 OperationLogFile
aOpLog( env
, name
, "data" );
123 aOpLog
.logOperation( "getFilePointer" );
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);
131 aOpLog
.logReturn( nReturn
);
135 // -----------------------------------------------------------------------------
138 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
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
)
146 OperationLogFile
aOpLog( env
, name
, "data" );
147 aOpLog
.logOperation( "length" );
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);
155 aOpLog
.logReturn( 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!");
170 Sequence
< ::sal_Int8
> aData(1);
171 sal_Int32 nBytesRead
= -1;
174 nBytesRead
= xIn
->readBytes(aData
,1);
178 StorageContainer::throwJavaException(e
,env
);
188 sal_Int32 tmpInt
= aData
[0];
190 tmpInt
= 256 +tmpInt
;
194 logger
->write( tmpInt
);
202 // -----------------------------------------------------------------------------
205 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
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
)
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
);
219 return read_from_storage_stream( env
, obj_this
, name
, key
);
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
);
230 ::rtl::OUString sKey
= StorageContainer::jstring2ustring(env
,key
);
231 ::rtl::OUString sName
= StorageContainer::jstring2ustring(env
,name
);
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!");
239 jsize nLen
= env
->GetArrayLength(buffer
);
243 "java/io/IOException",
244 "len is greater or equal to the buffer size");
247 sal_Int32 nBytesRead
= -1;
249 Sequence
< ::sal_Int8
> aData(nLen
);
252 nBytesRead
= xIn
->readBytes(aData
, len
);
256 StorageContainer::throwJavaException(e
,env
);
262 env
->SetByteArrayRegion(buffer
,off
,nBytesRead
,&aData
[0]);
266 logger
->write( aData
.getConstArray(), nBytesRead
);
271 "java/io/IOException",
272 "Stream is not valid");
275 // -----------------------------------------------------------------------------
278 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
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
)
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
);
292 return read_from_storage_stream_into_buffer( env
, obj_this
, name
, key
, buffer
, off
, len
);
296 // -----------------------------------------------------------------------------
299 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
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
)
307 OperationLogFile
aOpLog( env
, name
, "data" );
308 aOpLog
.logOperation( "readInt" );
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!");
316 Sequence
< ::sal_Int8
> aData(4);
317 sal_Int32 nBytesRead
= -1;
320 nBytesRead
= xIn
->readBytes(aData
, 4);
324 StorageContainer::throwJavaException(e
,env
);
328 if ( nBytesRead
!= 4 ) {
330 "java/io/IOException",
335 Sequence
< sal_Int32
> ch(4);
336 for(sal_Int32 i
= 0;i
< 4; ++i
)
343 if ((ch
[0] | ch
[1] | ch
[2] | ch
[3]) < 0)
346 "java/io/IOException",
350 jint nRet
= ((ch
[0] << 24) + (ch
[1] << 16) + (ch
[2] << 8) + (ch
[3] << 0));
352 DataLogFile
aDataLog( env
, name
, "data" );
353 aDataLog
.write( nRet
);
355 aOpLog
.logReturn( nRet
);
360 "java/io/IOException",
364 // -----------------------------------------------------------------------------
367 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
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
)
375 OperationLogFile
aOpLog( env
, name
, "data" );
376 aOpLog
.logOperation( "seek", position
);
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!");
386 DataLogFile
aDataLog( env
, name
, "data" );
389 ::sal_Int64 nLen
= xSeek
->getLength();
390 if ( nLen
< position
)
392 static ::sal_Int64 BUFFER_SIZE
= 9192;
394 aDataLog
.seek( nLen
);
397 Reference
< XOutputStream
> xOut
= pHelper
->getOutputStream();
398 OSL_ENSURE(xOut
.is(),"No output stream!");
400 ::sal_Int64 diff
= position
- nLen
;
404 if ( BUFFER_SIZE
< diff
)
406 n
= static_cast<sal_Int32
>(BUFFER_SIZE
);
407 diff
= diff
- BUFFER_SIZE
;
411 n
= static_cast<sal_Int32
>(diff
);
414 Sequence
< ::sal_Int8
> aData(n
);
415 memset(aData
.getArray(),0,n
);
416 xOut
->writeBytes(aData
);
418 aDataLog
.write( aData
.getConstArray(), n
);
422 xSeek
->seek(position
);
423 OSL_ENSURE(xSeek
->getPosition() == position
,"Wrong position after seeking the stream");
426 aDataLog
.seek( position
);
427 OSL_ENSURE( xSeek
->getPosition() == aDataLog
.tell(), "Wrong position after seeking the stream" );
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");
444 jbyte
*buf
= env
->GetByteArrayElements(buffer
,NULL
);
446 OSL_ENSURE(len
<= env
->GetArrayLength(buffer
),"Length is greater than the buffer!");
449 if (JNI_FALSE
!= env
->ExceptionCheck())
451 env
->ExceptionClear();
452 OSL_ENSURE(0,"ExceptionClear");
454 OSL_ENSURE(buf
,"buf is NULL");
457 Sequence
< ::sal_Int8
> aData(buf
+ off
,len
);
458 xOut
->writeBytes(aData
);
459 env
->ReleaseByteArrayElements(buffer
, buf
, JNI_ABORT
);
462 logger
->write( aData
.getConstArray(), len
);
469 "java/io/IOException",
475 OSL_ENSURE(0,"Exception caught! : write [BII)V");
476 StorageContainer::throwJavaException(e
,env
);
480 // -----------------------------------------------------------------------------
483 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
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
)
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
);
497 write_to_storage_stream_from_buffer( env
, obj_this
, name
, key
, buffer
, off
, len
);
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");
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
);
522 logger
->write( oneByte
.getConstArray(), 4 );
528 "java/io/IOException",
534 OSL_ENSURE(0,"Exception catched! : writeBytes(aData);");
535 StorageContainer::throwJavaException(e
,env
);
539 // -----------------------------------------------------------------------------
542 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
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
)
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
);
556 write_to_storage_stream( env
, obj_this
, name
, key
, v
);