1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: StorageNativeInputStream.cxx,v $
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
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"
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
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
)
79 OperationLogFile( env
, name
, "input" ).logOperation( "openStream" );
80 LogFile( env
, name
, "input" ).create();
83 StorageContainer::registerStream(env
,name
,key
,mode
);
85 // -----------------------------------------------------------------------------
88 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
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
)
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
);
101 return read_from_storage_stream( env
, obj_this
, name
, key
);
104 // -----------------------------------------------------------------------------
107 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
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
)
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
);
120 return read_from_storage_stream_into_buffer(env
,obj_this
,name
,key
,buffer
,off
,len
);
123 // -----------------------------------------------------------------------------
126 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
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
)
134 OperationLogFile
aOpLog( env
, name
, "input" );
135 aOpLog
.logOperation( "close" );
138 LogFile
aDataLog( env
, name
, "input" );
141 StorageContainer::revokeStream(env
,name
,key
);
143 // -----------------------------------------------------------------------------
146 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
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
)
154 OperationLogFile( env
, name
, "input" ).logOperation( "skip()" );
159 "java/io/IOException",
162 ::boost::shared_ptr
<StreamHelper
> pHelper
= StorageContainer::getRegisteredStream(env
,name
,key
);
163 OSL_ENSURE(pHelper
.get(),"No stream helper!");
166 Reference
<XInputStream
> xIn
= pHelper
->getInputStream();
171 sal_Int64 tmpLongVal
= n
;
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);
192 return n
- tmpLongVal
;
196 OSL_ENSURE(0,"Exception catched! : skip();");
197 StorageContainer::throwJavaException(e
,env
);
204 "java/io/IOException",
205 "Stream is not valid");
209 // -----------------------------------------------------------------------------
212 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
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
)
220 OperationLogFile
aOpLog( env
, name
, "input" );
221 aOpLog
.logOperation( "available" );
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
>();
231 jint nAvailable
= xIn
->available();
233 aOpLog
.logReturn( nAvailable
);
239 OSL_ENSURE(0,"Exception caught! : available();");
240 StorageContainer::throwJavaException(e
,env
);
246 "java/io/IOException",
247 "Stream is not valid");
251 // -----------------------------------------------------------------------------
254 * Class: com_sun_star_sdbcx_comp_hsqldb_StorageNativeInputStream
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
)
262 OperationLogFile
aOpLog( env
, name
, "input" );
263 aOpLog
.logOperation( "read( byte[] )" );
265 DataLogFile
aDataLog( env
, name
, "input" );
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!");
274 jsize nLen
= env
->GetArrayLength(buffer
);
275 Sequence
< ::sal_Int8
> aData(nLen
);
279 nBytesRead
= xIn
->readBytes(aData
,nLen
);
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
291 if (nBytesRead
<= 0) {
293 aOpLog
.logReturn( (jint
)-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]);
301 aDataLog
.write( &aData
[0], nBytesRead
);
305 aOpLog
.logReturn( nBytesRead
);
309 // -----------------------------------------------------------------------------