LibreOffice Module connectivity (master) 1
StorageNativeInputStream.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
20
21#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
22#include <config.h>
23#endif
24#include <com/sun/star/io/XStream.hpp>
25#include <com/sun/star/document/XDocumentSubStorageSupplier.hpp>
28
29#include <osl/diagnose.h>
31#include "accesslog.hxx"
32
33#include <limits>
34
35
36using namespace ::com::sun::star::container;
37using namespace ::com::sun::star::uno;
38using namespace ::com::sun::star::document;
39using namespace ::com::sun::star::embed;
40using namespace ::com::sun::star::io;
41using namespace ::com::sun::star::lang;
42using namespace ::connectivity::hsqldb;
43
44/*****************************************************************************/
45/* exception macros */
46
47#define ThrowException(env, type, msg) { \
48 env->ThrowNew(env->FindClass(type), msg); }
49/*
50 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
51 * Method: openStream
52 * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
53 */
55 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jint mode)
56{
57#ifdef HSQLDB_DBG
58 {
59 OperationLogFile( env, name, "input" ).logOperation( "openStream" );
60 LogFile( env, name, "input" ).create();
61 }
62#endif
63 StorageContainer::registerStream(env,name,key,mode);
64}
65
66
67/*
68 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
69 * Method: read
70 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
71 */
73 (JNIEnv * env, jobject /*obj_this*/, jstring key, jstring name)
74{
75#ifdef HSQLDB_DBG
76 OperationLogFile( env, name, "input" ).logOperation( "read()" );
77
78 DataLogFile aDataLog( env, name, "input" );
79 return read_from_storage_stream( env, obj_this, name, key, &aDataLog );
80#else
81 return read_from_storage_stream( env, name, key );
82#endif
83}
84
85
86/*
87 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
88 * Method: read
89 * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)I
90 */
92 (JNIEnv * env, jobject obj_this, jstring key, jstring name, jbyteArray buffer, jint off, jint len)
93{
94#ifdef HSQLDB_DBG
95 OperationLogFile( env, name, "input" ).logOperation( "read( byte[], int, int )" );
96
97 DataLogFile aDataLog( env, name, "input" );
98 return read_from_storage_stream_into_buffer( env, obj_this, name, key, buffer, off, len, &aDataLog );
99#else
100 (void)obj_this;
101 return read_from_storage_stream_into_buffer(env, name,key,buffer,off,len);
102#endif
103}
104
105
106/*
107 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
108 * Method: close
109 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
110 */
112 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name)
113{
114#ifdef HSQLDB_DBG
115 OperationLogFile aOpLog( env, name, "input" );
116 aOpLog.logOperation( "close" );
117 aOpLog.close();
118
119 LogFile aDataLog( env, name, "input" );
120 aDataLog.close();
121#endif
122 StorageContainer::revokeStream(env,name,key);
123}
124
125
126/*
127 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
128 * Method: skip
129 * Signature: (Ljava/lang/String;Ljava/lang/String;J)J
130 */
132 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jlong n)
133{
134#ifdef HSQLDB_DBG
135 OperationLogFile( env, name, "input" ).logOperation( "skip()" );
136#endif
137
138 if ( n < 0 )
140 "java/io/IOException",
141 "n < 0");
142
143 std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
144 OSL_ENSURE(pHelper,"No stream helper!");
145 if ( pHelper )
146 {
147 Reference<XInputStream> xIn = pHelper->getInputStream();
148 if ( xIn.is() )
149 {
150 try
151 {
152 sal_Int64 tmpLongVal = n;
153 sal_Int32 tmpIntVal;
154
155 try
156 {
157 do {
158 if (tmpLongVal >= std::numeric_limits<sal_Int64>::max() )
159 tmpIntVal = std::numeric_limits<sal_Int32>::max();
160 else // Casting is safe here.
161 tmpIntVal = static_cast<sal_Int32>(tmpLongVal);
162
163 tmpLongVal -= tmpIntVal;
164
165 xIn->skipBytes(tmpIntVal);
166
167 } while (tmpLongVal > 0);
168 }
169 catch(const Exception&)
170 {
171 }
172
173 return n - tmpLongVal;
174 }
175 catch(const Exception& e)
176 {
177 TOOLS_WARN_EXCEPTION( "connectivity.hsqldb", "skip();");
178 StorageContainer::throwJavaException(e,env);
179 }
180 }
181 }
182 else
183 {
185 "java/io/IOException",
186 "Stream is not valid");
187 }
188 return 0;
189}
190
191
192/*
193 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
194 * Method: available
195 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
196 */
198 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name)
199{
200#ifdef HSQLDB_DBG
201 OperationLogFile aOpLog( env, name, "input" );
202 aOpLog.logOperation( "available" );
203#endif
204
205 std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
206 OSL_ENSURE(pHelper,"No stream helper!");
207 Reference<XInputStream> xIn = pHelper ? pHelper->getInputStream() : Reference<XInputStream>();
208 if ( xIn.is() )
209 {
210 try
211 {
212 jint nAvailable = xIn->available();
213#ifdef HSQLDB_DBG
214 aOpLog.logReturn( nAvailable );
215#endif
216 return nAvailable;
217 }
218 catch(const Exception& e)
219 {
220 TOOLS_WARN_EXCEPTION( "connectivity.hsqldb", "available();");
221 StorageContainer::throwJavaException(e,env);
222 }
223 }
224 else
225 {
227 "java/io/IOException",
228 "Stream is not valid");
229 }
230 return 0;
231}
232
233
234/*
235 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
236 * Method: read
237 * Signature: (Ljava/lang/String;Ljava/lang/String;[B)I
238 */
240 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jbyteArray buffer)
241{
242#ifdef HSQLDB_DBG
243 OperationLogFile aOpLog( env, name, "input" );
244 aOpLog.logOperation( "read( byte[] )" );
245
246 DataLogFile aDataLog( env, name, "input" );
247#endif
248
249 std::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
250 Reference< XInputStream> xIn = pHelper ? pHelper->getInputStream() : Reference< XInputStream>();
251 OSL_ENSURE(xIn.is(),"Input stream is NULL!");
252 jint nBytesRead = 0;
253 if ( xIn.is() )
254 {
255 jsize nLen = env->GetArrayLength(buffer);
256 Sequence< ::sal_Int8 > aData(nLen);
257
258 try
259 {
260 nBytesRead = xIn->readBytes(aData,nLen);
261 }
262 catch(const Exception& e)
263 {
264 TOOLS_WARN_EXCEPTION( "connectivity.hsqldb", "skip();");
265 StorageContainer::throwJavaException(e,env);
266 }
267
268 // Casting bytesRead to an int is okay, since the user can
269 // only pass in an integer length to read, so the bytesRead
270 // must <= len.
271
272 if (nBytesRead <= 0) {
273#ifdef HSQLDB_DBG
274 aOpLog.logReturn( (jint)-1 );
275#endif
276 return -1;
277 }
278 OSL_ENSURE(nLen >= nBytesRead,"Buffer is too small!");
279 OSL_ENSURE(aData.getLength() >= nBytesRead,"Buffer is too small!");
280 env->SetByteArrayRegion(buffer, 0, nBytesRead, reinterpret_cast<const jbyte*>(&aData[0]));
281#ifdef HSQLDB_DBG
282 aDataLog.write( &aData[0], nBytesRead );
283#endif
284 }
285#ifdef HSQLDB_DBG
286 aOpLog.logReturn( nBytesRead );
287#endif
288 return nBytesRead;
289}
290
291
292/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
jint read_from_storage_stream_into_buffer(JNIEnv *env, jstring name, jstring key, jbyteArray buffer, jint off, jint len)
jint read_from_storage_stream(JNIEnv *env, jstring name, jstring key)
#define ThrowException(env, type, msg)
SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2(JNIEnv *env, jobject, jstring key, jstring name)
SAL_JNI_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_skip(JNIEnv *env, jobject, jstring key, jstring name, jlong n)
SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_openStream(JNIEnv *env, jobject, jstring key, jstring name, jint mode)
SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_available(JNIEnv *env, jobject, jstring key, jstring name)
SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2_3BII(JNIEnv *env, jobject obj_this, jstring key, jstring name, jbyteArray buffer, jint off, jint len)
SAL_JNI_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2_3B(JNIEnv *env, jobject, jstring key, jstring name, jbyteArray buffer)
SAL_JNI_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_close(JNIEnv *env, jobject, jstring key, jstring name)
#define TOOLS_WARN_EXCEPTION(area, stream)
sal_Int64 n
const css::uno::Reference< css::xml::crypto::XSecurityEnvironment > & env
constexpr OUStringLiteral aData
@ Exception
OUString name
Definition: pq_statics.cxx:74
ConversionMode mode