Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / hsqldb / HStorageAccess.cxx
blob8416f41abaa272c033b4092e6698107a14eeb19b
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 <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"
30 #include <string.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
44 * Method: openStream
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)
50 #ifdef HSQLDB_DBG
52 OperationLogFile( env, name, "data" ).logOperation( "openStream" );
53 LogFile( env, name, "data" ).create();
55 #endif
57 StorageContainer::registerStream(env,name,key,mode);
61 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
62 * Method: close
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)
68 #ifdef HSQLDB_DBG
70 OUString sKey = StorageContainer::jstring2ustring(env,key);
71 OUString sName = StorageContainer::jstring2ustring(env,name);
73 #endif
74 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
75 Reference< XOutputStream> xFlush = pHelper.get() ? pHelper->getOutputStream() : Reference< XOutputStream>();
76 if ( xFlush.is() )
77 try
79 xFlush->flush();
81 catch(const Exception&)
83 OSL_FAIL( "NativeStorageAccess::close: caught an exception while flushing!" );
85 #ifdef HSQLDB_DBG
87 OperationLogFile aOpLog( env, name, "data" );
88 aOpLog.logOperation( "close" );
89 aOpLog.close();
91 LogFile aDataLog( env, name, "data" );
92 aDataLog.close();
94 #endif
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)
107 #ifdef HSQLDB_DBG
108 OperationLogFile aOpLog( env, name, "data" );
109 aOpLog.logOperation( "getFilePointer" );
110 #endif
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);
116 #ifdef HSQLDB_DBG
117 aOpLog.logReturn( nReturn );
118 #endif
119 return nReturn;
124 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
125 * Method: length
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)
131 #ifdef HSQLDB_DBG
132 OperationLogFile aOpLog( env, name, "data" );
133 aOpLog.logOperation( "length" );
134 #endif
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);
140 #ifdef HSQLDB_DBG
141 aOpLog.logReturn( nReturn );
142 #endif
143 return 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!");
154 if ( xIn.is() )
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);
165 return -1;
168 if (nBytesRead <= 0)
170 return (-1);
172 else
174 sal_Int32 tmpInt = aData[0];
175 if (tmpInt < 0 )
176 tmpInt = 256 +tmpInt;
178 #ifdef HSQLDB_DBG
179 if ( logger )
180 logger->write( tmpInt );
181 #endif
182 return(tmpInt);
185 return -1;
191 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
192 * Method: read
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)
198 #ifdef HSQLDB_DBG
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 );
204 #else
205 return read_from_storage_stream( env, obj_this, name, key );
206 #endif
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 );
214 #ifdef HSQLDB_DBG
216 OUString sKey = StorageContainer::jstring2ustring(env,key);
217 OUString sName = StorageContainer::jstring2ustring(env,name);
219 #endif
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!");
223 if ( xIn.is() )
225 jsize nLen = env->GetArrayLength(buffer);
226 if ( nLen < len || len <= 0 )
228 ThrowException( env,
229 "java/io/IOException",
230 "len is greater or equal to the buffer size");
231 return -1;
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);
243 return -1;
246 if (nBytesRead <= 0)
247 return -1;
248 env->SetByteArrayRegion(buffer,off,nBytesRead,(jbyte*) &aData[0]);
250 #ifdef HSQLDB_DBG
251 if ( logger )
252 logger->write( aData.getConstArray(), nBytesRead );
253 #endif
254 return nBytesRead;
256 ThrowException( env,
257 "java/io/IOException",
258 "Stream is not valid");
259 return -1;
264 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
265 * Method: read
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)
271 #ifdef HSQLDB_DBG
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 );
277 #else
278 return read_from_storage_stream_into_buffer( env, obj_this, name, key, buffer, off, len );
279 #endif
285 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
286 * Method: readInt
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)
292 #ifdef HSQLDB_DBG
293 OperationLogFile aOpLog( env, name, "data" );
294 aOpLog.logOperation( "readInt" );
295 #endif
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!");
300 if ( xIn.is() )
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);
311 return -1;
314 if ( nBytesRead != 4 ) {
315 ThrowException( env,
316 "java/io/IOException",
317 "Bytes read != 4");
318 return -1;
321 Sequence< sal_Int32 > ch(4);
322 for(sal_Int32 i = 0;i < 4; ++i)
324 ch[i] = aData[i];
325 if (ch[i] < 0 )
326 ch[i] = 256 + ch[i];
329 if ((ch[0] | ch[1] | ch[2] | ch[3]) < 0)
331 ThrowException( env,
332 "java/io/IOException",
333 "One byte is < 0");
334 return -1;
336 jint nRet = ((ch[0] << 24) + (ch[1] << 16) + (ch[2] << 8) + (ch[3] << 0));
337 #ifdef HSQLDB_DBG
338 DataLogFile aDataLog( env, name, "data" );
339 aDataLog.write( nRet );
341 aOpLog.logReturn( nRet );
342 #endif
343 return nRet;
345 ThrowException( env,
346 "java/io/IOException",
347 "No InputStream");
348 return -1;
353 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
354 * Method: seek
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)
360 #ifdef HSQLDB_DBG
361 OperationLogFile aOpLog( env, name, "data" );
362 aOpLog.logOperation( "seek", position );
363 #endif
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!");
369 if ( xSeek.is() )
371 #ifdef HSQLDB_DBG
372 DataLogFile aDataLog( env, name, "data" );
373 #endif
375 ::sal_Int64 nLen = xSeek->getLength();
376 if ( nLen < position)
378 static ::sal_Int64 BUFFER_SIZE = 9192;
379 #ifdef HSQLDB_DBG
380 aDataLog.seek( nLen );
381 #endif
382 xSeek->seek(nLen);
383 Reference< XOutputStream> xOut = pHelper->getOutputStream();
384 OSL_ENSURE(xOut.is(),"No output stream!");
386 ::sal_Int64 diff = position - nLen;
387 sal_Int32 n;
388 while( diff != 0 )
390 if ( BUFFER_SIZE < diff )
392 n = static_cast<sal_Int32>(BUFFER_SIZE);
393 diff = diff - BUFFER_SIZE;
395 else
397 n = static_cast<sal_Int32>(diff);
398 diff = 0;
400 Sequence< ::sal_Int8 > aData(n);
401 memset(aData.getArray(),0,n);
402 xOut->writeBytes(aData);
403 #ifdef HSQLDB_DBG
404 aDataLog.write( aData.getConstArray(), n );
405 #endif
408 xSeek->seek(position);
409 OSL_ENSURE(xSeek->getPosition() == position,"Wrong position after seeking the stream");
411 #ifdef HSQLDB_DBG
412 aDataLog.seek( position );
413 OSL_ENSURE( xSeek->getPosition() == aDataLog.tell(), "Wrong position after seeking the stream" );
414 #endif
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");
428 if ( xOut.is() )
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);
442 #ifdef HSQLDB_DBG
443 if ( logger )
444 logger->write( aData.getConstArray(), len );
445 #endif
448 else
450 ThrowException( env,
451 "java/io/IOException",
452 "No OutputStream");
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
466 * Method: write
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)
472 #ifdef HSQLDB_DBG
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 );
478 #else
479 write_to_storage_stream_from_buffer( env, obj_this, name, key, buffer, off, len );
480 #endif
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");
493 if ( xOut.is() )
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);
502 #ifdef HSQLDB_DBG
503 if ( logger )
504 logger->write( oneByte.getConstArray(), 4 );
505 #endif
507 else
509 ThrowException( env,
510 "java/io/IOException",
511 "No OutputStream");
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
525 * Method: writeInt
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)
531 #ifdef HSQLDB_DBG
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 );
537 #else
538 write_to_storage_stream( env, obj_this, name, key, v );
539 #endif
542 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */