LibreOffice Module connectivity (master) 1
HStorageAccess.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21#include <com/sun/star/embed/XStorage.hpp>
23#include "accesslog.hxx"
24#include <osl/diagnose.h>
26
27#include <string.h>
28
29#include <algorithm>
30
31using namespace ::com::sun::star::container;
32using namespace ::com::sun::star::uno;
33using namespace ::com::sun::star::embed;
34using namespace ::com::sun::star::io;
35using namespace ::com::sun::star::lang;
36using namespace ::connectivity::hsqldb;
37
38#define ThrowException(env, type, msg) { \
39 env->ThrowNew(env->FindClass(type), msg); }
40
41/*
42 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
43 * Method: openStream
44 * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
45 */
47 (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key, jint mode)
48{
49#ifdef HSQLDB_DBG
50 {
51 OperationLogFile( env, name, "data" ).logOperation( "openStream" );
52 LogFile( env, name, "data" ).create();
53 }
54#endif
55
56 StorageContainer::registerStream(env,name,key,mode);
57}
58
59/*
60 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
61 * Method: close
62 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
63 */
65 (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key)
66{
67#ifdef HSQLDB_DBG
68 {
69 OUString sKey = StorageContainer::jstring2ustring(env,key);
70 OUString sName = StorageContainer::jstring2ustring(env,name);
71 }
72#endif
73 std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
74 Reference< XOutputStream> xFlush = pHelper ? pHelper->getOutputStream() : Reference< XOutputStream>();
75 if ( xFlush.is() )
76 try
77 {
78 xFlush->flush();
79 }
80 catch(const Exception&)
81 {
82 TOOLS_WARN_EXCEPTION( "connectivity.hsqldb", "NativeStorageAccess::close: caught an exception while flushing!" );
83 }
84#ifdef HSQLDB_DBG
85 {
86 OperationLogFile aOpLog( env, name, "data" );
87 aOpLog.logOperation( "close" );
88 aOpLog.close();
89
90 LogFile aDataLog( env, name, "data" );
91 aDataLog.close();
92 }
93#endif
94
95 StorageContainer::revokeStream(env,name,key);
96}
97
98/*
99 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
100 * Method: getFilePointer
101 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
102 */
104 (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key)
105{
106#ifdef HSQLDB_DBG
107 OperationLogFile aOpLog( env, name, "data" );
108 aOpLog.logOperation( "getFilePointer" );
109#endif
110
111 std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
112 OSL_ENSURE(pHelper,"No stream helper!");
113
114 jlong nReturn = pHelper ? pHelper->getSeek()->getPosition() : jlong(0);
115#ifdef HSQLDB_DBG
116 aOpLog.logReturn( nReturn );
117#endif
118 return nReturn;
119}
120
121
122/*
123 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
124 * Method: length
125 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
126 */
128 (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key)
129{
130#ifdef HSQLDB_DBG
131 OperationLogFile aOpLog( env, name, "data" );
132 aOpLog.logOperation( "length" );
133#endif
134
135 std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
136 OSL_ENSURE(pHelper,"No stream helper!");
137
138 jlong nReturn = pHelper ? pHelper->getSeek()->getLength() :jlong(0);
139#ifdef HSQLDB_DBG
140 aOpLog.logReturn( nReturn );
141#endif
142 return nReturn;
143}
144
145
146jint read_from_storage_stream( JNIEnv * env, jstring name, jstring key )
147{
148 std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
149 Reference< XInputStream> xIn = pHelper ? pHelper->getInputStream() : Reference< XInputStream>();
150 OSL_ENSURE(xIn.is(),"Input stream is NULL!");
151 if ( !xIn.is() )
152 return -1;
153
154 Sequence< ::sal_Int8 > aData(1);
155 sal_Int32 nBytesRead = -1;
156 try
157 {
158 nBytesRead = xIn->readBytes(aData,1);
159 }
160 catch(const Exception& e)
161 {
162 StorageContainer::throwJavaException(e,env);
163 return -1;
164
165 }
166 if (nBytesRead <= 0)
167 {
168 return -1;
169 }
170 else
171 {
172 return static_cast<unsigned char>(aData[0]);
173 }
174 return -1;
175}
176
177
178/*
179 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
180 * Method: read
181 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
182 */
184 (JNIEnv* env, jobject /*obj_this*/, jstring name, jstring key)
185{
186#ifdef HSQLDB_DBG
187 OperationLogFile aOpLog( env, name, "data" );
188 aOpLog.logOperation( "read" );
189
190 DataLogFile aDataLog( env, name, "data" );
191 return read_from_storage_stream( env, obj_this, name, key, &aDataLog );
192#else
193 return read_from_storage_stream( env, name, key );
194#endif
195}
196
197
198jint read_from_storage_stream_into_buffer( JNIEnv * env, jstring name, jstring key, jbyteArray buffer, jint off, jint len )
199{
200#ifdef HSQLDB_DBG
201 {
202 OUString sKey = StorageContainer::jstring2ustring(env,key);
203 OUString sName = StorageContainer::jstring2ustring(env,name);
204 }
205#endif
206 std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
207 Reference< XInputStream> xIn = pHelper ? pHelper->getInputStream() : Reference< XInputStream>();
208 OSL_ENSURE(xIn.is(),"Input stream is NULL!");
209 if ( xIn.is() )
210 {
211 jsize nLen = env->GetArrayLength(buffer);
212 if ( nLen < len || len <= 0 )
213 {
215 "java/io/IOException",
216 "len is greater or equal to the buffer size");
217 return -1;
218 }
219 sal_Int32 nBytesRead = -1;
220
221 Sequence< ::sal_Int8 > aData(nLen);
222 try
223 {
224 nBytesRead = xIn->readBytes(aData, len);
225 }
226 catch(const Exception& e)
227 {
228 StorageContainer::throwJavaException(e,env);
229 return -1;
230 }
231
232 if (nBytesRead <= 0)
233 return -1;
234 env->SetByteArrayRegion(buffer,off,nBytesRead,reinterpret_cast<const jbyte*>(&aData[0]));
235
236 return nBytesRead;
237 }
239 "java/io/IOException",
240 "Stream is not valid");
241 return -1;
242}
243
244
245/*
246 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
247 * Method: read
248 * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)I
249 */
251 (JNIEnv * env, jobject obj_this,jstring name, jstring key, jbyteArray buffer, jint off, jint len)
252{
253#ifdef HSQLDB_DBG
254 OperationLogFile aOpLog( env, name, "data" );
255 aOpLog.logOperation( "read( byte[], int, int )" );
256
257 DataLogFile aDataLog( env, name, "data" );
258 return read_from_storage_stream_into_buffer( env, obj_this, name, key, buffer, off, len, &aDataLog );
259#else
260 (void)obj_this;
261 return read_from_storage_stream_into_buffer( env, name, key, buffer, off, len );
262#endif
263}
264
265
266/*
267 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
268 * Method: readInt
269 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
270 */
272 (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key)
273{
274#ifdef HSQLDB_DBG
275 OperationLogFile aOpLog( env, name, "data" );
276 aOpLog.logOperation( "readInt" );
277#endif
278
279 std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
280 Reference< XInputStream> xIn = pHelper ? pHelper->getInputStream() : Reference< XInputStream>();
281 OSL_ENSURE(xIn.is(),"Input stream is NULL!");
282 if ( xIn.is() )
283 {
284 Sequence< ::sal_Int8 > aData(4);
285 sal_Int32 nBytesRead = -1;
286 try
287 {
288 nBytesRead = xIn->readBytes(aData, 4);
289 }
290 catch(const Exception& e)
291 {
292 StorageContainer::throwJavaException(e,env);
293 return -1;
294 }
295
296 if ( nBytesRead != 4 ) {
298 "java/io/IOException",
299 "Bytes read != 4");
300 return -1;
301 }
302
303 Sequence< sal_Int32 > ch(4);
304 std::transform(aData.begin(), aData.end(), ch.getArray(),
305 [](auto c) { return static_cast<unsigned char>(c); });
306
307 if ((ch[0] | ch[1] | ch[2] | ch[3]) < 0)
308 {
310 "java/io/IOException",
311 "One byte is < 0");
312 return -1;
313 }
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 );
318
319 aOpLog.logReturn( nRet );
320#endif
321 return nRet;
322 }
324 "java/io/IOException",
325 "No InputStream");
326 return -1;
327}
328
329
330/*
331 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
332 * Method: seek
333 * Signature: (Ljava/lang/String;Ljava/lang/String;J)V
334 */
336 (JNIEnv * env, jobject /*obj_this*/,jstring name, jstring key, jlong position)
337{
338#ifdef HSQLDB_DBG
339 OperationLogFile aOpLog( env, name, "data" );
340 aOpLog.logOperation( "seek", position );
341#endif
342
343 std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
344
345 OSL_ENSURE(pHelper, "No StreamHelper!");
346 if (!pHelper)
347 return;
348
349 Reference< XSeekable> xSeek = pHelper->getSeek();
350
351 OSL_ENSURE(xSeek.is(), "No Seekable stream!");
352 if (!xSeek)
353 return;
354
355#ifdef HSQLDB_DBG
356 DataLogFile aDataLog( env, name, "data" );
357#endif
358
359 ::sal_Int64 nLen = xSeek->getLength();
360 if ( nLen < position)
361 {
362 static const ::sal_Int64 BUFFER_SIZE = 9192;
363 #ifdef HSQLDB_DBG
364 aDataLog.seek( nLen );
365 #endif
366 xSeek->seek(nLen);
367 Reference< XOutputStream> xOut = pHelper->getOutputStream();
368 OSL_ENSURE(xOut.is(),"No output stream!");
369
370 ::sal_Int64 diff = position - nLen;
371 sal_Int32 n;
372 while( diff != 0 )
373 {
374 if ( BUFFER_SIZE < diff )
375 {
376 n = static_cast<sal_Int32>(BUFFER_SIZE);
377 diff = diff - BUFFER_SIZE;
378 }
379 else
380 {
381 n = static_cast<sal_Int32>(diff);
382 diff = 0;
383 }
384 Sequence< ::sal_Int8 > aData(n);
385 memset(aData.getArray(),0,n);
386 xOut->writeBytes(aData);
387 #ifdef HSQLDB_DBG
388 aDataLog.write( aData.getConstArray(), n );
389 #endif
390 }
391 }
392 xSeek->seek(position);
393 OSL_ENSURE(xSeek->getPosition() == position,"Wrong position after seeking the stream");
394
395#ifdef HSQLDB_DBG
396 aDataLog.seek( position );
397 OSL_ENSURE( xSeek->getPosition() == aDataLog.tell(), "Wrong position after seeking the stream" );
398#endif
399}
400
401
402void write_to_storage_stream_from_buffer( JNIEnv* env, jstring name, jstring key, jbyteArray buffer, jint off, jint len )
403{
404 std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
405 Reference< XOutputStream> xOut = pHelper ? pHelper->getOutputStream() : Reference< XOutputStream>();
406 OSL_ENSURE(xOut.is(),"Stream is NULL");
407
408 try
409 {
410 if ( xOut.is() )
411 {
412 jbyte *buf = env->GetByteArrayElements(buffer,nullptr);
413 if (env->ExceptionCheck())
414 {
415 env->ExceptionClear();
416 OSL_FAIL("ExceptionClear");
417 }
418 OSL_ENSURE(buf,"buf is NULL");
419 if ( buf && len > 0 && len <= env->GetArrayLength(buffer))
420 {
421 Sequence< ::sal_Int8 > aData(reinterpret_cast<sal_Int8 *>(buf + off),len);
422 env->ReleaseByteArrayElements(buffer, buf, JNI_ABORT);
423 xOut->writeBytes(aData);
424 }
425 }
426 else
427 {
429 "java/io/IOException",
430 "No OutputStream");
431 }
432 }
433 catch(const Exception& e)
434 {
435 TOOLS_WARN_EXCEPTION( "connectivity.hsqldb", "Exception caught! : write [BII)V");
436 StorageContainer::throwJavaException(e,env);
437 }
438}
439
440
441/*
442 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
443 * Method: write
444 * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)V
445 */
447 (JNIEnv * env, jobject obj_this,jstring name, jstring key, jbyteArray buffer, jint off, jint len)
448{
449#ifdef HSQLDB_DBG
450 OperationLogFile aOpLog( env, name, "data" );
451 aOpLog.logOperation( "write( byte[], int, int )" );
452
453 DataLogFile aDataLog( env, name, "data" );
454 write_to_storage_stream_from_buffer( env, obj_this, name, key, buffer, off, len, &aDataLog );
455#else
456 (void)obj_this;
457 write_to_storage_stream_from_buffer( env, name, key, buffer, off, len );
458#endif
459}
460
461
462void write_to_storage_stream( JNIEnv* env, jstring name, jstring key, jint v )
463{
464 std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
465 Reference< XOutputStream> xOut = pHelper ? pHelper->getOutputStream() : Reference< XOutputStream>();
466 OSL_ENSURE(xOut.is(),"Stream is NULL");
467 try
468 {
469 if ( xOut.is() )
470 {
471 Sequence< ::sal_Int8 > oneByte
472 {
473 static_cast<sal_Int8>((v >> 24) & 0xFF),
474 static_cast<sal_Int8>((v >> 16) & 0xFF),
475 static_cast<sal_Int8>((v >> 8) & 0xFF),
476 static_cast<sal_Int8>((v >> 0) & 0xFF)
477 };
478
479 xOut->writeBytes(oneByte);
480 }
481 else
482 {
484 "java/io/IOException",
485 "No OutputStream");
486 }
487 }
488 catch(const Exception& e)
489 {
490 TOOLS_WARN_EXCEPTION( "connectivity.hsqldb", "writeBytes(aData);");
491 StorageContainer::throwJavaException(e,env);
492 }
493}
494
495
496/*
497 * Class: com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess
498 * Method: writeInt
499 * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
500 */
502 (JNIEnv * env, jobject obj_this,jstring name, jstring key, jint v)
503{
504#ifdef HSQLDB_DBG
505 OperationLogFile aOpLog( env, name, "data" );
506 aOpLog.logOperation( "writeInt" );
507
508 DataLogFile aDataLog( env, name, "data" );
509 write_to_storage_stream( env, name, key, v, &aDataLog );
510#else
511 (void)obj_this;
513#endif
514}
515
516/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_JNI_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_getFilePointer(JNIEnv *env, jobject, jstring name, jstring key)
void write_to_storage_stream_from_buffer(JNIEnv *env, jstring name, jstring key, jbyteArray buffer, jint off, jint len)
#define ThrowException(env, type, msg)
SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_readInt(JNIEnv *env, jobject, jstring name, jstring key)
jint read_from_storage_stream_into_buffer(JNIEnv *env, jstring name, jstring key, jbyteArray buffer, jint off, jint len)
SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_openStream(JNIEnv *env, jobject, jstring name, jstring key, jint mode)
SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_seek(JNIEnv *env, jobject, jstring name, jstring key, jlong position)
SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_read__Ljava_lang_String_2Ljava_lang_String_2(JNIEnv *env, jobject, jstring name, jstring key)
SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_close(JNIEnv *env, jobject, jstring name, jstring key)
SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_read__Ljava_lang_String_2Ljava_lang_String_2_3BII(JNIEnv *env, jobject obj_this, jstring name, jstring key, jbyteArray buffer, jint off, jint len)
SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_write(JNIEnv *env, jobject obj_this, jstring name, jstring key, jbyteArray buffer, jint off, jint len)
void write_to_storage_stream(JNIEnv *env, jstring name, jstring key, jint v)
SAL_JNI_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_length(JNIEnv *env, jobject, jstring name, jstring key)
jint read_from_storage_stream(JNIEnv *env, jstring name, jstring key)
SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_NativeStorageAccess_writeInt(JNIEnv *env, jobject obj_this, jstring name, jstring key, jint v)
#define TOOLS_WARN_EXCEPTION(area, stream)
float v
OUString sName
sal_Int64 n
const css::uno::Reference< css::xml::crypto::XSecurityEnvironment > & env
def position(n=-1)
constexpr OUStringLiteral aData
@ Exception
OUString name
Definition: pq_statics.cxx:74
ConversionMode mode
signed char sal_Int8
#define BUFFER_SIZE