bump product version to 7.2.5.1
[LibreOffice.git] / connectivity / source / drivers / hsqldb / HStorageAccess.cxx
blob4e939ad586035da772a5e0ed3e229fdbdc6e125f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
27 #include <string.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
41 * Method: openStream
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)
47 #ifdef HSQLDB_DBG
49 OperationLogFile( env, name, "data" ).logOperation( "openStream" );
50 LogFile( env, name, "data" ).create();
52 #endif
54 StorageContainer::registerStream(env,name,key,mode);
58 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
59 * Method: close
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)
65 #ifdef HSQLDB_DBG
67 OUString sKey = StorageContainer::jstring2ustring(env,key);
68 OUString sName = StorageContainer::jstring2ustring(env,name);
70 #endif
71 std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
72 Reference< XOutputStream> xFlush = pHelper ? pHelper->getOutputStream() : Reference< XOutputStream>();
73 if ( xFlush.is() )
74 try
76 xFlush->flush();
78 catch(const Exception&)
80 TOOLS_WARN_EXCEPTION( "connectivity.hsqldb", "NativeStorageAccess::close: caught an exception while flushing!" );
82 #ifdef HSQLDB_DBG
84 OperationLogFile aOpLog( env, name, "data" );
85 aOpLog.logOperation( "close" );
86 aOpLog.close();
88 LogFile aDataLog( env, name, "data" );
89 aDataLog.close();
91 #endif
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)
104 #ifdef HSQLDB_DBG
105 OperationLogFile aOpLog( env, name, "data" );
106 aOpLog.logOperation( "getFilePointer" );
107 #endif
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);
113 #ifdef HSQLDB_DBG
114 aOpLog.logReturn( nReturn );
115 #endif
116 return nReturn;
121 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
122 * Method: length
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)
128 #ifdef HSQLDB_DBG
129 OperationLogFile aOpLog( env, name, "data" );
130 aOpLog.logOperation( "length" );
131 #endif
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);
137 #ifdef HSQLDB_DBG
138 aOpLog.logReturn( nReturn );
139 #endif
140 return 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!");
149 if ( xIn.is() )
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);
160 return -1;
163 if (nBytesRead <= 0)
165 return -1;
167 else
169 return static_cast<unsigned char>(aData[0]);
172 return -1;
177 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
178 * Method: read
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)
184 #ifdef HSQLDB_DBG
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 );
190 #else
191 return read_from_storage_stream( env, name, key );
192 #endif
196 jint read_from_storage_stream_into_buffer( JNIEnv * env, jstring name, jstring key, jbyteArray buffer, jint off, jint len )
198 #ifdef HSQLDB_DBG
200 OUString sKey = StorageContainer::jstring2ustring(env,key);
201 OUString sName = StorageContainer::jstring2ustring(env,name);
203 #endif
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!");
207 if ( xIn.is() )
209 jsize nLen = env->GetArrayLength(buffer);
210 if ( nLen < len || len <= 0 )
212 ThrowException( env,
213 "java/io/IOException",
214 "len is greater or equal to the buffer size");
215 return -1;
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);
227 return -1;
230 if (nBytesRead <= 0)
231 return -1;
232 env->SetByteArrayRegion(buffer,off,nBytesRead,reinterpret_cast<jbyte*>(&aData[0]));
234 return nBytesRead;
236 ThrowException( env,
237 "java/io/IOException",
238 "Stream is not valid");
239 return -1;
244 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
245 * Method: read
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)
251 #ifdef HSQLDB_DBG
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 );
257 #else
258 (void)obj_this;
259 return read_from_storage_stream_into_buffer( env, name, key, buffer, off, len );
260 #endif
265 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
266 * Method: readInt
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)
272 #ifdef HSQLDB_DBG
273 OperationLogFile aOpLog( env, name, "data" );
274 aOpLog.logOperation( "readInt" );
275 #endif
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!");
280 if ( xIn.is() )
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);
291 return -1;
294 if ( nBytesRead != 4 ) {
295 ThrowException( env,
296 "java/io/IOException",
297 "Bytes read != 4");
298 return -1;
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)
309 ThrowException( env,
310 "java/io/IOException",
311 "One byte is < 0");
312 return -1;
314 jint nRet = (ch[0] << 24) + (ch[1] << 16) + (ch[2] << 8) + (ch[3] << 0);
315 #ifdef HSQLDB_DBG
316 DataLogFile aDataLog( env, name, "data" );
317 aDataLog.write( nRet );
319 aOpLog.logReturn( nRet );
320 #endif
321 return nRet;
323 ThrowException( env,
324 "java/io/IOException",
325 "No InputStream");
326 return -1;
331 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
332 * Method: seek
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)
338 #ifdef HSQLDB_DBG
339 OperationLogFile aOpLog( env, name, "data" );
340 aOpLog.logOperation( "seek", position );
341 #endif
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!");
347 if ( !xSeek.is() )
348 return;
350 #ifdef HSQLDB_DBG
351 DataLogFile aDataLog( env, name, "data" );
352 #endif
354 ::sal_Int64 nLen = xSeek->getLength();
355 if ( nLen < position)
357 static const ::sal_Int64 BUFFER_SIZE = 9192;
358 #ifdef HSQLDB_DBG
359 aDataLog.seek( nLen );
360 #endif
361 xSeek->seek(nLen);
362 Reference< XOutputStream> xOut = pHelper->getOutputStream();
363 OSL_ENSURE(xOut.is(),"No output stream!");
365 ::sal_Int64 diff = position - nLen;
366 sal_Int32 n;
367 while( diff != 0 )
369 if ( BUFFER_SIZE < diff )
371 n = static_cast<sal_Int32>(BUFFER_SIZE);
372 diff = diff - BUFFER_SIZE;
374 else
376 n = static_cast<sal_Int32>(diff);
377 diff = 0;
379 Sequence< ::sal_Int8 > aData(n);
380 memset(aData.getArray(),0,n);
381 xOut->writeBytes(aData);
382 #ifdef HSQLDB_DBG
383 aDataLog.write( aData.getConstArray(), n );
384 #endif
387 xSeek->seek(position);
388 OSL_ENSURE(xSeek->getPosition() == position,"Wrong position after seeking the stream");
390 #ifdef HSQLDB_DBG
391 aDataLog.seek( position );
392 OSL_ENSURE( xSeek->getPosition() == aDataLog.tell(), "Wrong position after seeking the stream" );
393 #endif
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");
405 if ( xOut.is() )
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);
421 else
423 ThrowException( env,
424 "java/io/IOException",
425 "No OutputStream");
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
438 * Method: write
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)
444 #ifdef HSQLDB_DBG
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 );
450 #else
451 (void)obj_this;
452 write_to_storage_stream_from_buffer( env, name, key, buffer, off, len );
453 #endif
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");
464 if ( xOut.is() )
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);
476 else
478 ThrowException( env,
479 "java/io/IOException",
480 "No OutputStream");
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
493 * Method: writeInt
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)
499 #ifdef HSQLDB_DBG
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 );
505 #else
506 (void)obj_this;
507 write_to_storage_stream( env, name, key, v );
508 #endif
511 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */