Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / connectivity / source / drivers / jdbc / Blob.cxx
blob4531fc9b8724302811fe314323c1a539db44d235
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <java/sql/Blob.hxx>
21 #include <java/io/InputStream.hxx>
22 #include <connectivity/dbexception.hxx>
23 #include <osl/diagnose.h>
25 #include <string.h>
27 using namespace connectivity;
29 //************ Class: java.sql.Blob
32 jclass java_sql_Blob::theClass = nullptr;
33 java_sql_Blob::java_sql_Blob( JNIEnv * pEnv, jobject myObj )
34 : java_lang_Object( pEnv, myObj )
36 SDBThreadAttach::addRef();
38 java_sql_Blob::~java_sql_Blob()
40 SDBThreadAttach::releaseRef();
43 jclass java_sql_Blob::getMyClass() const
45 // the class must be fetched only once, therefore it's static
46 if( !theClass )
47 theClass = findMyClass("java/sql/Blob");
48 return theClass;
51 sal_Int64 SAL_CALL java_sql_Blob::length( )
53 jlong out(0);
54 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
57 // initialize temporary variable
58 static const char * const cSignature = "()J";
59 static const char * const cMethodName = "length";
60 // submit Java-Call
61 static jmethodID mID(nullptr);
62 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
63 out = t.pEnv->CallLongMethod( object, mID );
64 ThrowSQLException(t.pEnv,*this);
65 } //t.pEnv
66 return static_cast<sal_Int64>(out);
68 css::uno::Sequence< sal_Int8 > SAL_CALL java_sql_Blob::getBytes( sal_Int64 pos, sal_Int32 count )
71 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
72 css::uno::Sequence< sal_Int8 > aSeq;
74 // initialize temporary variable
75 static const char * const cSignature = "(JI)[B";
76 static const char * const cMethodName = "getBytes";
77 // submit Java-Call
78 static jmethodID mID(nullptr);
79 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
80 jbyteArray out = static_cast<jbyteArray>(t.pEnv->CallObjectMethod( object, mID,pos,count));
81 ThrowSQLException(t.pEnv,*this);
82 if(out)
84 jboolean p = false;
85 aSeq.realloc(t.pEnv->GetArrayLength(out));
86 memcpy(aSeq.getArray(),t.pEnv->GetByteArrayElements(out,&p),aSeq.getLength());
87 t.pEnv->DeleteLocalRef(out);
89 } //t.pEnv
90 // WARNING: the caller becomes the owner of the returned pointer
91 return aSeq;
94 css::uno::Reference< css::io::XInputStream > SAL_CALL java_sql_Blob::getBinaryStream( )
96 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
97 static jmethodID mID(nullptr);
98 jobject out = callObjectMethod(t.pEnv,"getBinaryStream","()Ljava/io/InputStream;", mID);
99 // WARNING: the caller becomes the owner of the returned pointer
100 return out==nullptr ? nullptr : new java_io_InputStream( t.pEnv, out );
103 sal_Int64 SAL_CALL java_sql_Blob::position( const css::uno::Sequence< sal_Int8 >& pattern, sal_Int64 start )
105 jlong out(0);
106 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
109 // initialize temporary variable
110 static const char * const cSignature = "([BI)J";
111 static const char * const cMethodName = "position";
112 // submit Java-Call
113 static jmethodID mID(nullptr);
114 obtainMethodId_throwSQL(t.pEnv, cMethodName,cSignature, mID);
115 // convert Parameter
116 jbyteArray pByteArray = t.pEnv->NewByteArray(pattern.getLength());
117 jbyte * patternData = reinterpret_cast<jbyte *>(
118 const_cast<sal_Int8 *>(pattern.getConstArray()));
119 // 4th param of Set*ArrayRegion changed from pointer to non-const to
120 // pointer to const between <http://docs.oracle.com/javase/6/docs/
121 // technotes/guides/jni/spec/functions.html#wp22933> and
122 // <http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/
123 // functions.html#wp22933>; work around that difference in a way
124 // that doesn't trigger loplugin:redundantcast
125 t.pEnv->SetByteArrayRegion(pByteArray,0,pattern.getLength(),patternData);
126 out = t.pEnv->CallLongMethod( object, mID, pByteArray,start );
127 t.pEnv->DeleteLocalRef(pByteArray);
128 ThrowSQLException(t.pEnv,*this);
129 } //t.pEnv
130 return static_cast<sal_Int64>(out);
133 sal_Int64 SAL_CALL java_sql_Blob::positionOfBlob( const css::uno::Reference< css::sdbc::XBlob >& /*pattern*/, sal_Int64 /*start*/ )
135 ::dbtools::throwFeatureNotImplementedSQLException( "XBlob::positionOfBlob", *this );
136 // this was put here in CWS warnings01. The previous implementation was defective, as it did ignore
137 // the pattern parameter. Since the effort for proper implementation is rather high - we would need
138 // to translated pattern into a byte[] -, we defer this functionality for the moment (hey, it was
139 // unusable, anyway)
140 // #i57457#
141 return 0;
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */