Update ooo320-m1
[ooovba.git] / connectivity / source / drivers / hsqldb / StorageNativeInputStream.cxx
blob9eef51caa6ae7e3297f92bc9f41d863d7a06c1c2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: StorageNativeInputStream.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
34 #if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 #include <com/sun/star/io/XStream.hpp>
38 #include <com/sun/star/container/XNameAccess.hpp>
39 #include <com/sun/star/document/XDocumentSubStorageSupplier.hpp>
40 #include <com/sun/star/embed/XStorage.hpp>
41 #include <com/sun/star/embed/ElementModes.hpp>
42 #include <comphelper/stl_types.hxx>
43 #include <comphelper/types.hxx>
44 #include "hsqldb/HStorageAccess.hxx"
45 #include "hsqldb/HStorageMap.hxx"
46 #include "hsqldb/StorageNativeInputStream.h"
48 #include "jvmaccess/virtualmachine.hxx"
49 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
50 #include "accesslog.hxx"
52 #include <limits>
55 using namespace ::com::sun::star::container;
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::document;
58 using namespace ::com::sun::star::embed;
59 using namespace ::com::sun::star::io;
60 using namespace ::com::sun::star::lang;
61 using namespace ::connectivity::hsqldb;
63 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
64 /*****************************************************************************/
65 /* exception macros */
67 #define ThrowException(env, type, msg) { \
68 env->ThrowNew(env->FindClass(type), msg); }
70 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
71 * Method: openStream
72 * Signature: (Ljava/lang/String;Ljava/lang/String;I)V
74 SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_openStream
75 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jint mode)
77 #ifdef HSQLDB_DBG
79 OperationLogFile( env, name, "input" ).logOperation( "openStream" );
80 LogFile( env, name, "input" ).create();
82 #endif
83 StorageContainer::registerStream(env,name,key,mode);
85 // -----------------------------------------------------------------------------
88 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
89 * Method: read
90 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
92 SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2
93 (JNIEnv * env, jobject obj_this,jstring key, jstring name)
95 #ifdef HSQLDB_DBG
96 OperationLogFile( env, name, "input" ).logOperation( "read()" );
98 DataLogFile aDataLog( env, name, "input" );
99 return read_from_storage_stream( env, obj_this, name, key, &aDataLog );
100 #else
101 return read_from_storage_stream( env, obj_this, name, key );
102 #endif
104 // -----------------------------------------------------------------------------
107 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
108 * Method: read
109 * Signature: (Ljava/lang/String;Ljava/lang/String;[BII)I
111 SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2_3BII
112 (JNIEnv * env, jobject obj_this,jstring key, jstring name, jbyteArray buffer, jint off, jint len)
114 #ifdef HSQLDB_DBG
115 OperationLogFile( env, name, "input" ).logOperation( "read( byte[], int, int )" );
117 DataLogFile aDataLog( env, name, "input" );
118 return read_from_storage_stream_into_buffer( env, obj_this, name, key, buffer, off, len, &aDataLog );
119 #else
120 return read_from_storage_stream_into_buffer(env,obj_this,name,key,buffer,off,len);
121 #endif
123 // -----------------------------------------------------------------------------
126 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
127 * Method: close
128 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
130 SAL_DLLPUBLIC_EXPORT void JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_close
131 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name)
133 #ifdef HSQLDB_DBG
134 OperationLogFile aOpLog( env, name, "input" );
135 aOpLog.logOperation( "close" );
136 aOpLog.close();
138 LogFile aDataLog( env, name, "input" );
139 aDataLog.close();
140 #endif
141 StorageContainer::revokeStream(env,name,key);
143 // -----------------------------------------------------------------------------
146 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
147 * Method: skip
148 * Signature: (Ljava/lang/String;Ljava/lang/String;J)J
150 SAL_DLLPUBLIC_EXPORT jlong JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_skip
151 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jlong n)
153 #ifdef HSQLDB_DBG
154 OperationLogFile( env, name, "input" ).logOperation( "skip()" );
155 #endif
157 if ( n < 0 )
158 ThrowException( env,
159 "java/io/IOException",
160 "n < 0");
162 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
163 OSL_ENSURE(pHelper.get(),"No stream helper!");
164 if ( pHelper.get() )
166 Reference<XInputStream> xIn = pHelper->getInputStream();
167 if ( xIn.is() )
171 sal_Int64 tmpLongVal = n;
172 sal_Int32 tmpIntVal;
176 do {
177 if (tmpLongVal >= ::std::numeric_limits<sal_Int64>::max() )
178 tmpIntVal = ::std::numeric_limits<sal_Int32>::max();
179 else // Casting is safe here.
180 tmpIntVal = static_cast<sal_Int32>(tmpLongVal);
182 tmpLongVal -= tmpIntVal;
184 xIn->skipBytes(tmpIntVal);
186 } while (tmpLongVal > 0);
188 catch(Exception& )
192 return n - tmpLongVal;
194 catch(Exception& e)
196 OSL_ENSURE(0,"Exception catched! : skip();");
197 StorageContainer::throwJavaException(e,env);
201 else
203 ThrowException( env,
204 "java/io/IOException",
205 "Stream is not valid");
207 return 0;
209 // -----------------------------------------------------------------------------
212 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
213 * Method: available
214 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
216 SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_available
217 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name)
219 #ifdef HSQLDB_DBG
220 OperationLogFile aOpLog( env, name, "input" );
221 aOpLog.logOperation( "available" );
222 #endif
224 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
225 OSL_ENSURE(pHelper.get(),"No stream helper!");
226 Reference<XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference<XInputStream>();
227 if ( xIn.is() )
231 jint nAvailable = xIn->available();
232 #ifdef HSQLDB_DBG
233 aOpLog.logReturn( nAvailable );
234 #endif
235 return nAvailable;
237 catch(Exception& e)
239 OSL_ENSURE(0,"Exception caught! : available();");
240 StorageContainer::throwJavaException(e,env);
243 else
245 ThrowException( env,
246 "java/io/IOException",
247 "Stream is not valid");
249 return 0;
251 // -----------------------------------------------------------------------------
254 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
255 * Method: read
256 * Signature: (Ljava/lang/String;Ljava/lang/String;[B)I
258 SAL_DLLPUBLIC_EXPORT jint JNICALL Java_com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream_read__Ljava_lang_String_2Ljava_lang_String_2_3B
259 (JNIEnv * env, jobject /*obj_this*/,jstring key, jstring name, jbyteArray buffer)
261 #ifdef HSQLDB_DBG
262 OperationLogFile aOpLog( env, name, "input" );
263 aOpLog.logOperation( "read( byte[] )" );
265 DataLogFile aDataLog( env, name, "input" );
266 #endif
268 ::boost::shared_ptr<StreamHelper> pHelper = StorageContainer::getRegisteredStream(env,name,key);
269 Reference< XInputStream> xIn = pHelper.get() ? pHelper->getInputStream() : Reference< XInputStream>();
270 OSL_ENSURE(xIn.is(),"Input stream is NULL!");
271 jint nBytesRead = 0;
272 if ( xIn.is() )
274 jsize nLen = env->GetArrayLength(buffer);
275 Sequence< ::sal_Int8 > aData(nLen);
279 nBytesRead = xIn->readBytes(aData,nLen);
281 catch(Exception& e)
283 OSL_ENSURE(0,"Exception catched! : skip();");
284 StorageContainer::throwJavaException(e,env);
287 // Casting bytesRead to an int is okay, since the user can
288 // only pass in an integer length to read, so the bytesRead
289 // must <= len.
291 if (nBytesRead <= 0) {
292 #ifdef HSQLDB_DBG
293 aOpLog.logReturn( (jint)-1 );
294 #endif
295 return -1;
297 OSL_ENSURE(nLen >= nBytesRead,"Buffer is too small!");
298 OSL_ENSURE(aData.getLength() >= nBytesRead,"Buffer is too small!");
299 env->SetByteArrayRegion(buffer,0,nBytesRead,&aData[0]);
300 #ifdef HSQLDB_DBG
301 aDataLog.write( &aData[0], nBytesRead );
302 #endif
304 #ifdef HSQLDB_DBG
305 aOpLog.logReturn( nBytesRead );
306 #endif
307 return nBytesRead;
309 // -----------------------------------------------------------------------------