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 <com/sun/star/embed/XStorage.hpp>
22 #include <hsqldb/HStorageMap.hxx>
23 #include "accesslog.hxx"
24 #include <osl/diagnose.h>
25 #include <tools/diagnose_ex.h>
29 using namespace ::com::sun::star::container
;
30 using namespace ::com::sun::star::uno
;
31 using namespace ::com::sun::star::embed
;
32 using namespace ::com::sun::star::io
;
33 using namespace ::com::sun::star::lang
;
34 using namespace ::connectivity::hsqldb
;
36 #define ThrowException(env, type, msg) { \
37 env->ThrowNew(env->FindClass(type), msg); }
40 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
42 * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
44 extern "C" SAL_JNI_EXPORT
void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_openStream
45 (JNIEnv
* env
, jobject
/*obj_this*/,jstring name
, jstring key
, jint mode
)
49 OperationLogFile( env
, name
, "data" ).logOperation( "openStream" );
50 LogFile( env
, name
, "data" ).create();
54 StorageContainer::registerStream(env
,name
,key
,mode
);
58 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
60 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
62 extern "C" SAL_JNI_EXPORT
void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_close
63 (JNIEnv
* env
, jobject
/*obj_this*/,jstring name
, jstring key
)
67 OUString sKey
= StorageContainer::jstring2ustring(env
,key
);
68 OUString sName
= StorageContainer::jstring2ustring(env
,name
);
71 std::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
72 Reference
< XOutputStream
> xFlush
= pHelper
? pHelper
->getOutputStream() : Reference
< XOutputStream
>();
78 catch(const Exception
&)
80 TOOLS_WARN_EXCEPTION( "connectivity.hsqldb", "NativeStorageAccess::close: caught an exception while flushing!" );
84 OperationLogFile
aOpLog( env
, name
, "data" );
85 aOpLog
.logOperation( "close" );
88 LogFile
aDataLog( env
, name
, "data" );
93 StorageContainer::revokeStream(env
,name
,key
);
97 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
98 * Method: getFilePointer
99 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
101 extern "C" SAL_JNI_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_getFilePointer
102 (JNIEnv
* env
, jobject
/*obj_this*/,jstring name
, jstring key
)
105 OperationLogFile
aOpLog( env
, name
, "data" );
106 aOpLog
.logOperation( "getFilePointer" );
109 std::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
110 OSL_ENSURE(pHelper
,"No stream helper!");
112 jlong nReturn
= pHelper
? pHelper
->getSeek()->getPosition() : jlong(0);
114 aOpLog
.logReturn( nReturn
);
121 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
123 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
125 extern "C" SAL_JNI_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_length
126 (JNIEnv
* env
, jobject
/*obj_this*/,jstring name
, jstring key
)
129 OperationLogFile
aOpLog( env
, name
, "data" );
130 aOpLog
.logOperation( "length" );
133 std::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
134 OSL_ENSURE(pHelper
,"No stream helper!");
136 jlong nReturn
= pHelper
? pHelper
->getSeek()->getLength() :jlong(0);
138 aOpLog
.logReturn( nReturn
);
144 jint
read_from_storage_stream( JNIEnv
* env
, jstring name
, jstring key
)
146 std::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
147 Reference
< XInputStream
> xIn
= pHelper
? pHelper
->getInputStream() : Reference
< XInputStream
>();
148 OSL_ENSURE(xIn
.is(),"Input stream is NULL!");
151 Sequence
< ::sal_Int8
> aData(1);
152 sal_Int32 nBytesRead
= -1;
155 nBytesRead
= xIn
->readBytes(aData
,1);
157 catch(const Exception
& e
)
159 StorageContainer::throwJavaException(e
,env
);
169 return static_cast<unsigned char>(aData
[0]);
177 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
179 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
181 extern "C" SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_read__Ljava_lang_String_2Ljava_lang_String_2
182 (JNIEnv
* env
, jobject
/*obj_this*/, jstring name
, jstring key
)
185 OperationLogFile
aOpLog( env
, name
, "data" );
186 aOpLog
.logOperation( "read" );
188 DataLogFile
aDataLog( env
, name
, "data" );
189 return read_from_storage_stream( env
, obj_this
, name
, key
, &aDataLog
);
191 return read_from_storage_stream( env
, name
, key
);
196 jint
read_from_storage_stream_into_buffer( JNIEnv
* env
, jstring name
, jstring key
, jbyteArray buffer
, jint off
, jint len
)
200 OUString sKey
= StorageContainer::jstring2ustring(env
,key
);
201 OUString sName
= StorageContainer::jstring2ustring(env
,name
);
204 std::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
205 Reference
< XInputStream
> xIn
= pHelper
? pHelper
->getInputStream() : Reference
< XInputStream
>();
206 OSL_ENSURE(xIn
.is(),"Input stream is NULL!");
209 jsize nLen
= env
->GetArrayLength(buffer
);
210 if ( nLen
< len
|| len
<= 0 )
213 "java/io/IOException",
214 "len is greater or equal to the buffer size");
217 sal_Int32 nBytesRead
= -1;
219 Sequence
< ::sal_Int8
> aData(nLen
);
222 nBytesRead
= xIn
->readBytes(aData
, len
);
224 catch(const Exception
& e
)
226 StorageContainer::throwJavaException(e
,env
);
232 env
->SetByteArrayRegion(buffer
,off
,nBytesRead
,reinterpret_cast<jbyte
*>(&aData
[0]));
237 "java/io/IOException",
238 "Stream is not valid");
244 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
246 * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)I
248 extern "C" SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_read__Ljava_lang_String_2Ljava_lang_String_2_3BII
249 (JNIEnv
* env
, jobject obj_this
,jstring name
, jstring key
, jbyteArray buffer
, jint off
, jint len
)
252 OperationLogFile
aOpLog( env
, name
, "data" );
253 aOpLog
.logOperation( "read( byte[], int, int )" );
255 DataLogFile
aDataLog( env
, name
, "data" );
256 return read_from_storage_stream_into_buffer( env
, obj_this
, name
, key
, buffer
, off
, len
, &aDataLog
);
259 return read_from_storage_stream_into_buffer( env
, name
, key
, buffer
, off
, len
);
265 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
267 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
269 extern "C" SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_readInt
270 (JNIEnv
* env
, jobject
/*obj_this*/,jstring name
, jstring key
)
273 OperationLogFile
aOpLog( env
, name
, "data" );
274 aOpLog
.logOperation( "readInt" );
277 std::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
278 Reference
< XInputStream
> xIn
= pHelper
? pHelper
->getInputStream() : Reference
< XInputStream
>();
279 OSL_ENSURE(xIn
.is(),"Input stream is NULL!");
282 Sequence
< ::sal_Int8
> aData(4);
283 sal_Int32 nBytesRead
= -1;
286 nBytesRead
= xIn
->readBytes(aData
, 4);
288 catch(const Exception
& e
)
290 StorageContainer::throwJavaException(e
,env
);
294 if ( nBytesRead
!= 4 ) {
296 "java/io/IOException",
301 Sequence
< sal_Int32
> ch(4);
302 for(sal_Int32 i
= 0;i
< 4; ++i
)
304 ch
[i
] = static_cast<unsigned char>(aData
[i
]);
307 if ((ch
[0] | ch
[1] | ch
[2] | ch
[3]) < 0)
310 "java/io/IOException",
314 jint nRet
= (ch
[0] << 24) + (ch
[1] << 16) + (ch
[2] << 8) + (ch
[3] << 0);
316 DataLogFile
aDataLog( env
, name
, "data" );
317 aDataLog
.write( nRet
);
319 aOpLog
.logReturn( nRet
);
324 "java/io/IOException",
331 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
333 * Signature: (Ljava/lang/String;Ljava/lang/String;J)V
335 extern "C" SAL_JNI_EXPORT
void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_seek
336 (JNIEnv
* env
, jobject
/*obj_this*/,jstring name
, jstring key
, jlong position
)
339 OperationLogFile
aOpLog( env
, name
, "data" );
340 aOpLog
.logOperation( "seek", position
);
343 std::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
344 Reference
< XSeekable
> xSeek
= pHelper
? pHelper
->getSeek() : Reference
< XSeekable
>();
346 OSL_ENSURE(xSeek
.is(),"No Seekable stream!");
351 DataLogFile
aDataLog( env
, name
, "data" );
354 ::sal_Int64 nLen
= xSeek
->getLength();
355 if ( nLen
< position
)
357 static const ::sal_Int64 BUFFER_SIZE
= 9192;
359 aDataLog
.seek( nLen
);
362 Reference
< XOutputStream
> xOut
= pHelper
->getOutputStream();
363 OSL_ENSURE(xOut
.is(),"No output stream!");
365 ::sal_Int64 diff
= position
- nLen
;
369 if ( BUFFER_SIZE
< diff
)
371 n
= static_cast<sal_Int32
>(BUFFER_SIZE
);
372 diff
= diff
- BUFFER_SIZE
;
376 n
= static_cast<sal_Int32
>(diff
);
379 Sequence
< ::sal_Int8
> aData(n
);
380 memset(aData
.getArray(),0,n
);
381 xOut
->writeBytes(aData
);
383 aDataLog
.write( aData
.getConstArray(), n
);
387 xSeek
->seek(position
);
388 OSL_ENSURE(xSeek
->getPosition() == position
,"Wrong position after seeking the stream");
391 aDataLog
.seek( position
);
392 OSL_ENSURE( xSeek
->getPosition() == aDataLog
.tell(), "Wrong position after seeking the stream" );
397 void write_to_storage_stream_from_buffer( JNIEnv
* env
, jstring name
, jstring key
, jbyteArray buffer
, jint off
, jint len
)
399 std::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
400 Reference
< XOutputStream
> xOut
= pHelper
? pHelper
->getOutputStream() : Reference
< XOutputStream
>();
401 OSL_ENSURE(xOut
.is(),"Stream is NULL");
407 jbyte
*buf
= env
->GetByteArrayElements(buffer
,nullptr);
408 if (env
->ExceptionCheck())
410 env
->ExceptionClear();
411 OSL_FAIL("ExceptionClear");
413 OSL_ENSURE(buf
,"buf is NULL");
414 if ( buf
&& len
> 0 && len
<= env
->GetArrayLength(buffer
))
416 Sequence
< ::sal_Int8
> aData(reinterpret_cast<sal_Int8
*>(buf
+ off
),len
);
417 env
->ReleaseByteArrayElements(buffer
, buf
, JNI_ABORT
);
418 xOut
->writeBytes(aData
);
424 "java/io/IOException",
428 catch(const Exception
& e
)
430 TOOLS_WARN_EXCEPTION( "connectivity.hsqldb", "Exception caught! : write [BII)V");
431 StorageContainer::throwJavaException(e
,env
);
437 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
439 * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)V
441 extern "C" SAL_JNI_EXPORT
void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_write
442 (JNIEnv
* env
, jobject obj_this
,jstring name
, jstring key
, jbyteArray buffer
, jint off
, jint len
)
445 OperationLogFile
aOpLog( env
, name
, "data" );
446 aOpLog
.logOperation( "write( byte[], int, int )" );
448 DataLogFile
aDataLog( env
, name
, "data" );
449 write_to_storage_stream_from_buffer( env
, obj_this
, name
, key
, buffer
, off
, len
, &aDataLog
);
452 write_to_storage_stream_from_buffer( env
, name
, key
, buffer
, off
, len
);
457 void write_to_storage_stream( JNIEnv
* env
, jstring name
, jstring key
, jint v
)
459 std::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
460 Reference
< XOutputStream
> xOut
= pHelper
? pHelper
->getOutputStream() : Reference
< XOutputStream
>();
461 OSL_ENSURE(xOut
.is(),"Stream is NULL");
466 Sequence
< ::sal_Int8
> oneByte
468 static_cast<sal_Int8
>((v
>> 24) & 0xFF),
469 static_cast<sal_Int8
>((v
>> 16) & 0xFF),
470 static_cast<sal_Int8
>((v
>> 8) & 0xFF),
471 static_cast<sal_Int8
>((v
>> 0) & 0xFF)
474 xOut
->writeBytes(oneByte
);
479 "java/io/IOException",
483 catch(const Exception
& e
)
485 TOOLS_WARN_EXCEPTION( "connectivity.hsqldb", "writeBytes(aData);");
486 StorageContainer::throwJavaException(e
,env
);
492 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
494 * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
496 extern "C" SAL_JNI_EXPORT
void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_writeInt
497 (JNIEnv
* env
, jobject obj_this
,jstring name
, jstring key
, jint v
)
500 OperationLogFile
aOpLog( env
, name
, "data" );
501 aOpLog
.logOperation( "writeInt" );
503 DataLogFile
aDataLog( env
, name
, "data" );
504 write_to_storage_stream( env
, name
, key
, v
, &aDataLog
);
507 write_to_storage_stream( env
, name
, key
, v
);
511 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */