1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_connectivity.hxx" 26 27 #if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H 28 #include <config.h> 29 #endif 30 #include <com/sun/star/io/XStream.hpp> 31 #include <com/sun/star/container/XNameAccess.hpp> 32 #include <com/sun/star/document/XDocumentSubStorageSupplier.hpp> 33 #include <com/sun/star/embed/XStorage.hpp> 34 #include <com/sun/star/embed/ElementModes.hpp> 35 #include <comphelper/stl_types.hxx> 36 #include <comphelper/types.hxx> 37 #include "hsqldb/HStorageAccess.hxx" 38 #include "hsqldb/HStorageMap.hxx" 39 #include "hsqldb/StorageNativeInputStream.h" 40 41 #include "jvmaccess/virtualmachine.hxx" 42 #include <com/sun/star/lang/XSingleComponentFactory.hpp> 43 #include "accesslog.hxx" 44 45 #include <limits> 46 47 48 using namespace ::com::sun::star::container; 49 using namespace ::com::sun::star::uno; 50 using namespace ::com::sun::star::document; 51 using namespace ::com::sun::star::embed; 52 using namespace ::com::sun::star::io; 53 using namespace ::com::sun::star::lang; 54 using namespace ::connectivity::hsqldb; 55 56 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) ) 57 /*****************************************************************************/ 58 /* exception macros */ 59 60 #define ThrowException(env, type, msg) { \ 61 env->ThrowNew(env->FindClass(type), msg); } 62 /* 63 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream 64 * Method: openStream 65 * Signature: (Ljava/lang/String;Ljava/lang/String;I)V 66 */ 67 SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_openStream 68 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jint mode) 69 { 70 #ifdef HSQLDB_DBG 71 { 72 OperationLogFile( env, name, "input" ).logOperation( "openStream" ); 73 LogFile( env, name, "input" ).create(); 74 } 75 #endif 76 StorageContainer::registerStream(env,name,key,mode); 77 } 78 // ----------------------------------------------------------------------------- 79 80 /* 81 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream 82 * Method: read 83 * Signature: (Ljava/lang/String;Ljava/lang/String;)I 84 */ 85 SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2 86 (JNIEnv * env, jobject obj_this,jstring key, jstring name) 87 { 88 #ifdef HSQLDB_DBG 89 OperationLogFile( env, name, "input" ).logOperation( "read()" ); 90 91 DataLogFile aDataLog( env, name, "input" ); 92 return read_from_storage_stream( env, obj_this, name, key, &aDataLog ); 93 #else 94 return read_from_storage_stream( env, obj_this, name, key ); 95 #endif 96 } 97 // ----------------------------------------------------------------------------- 98 99 /* 100 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream 101 * Method: read 102 * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)I 103 */ 104 SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2_3BII 105 (JNIEnv * env, jobject obj_this,jstring key, jstring name, jbyteArray buffer, jint off, jint len) 106 { 107 #ifdef HSQLDB_DBG 108 OperationLogFile( env, name, "input" ).logOperation( "read( byte[], int, int )" ); 109 110 DataLogFile aDataLog( env, name, "input" ); 111 return read_from_storage_stream_into_buffer( env, obj_this, name, key, buffer, off, len, &aDataLog ); 112 #else 113 return read_from_storage_stream_into_buffer(env,obj_this,name,key,buffer,off,len); 114 #endif 115 } 116 // ----------------------------------------------------------------------------- 117 118 /* 119 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream 120 * Method: close 121 * Signature: (Ljava/lang/String;Ljava/lang/String;)V 122 */ 123 SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_close 124 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name) 125 { 126 #ifdef HSQLDB_DBG 127 OperationLogFile aOpLog( env, name, "input" ); 128 aOpLog.logOperation( "close" ); 129 aOpLog.close(); 130 131 LogFile aDataLog( env, name, "input" ); 132 aDataLog.close(); 133 #endif 134 StorageContainer::revokeStream(env,name,key); 135 } 136 // ----------------------------------------------------------------------------- 137 138 /* 139 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream 140 * Method: skip 141 * Signature: (Ljava/lang/String;Ljava/lang/String;J)J 142 */ 143 SAL_DLLPUBLIC_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_skip 144 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jlong n) 145 { 146 #ifdef HSQLDB_DBG 147 OperationLogFile( env, name, "input" ).logOperation( "skip()" ); 148 #endif 149 150 if ( n < 0 ) 151 ThrowException( env, 152 "java/io/IOException", 153 "n < 0"); 154 155 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key); 156 OSL_ENSURE(pHelper.get(),"No stream helper!"); 157 if ( pHelper.get() ) 158 { 159 Reference<XInputStream> xIn = pHelper->getInputStream(); 160 if ( xIn.is() ) 161 { 162 try 163 { 164 sal_Int64 tmpLongVal = n; 165 sal_Int32 tmpIntVal; 166 167 try 168 { 169 do { 170 if (tmpLongVal >= ::std::numeric_limits<sal_Int64>::max() ) 171 tmpIntVal = ::std::numeric_limits<sal_Int32>::max(); 172 else // Casting is safe here. 173 tmpIntVal = static_cast<sal_Int32>(tmpLongVal); 174 175 tmpLongVal -= tmpIntVal; 176 177 xIn->skipBytes(tmpIntVal); 178 179 } while (tmpLongVal > 0); 180 } 181 catch(Exception& ) 182 { 183 } 184 185 return n - tmpLongVal; 186 } 187 catch(Exception& e) 188 { 189 OSL_ENSURE(0,"Exception catched! : skip();"); 190 StorageContainer::throwJavaException(e,env); 191 } 192 } 193 } 194 else 195 { 196 ThrowException( env, 197 "java/io/IOException", 198 "Stream is not valid"); 199 } 200 return 0; 201 } 202 // ----------------------------------------------------------------------------- 203 204 /* 205 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream 206 * Method: available 207 * Signature: (Ljava/lang/String;Ljava/lang/String;)I 208 */ 209 SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_available 210 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name) 211 { 212 #ifdef HSQLDB_DBG 213 OperationLogFile aOpLog( env, name, "input" ); 214 aOpLog.logOperation( "available" ); 215 #endif 216 217 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key); 218 OSL_ENSURE(pHelper.get(),"No stream helper!"); 219 Reference<XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference<XInputStream>(); 220 if ( xIn.is() ) 221 { 222 try 223 { 224 jint nAvailable = xIn->available(); 225 #ifdef HSQLDB_DBG 226 aOpLog.logReturn( nAvailable ); 227 #endif 228 return nAvailable; 229 } 230 catch(Exception& e) 231 { 232 OSL_ENSURE(0,"Exception caught! : available();"); 233 StorageContainer::throwJavaException(e,env); 234 } 235 } 236 else 237 { 238 ThrowException( env, 239 "java/io/IOException", 240 "Stream is not valid"); 241 } 242 return 0; 243 } 244 // ----------------------------------------------------------------------------- 245 246 /* 247 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream 248 * Method: read 249 * Signature: (Ljava/lang/String;Ljava/lang/String;[B)I 250 */ 251 SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2_3B 252 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jbyteArray buffer) 253 { 254 #ifdef HSQLDB_DBG 255 OperationLogFile aOpLog( env, name, "input" ); 256 aOpLog.logOperation( "read( byte[] )" ); 257 258 DataLogFile aDataLog( env, name, "input" ); 259 #endif 260 261 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key); 262 Reference< XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference< XInputStream>(); 263 OSL_ENSURE(xIn.is(),"Input stream is NULL!"); 264 jint nBytesRead = 0; 265 if ( xIn.is() ) 266 { 267 jsize nLen = env->GetArrayLength(buffer); 268 Sequence< ::sal_Int8 > aData(nLen); 269 270 try 271 { 272 nBytesRead = xIn->readBytes(aData,nLen); 273 } 274 catch(Exception& e) 275 { 276 OSL_ENSURE(0,"Exception catched! : skip();"); 277 StorageContainer::throwJavaException(e,env); 278 } 279 280 // Casting bytesRead to an int is okay, since the user can 281 // only pass in an integer length to read, so the bytesRead 282 // must <= len. 283 // 284 if (nBytesRead <= 0) { 285 #ifdef HSQLDB_DBG 286 aOpLog.logReturn( (jint)-1 ); 287 #endif 288 return -1; 289 } 290 OSL_ENSURE(nLen >= nBytesRead,"Buffer is too small!"); 291 OSL_ENSURE(aData.getLength() >= nBytesRead,"Buffer is too small!"); 292 env->SetByteArrayRegion(buffer,0,nBytesRead,&aData[0]); 293 #ifdef HSQLDB_DBG 294 aDataLog.write( &aData[0], nBytesRead ); 295 #endif 296 } 297 #ifdef HSQLDB_DBG 298 aOpLog.logReturn( nBytesRead ); 299 #endif 300 return nBytesRead; 301 } 302 // ----------------------------------------------------------------------------- 303