1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "hsqldb/HStorageAccess.hxx"
21 #include <comphelper/processfactory.hxx>
22 #include <com/sun/star/embed/XStorage.hpp>
23 #include <com/sun/star/embed/ElementModes.hpp>
24 #include <com/sun/star/io/XStream.hpp>
25 #include "hsqldb/HStorageMap.hxx"
26 #include "hsqldb/StorageNativeInputStream.h"
27 #include "accesslog.hxx"
28 #include "diagnose_ex.h"
32 using namespace ::com::sun::star::container
;
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::embed
;
35 using namespace ::com::sun::star::io
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::connectivity::hsqldb
;
39 #define ThrowException(env, type, msg) { \
40 env->ThrowNew(env->FindClass(type), msg); }
43 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
45 * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
47 SAL_JNI_EXPORT
void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_openStream
48 (JNIEnv
* env
, jobject
/*obj_this*/,jstring name
, jstring key
, jint mode
)
52 OperationLogFile( env
, name
, "data" ).logOperation( "openStream" );
53 LogFile( env
, name
, "data" ).create();
57 StorageContainer::registerStream(env
,name
,key
,mode
);
61 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
63 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
65 SAL_JNI_EXPORT
void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_close
66 (JNIEnv
* env
, jobject
/*obj_this*/,jstring name
, jstring key
)
70 OUString sKey
= StorageContainer::jstring2ustring(env
,key
);
71 OUString sName
= StorageContainer::jstring2ustring(env
,name
);
74 ::boost::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
75 Reference
< XOutputStream
> xFlush
= pHelper
.get() ? pHelper
->getOutputStream() : Reference
< XOutputStream
>();
81 catch(const Exception
&)
83 OSL_FAIL( "NativeStorageAccess::close: caught an exception while flushing!" );
87 OperationLogFile
aOpLog( env
, name
, "data" );
88 aOpLog
.logOperation( "close" );
91 LogFile
aDataLog( env
, name
, "data" );
96 StorageContainer::revokeStream(env
,name
,key
);
100 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
101 * Method: getFilePointer
102 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
104 SAL_JNI_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_getFilePointer
105 (JNIEnv
* env
, jobject
/*obj_this*/,jstring name
, jstring key
)
108 OperationLogFile
aOpLog( env
, name
, "data" );
109 aOpLog
.logOperation( "getFilePointer" );
112 ::boost::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
113 OSL_ENSURE(pHelper
.get(),"No stream helper!");
115 jlong nReturn
= pHelper
.get() ? pHelper
->getSeek()->getPosition() : jlong(0);
117 aOpLog
.logReturn( nReturn
);
124 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
126 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
128 SAL_JNI_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_length
129 (JNIEnv
* env
, jobject
/*obj_this*/,jstring name
, jstring key
)
132 OperationLogFile
aOpLog( env
, name
, "data" );
133 aOpLog
.logOperation( "length" );
136 ::boost::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
137 OSL_ENSURE(pHelper
.get(),"No stream helper!");
139 jlong nReturn
= pHelper
.get() ? pHelper
->getSeek()->getLength() :jlong(0);
141 aOpLog
.logReturn( nReturn
);
148 jint
read_from_storage_stream( JNIEnv
* env
, jobject
/*obj_this*/, jstring name
, jstring key
, DataLogFile
* logger
)
150 OSL_UNUSED( logger
);
151 ::boost::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
152 Reference
< XInputStream
> xIn
= pHelper
.get() ? pHelper
->getInputStream() : Reference
< XInputStream
>();
153 OSL_ENSURE(xIn
.is(),"Input stream is NULL!");
156 Sequence
< ::sal_Int8
> aData(1);
157 sal_Int32 nBytesRead
= -1;
160 nBytesRead
= xIn
->readBytes(aData
,1);
162 catch(const Exception
& e
)
164 StorageContainer::throwJavaException(e
,env
);
174 sal_Int32 tmpInt
= aData
[0];
176 tmpInt
= 256 +tmpInt
;
180 logger
->write( tmpInt
);
191 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
193 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
195 SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_read__Ljava_lang_String_2Ljava_lang_String_2
196 (JNIEnv
* env
, jobject obj_this
, jstring name
, jstring key
)
199 OperationLogFile
aOpLog( env
, name
, "data" );
200 aOpLog
.logOperation( "read" );
202 DataLogFile
aDataLog( env
, name
, "data" );
203 return read_from_storage_stream( env
, obj_this
, name
, key
, &aDataLog
);
205 return read_from_storage_stream( env
, obj_this
, name
, key
);
211 jint
read_from_storage_stream_into_buffer( JNIEnv
* env
, jobject
/*obj_this*/,jstring name
, jstring key
, jbyteArray buffer
, jint off
, jint len
, DataLogFile
* logger
)
213 OSL_UNUSED( logger
);
216 OUString sKey
= StorageContainer::jstring2ustring(env
,key
);
217 OUString sName
= StorageContainer::jstring2ustring(env
,name
);
220 ::boost::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
221 Reference
< XInputStream
> xIn
= pHelper
.get() ? pHelper
->getInputStream() : Reference
< XInputStream
>();
222 OSL_ENSURE(xIn
.is(),"Input stream is NULL!");
225 jsize nLen
= env
->GetArrayLength(buffer
);
226 if ( nLen
< len
|| len
<= 0 )
229 "java/io/IOException",
230 "len is greater or equal to the buffer size");
233 sal_Int32 nBytesRead
= -1;
235 Sequence
< ::sal_Int8
> aData(nLen
);
238 nBytesRead
= xIn
->readBytes(aData
, len
);
240 catch(const Exception
& e
)
242 StorageContainer::throwJavaException(e
,env
);
248 env
->SetByteArrayRegion(buffer
,off
,nBytesRead
,(jbyte
*) &aData
[0]);
252 logger
->write( aData
.getConstArray(), nBytesRead
);
257 "java/io/IOException",
258 "Stream is not valid");
264 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
266 * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)I
268 SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_read__Ljava_lang_String_2Ljava_lang_String_2_3BII
269 (JNIEnv
* env
, jobject obj_this
,jstring name
, jstring key
, jbyteArray buffer
, jint off
, jint len
)
272 OperationLogFile
aOpLog( env
, name
, "data" );
273 aOpLog
.logOperation( "read( byte[], int, int )" );
275 DataLogFile
aDataLog( env
, name
, "data" );
276 return read_from_storage_stream_into_buffer( env
, obj_this
, name
, key
, buffer
, off
, len
, &aDataLog
);
278 return read_from_storage_stream_into_buffer( env
, obj_this
, name
, key
, buffer
, off
, len
);
285 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
287 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
289 SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_readInt
290 (JNIEnv
* env
, jobject
/*obj_this*/,jstring name
, jstring key
)
293 OperationLogFile
aOpLog( env
, name
, "data" );
294 aOpLog
.logOperation( "readInt" );
297 ::boost::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
298 Reference
< XInputStream
> xIn
= pHelper
.get() ? pHelper
->getInputStream() : Reference
< XInputStream
>();
299 OSL_ENSURE(xIn
.is(),"Input stream is NULL!");
302 Sequence
< ::sal_Int8
> aData(4);
303 sal_Int32 nBytesRead
= -1;
306 nBytesRead
= xIn
->readBytes(aData
, 4);
308 catch(const Exception
& e
)
310 StorageContainer::throwJavaException(e
,env
);
314 if ( nBytesRead
!= 4 ) {
316 "java/io/IOException",
321 Sequence
< sal_Int32
> ch(4);
322 for(sal_Int32 i
= 0;i
< 4; ++i
)
329 if ((ch
[0] | ch
[1] | ch
[2] | ch
[3]) < 0)
332 "java/io/IOException",
336 jint nRet
= ((ch
[0] << 24) + (ch
[1] << 16) + (ch
[2] << 8) + (ch
[3] << 0));
338 DataLogFile
aDataLog( env
, name
, "data" );
339 aDataLog
.write( nRet
);
341 aOpLog
.logReturn( nRet
);
346 "java/io/IOException",
353 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
355 * Signature: (Ljava/lang/String;Ljava/lang/String;J)V
357 SAL_JNI_EXPORT
void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_seek
358 (JNIEnv
* env
, jobject
/*obj_this*/,jstring name
, jstring key
, jlong position
)
361 OperationLogFile
aOpLog( env
, name
, "data" );
362 aOpLog
.logOperation( "seek", position
);
365 ::boost::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
366 Reference
< XSeekable
> xSeek
= pHelper
.get() ? pHelper
->getSeek() : Reference
< XSeekable
>();
368 OSL_ENSURE(xSeek
.is(),"No Seekable stream!");
372 DataLogFile
aDataLog( env
, name
, "data" );
375 ::sal_Int64 nLen
= xSeek
->getLength();
376 if ( nLen
< position
)
378 static ::sal_Int64 BUFFER_SIZE
= 9192;
380 aDataLog
.seek( nLen
);
383 Reference
< XOutputStream
> xOut
= pHelper
->getOutputStream();
384 OSL_ENSURE(xOut
.is(),"No output stream!");
386 ::sal_Int64 diff
= position
- nLen
;
390 if ( BUFFER_SIZE
< diff
)
392 n
= static_cast<sal_Int32
>(BUFFER_SIZE
);
393 diff
= diff
- BUFFER_SIZE
;
397 n
= static_cast<sal_Int32
>(diff
);
400 Sequence
< ::sal_Int8
> aData(n
);
401 memset(aData
.getArray(),0,n
);
402 xOut
->writeBytes(aData
);
404 aDataLog
.write( aData
.getConstArray(), n
);
408 xSeek
->seek(position
);
409 OSL_ENSURE(xSeek
->getPosition() == position
,"Wrong position after seeking the stream");
412 aDataLog
.seek( position
);
413 OSL_ENSURE( xSeek
->getPosition() == aDataLog
.tell(), "Wrong position after seeking the stream" );
419 void write_to_storage_stream_from_buffer( JNIEnv
* env
, jobject
/*obj_this*/, jstring name
, jstring key
, jbyteArray buffer
, jint off
, jint len
, DataLogFile
* logger
)
421 OSL_UNUSED( logger
);
422 ::boost::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
423 Reference
< XOutputStream
> xOut
= pHelper
.get() ? pHelper
->getOutputStream() : Reference
< XOutputStream
>();
424 OSL_ENSURE(xOut
.is(),"Stream is NULL");
430 jbyte
*buf
= env
->GetByteArrayElements(buffer
,NULL
);
431 if (JNI_FALSE
!= env
->ExceptionCheck())
433 env
->ExceptionClear();
434 OSL_FAIL("ExceptionClear");
436 OSL_ENSURE(buf
,"buf is NULL");
437 if ( buf
&& len
> 0 && len
<= env
->GetArrayLength(buffer
))
439 Sequence
< ::sal_Int8
> aData((const signed char*) buf
+ off
,len
);
440 env
->ReleaseByteArrayElements(buffer
, buf
, JNI_ABORT
);
441 xOut
->writeBytes(aData
);
444 logger
->write( aData
.getConstArray(), len
);
451 "java/io/IOException",
455 catch(const Exception
& e
)
457 OSL_FAIL("Exception caught! : write [BII)V");
458 StorageContainer::throwJavaException(e
,env
);
465 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
467 * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)V
469 SAL_JNI_EXPORT
void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_write
470 (JNIEnv
* env
, jobject obj_this
,jstring name
, jstring key
, jbyteArray buffer
, jint off
, jint len
)
473 OperationLogFile
aOpLog( env
, name
, "data" );
474 aOpLog
.logOperation( "write( byte[], int, int )" );
476 DataLogFile
aDataLog( env
, name
, "data" );
477 write_to_storage_stream_from_buffer( env
, obj_this
, name
, key
, buffer
, off
, len
, &aDataLog
);
479 write_to_storage_stream_from_buffer( env
, obj_this
, name
, key
, buffer
, off
, len
);
484 void write_to_storage_stream( JNIEnv
* env
, jobject
/*obj_this*/, jstring name
, jstring key
, jint v
, DataLogFile
* logger
)
486 OSL_UNUSED( logger
);
488 ::boost::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
489 Reference
< XOutputStream
> xOut
= pHelper
.get() ? pHelper
->getOutputStream() : Reference
< XOutputStream
>();
490 OSL_ENSURE(xOut
.is(),"Stream is NULL");
495 Sequence
< ::sal_Int8
> oneByte(4);
496 oneByte
[0] = (sal_Int8
) ((v
>> 24) & 0xFF);
497 oneByte
[1] = (sal_Int8
) ((v
>> 16) & 0xFF);
498 oneByte
[2] = (sal_Int8
) ((v
>> 8) & 0xFF);
499 oneByte
[3] = (sal_Int8
) ((v
>> 0) & 0xFF);
501 xOut
->writeBytes(oneByte
);
504 logger
->write( oneByte
.getConstArray(), 4 );
510 "java/io/IOException",
514 catch(const Exception
& e
)
516 OSL_FAIL("Exception caught! : writeBytes(aData);");
517 StorageContainer::throwJavaException(e
,env
);
524 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
526 * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
528 SAL_JNI_EXPORT
void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_writeInt
529 (JNIEnv
* env
, jobject obj_this
,jstring name
, jstring key
, jint v
)
532 OperationLogFile
aOpLog( env
, name
, "data" );
533 aOpLog
.logOperation( "writeInt" );
535 DataLogFile
aDataLog( env
, name
, "data" );
536 write_to_storage_stream( env
, obj_this
, name
, key
, v
, &aDataLog
);
538 write_to_storage_stream( env
, obj_this
, name
, key
, v
);
542 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */