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