1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 #include "java/sql/Blob.hxx"
31 #include "java/tools.hxx"
32 #include "java/io/InputStream.hxx"
33 #ifndef _INC_MEMORY
34 //#include <memory.h>
35 #endif
36 #include <connectivity/dbexception.hxx>
37 
38 #include <string.h>
39 
40 using namespace connectivity;
41 //**************************************************************
42 //************ Class: java.sql.Blob
43 //**************************************************************
44 
45 jclass java_sql_Blob::theClass = 0;
46 java_sql_Blob::java_sql_Blob( JNIEnv * pEnv, jobject myObj )
47 	: java_lang_Object( pEnv, myObj )
48 {
49 	SDBThreadAttach::addRef();
50 }
51 java_sql_Blob::~java_sql_Blob()
52 {
53 	SDBThreadAttach::releaseRef();
54 }
55 
56 jclass java_sql_Blob::getMyClass() const
57 {
58 	// die Klasse muss nur einmal geholt werden, daher statisch
59 	if( !theClass )
60         theClass = findMyClass("java/sql/Blob");
61 	return theClass;
62 }
63 
64 sal_Int64 SAL_CALL java_sql_Blob::length(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
65 {
66 	jlong out(0);
67     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
68 
69 	{
70 		// temporaere Variable initialisieren
71 		static const char * cSignature = "()J";
72 		static const char * cMethodName = "length";
73 		// Java-Call absetzen
74 		static jmethodID mID(NULL);
75         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
76 		out = t.pEnv->CallLongMethod( object, mID );
77 		ThrowSQLException(t.pEnv,*this);
78 	} //t.pEnv
79 	return (sal_Int64)out;
80 }
81 ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL java_sql_Blob::getBytes( sal_Int64 pos, sal_Int32 count ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
82 {
83 
84     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
85 	::com::sun::star::uno::Sequence< sal_Int8 > aSeq;
86 	{
87 		// temporaere Variable initialisieren
88 		static const char * cSignature = "(JI)[B";
89 		static const char * cMethodName = "getBytes";
90 		// Java-Call absetzen
91 		static jmethodID mID(NULL);
92         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
93 		jbyteArray out = (jbyteArray)t.pEnv->CallObjectMethod( object, mID,pos,count);
94 		ThrowSQLException(t.pEnv,*this);
95 		if(out)
96 		{
97 			jboolean p = sal_False;
98 			aSeq.realloc(t.pEnv->GetArrayLength(out));
99 			memcpy(aSeq.getArray(),t.pEnv->GetByteArrayElements(out,&p),aSeq.getLength());
100 			t.pEnv->DeleteLocalRef(out);
101 		}
102 	} //t.pEnv
103 	// ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
104 	return 	aSeq;
105 }
106 
107 ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL java_sql_Blob::getBinaryStream(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
108 {
109     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
110     static jmethodID mID(NULL);
111     jobject out = callObjectMethod(t.pEnv,"getBinaryStream","()Ljava/io/InputStream;", mID);
112 	// ACHTUNG: der Aufrufer wird Eigentuemer des zurueckgelieferten Zeigers !!!
113 	return out==0 ? 0 : new java_io_InputStream( t.pEnv, out );
114 }
115 
116 sal_Int64 SAL_CALL java_sql_Blob::position( const ::com::sun::star::uno::Sequence< sal_Int8 >& pattern, sal_Int64 start ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
117 {
118 	jlong out(0);
119     SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
120 
121 	{
122 		// temporaere Variable initialisieren
123 		static const char * cSignature = "([BI)J";
124 		static const char * cMethodName = "position";
125 		// Java-Call absetzen
126 		static jmethodID mID(NULL);
127         obtainMethodId(t.pEnv, cMethodName,cSignature, mID);
128 		// Parameter konvertieren
129 		jbyteArray pByteArray = t.pEnv->NewByteArray(pattern.getLength());
130 		t.pEnv->SetByteArrayRegion(pByteArray,0,pattern.getLength(),(jbyte*)pattern.getConstArray());
131 		out = t.pEnv->CallLongMethod( object, mID, pByteArray,start );
132 		t.pEnv->DeleteLocalRef(pByteArray);
133 		ThrowSQLException(t.pEnv,*this);
134 	} //t.pEnv
135 	return (sal_Int64)out;
136 }
137 
138 sal_Int64 SAL_CALL java_sql_Blob::positionOfBlob( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XBlob >& /*pattern*/, sal_Int64 /*start*/ ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException)
139 {
140     ::dbtools::throwFeatureNotImplementedException( "XBlob::positionOfBlob", *this );
141     // this was put here in CWS warnings01. The previous implementation was defective, as it did ignore
142     // the pattern parameter. Since the effort for proper implementation is rather high - we would need
143     // to translated patter into a byte[] -, we defer this functionality for the moment (hey, it was
144     // unusable, anyway)
145     // 2005-11-15 / #i57457# / frank.schoenheit@sun.com
146     return 0;
147 }
148 
149