LibreOffice Module connectivity (master) 1
jdbc/Blob.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <java/sql/Blob.hxx>
23#include <osl/diagnose.h>
24
25#include <string.h>
26
27using namespace connectivity;
28
29//************ Class: java.sql.Blob
30
31
32jclass java_sql_Blob::theClass = nullptr;
33java_sql_Blob::java_sql_Blob( JNIEnv * pEnv, jobject myObj )
34 : java_lang_Object( pEnv, myObj )
35{
37}
39{
41}
42
44{
45 // the class must be fetched only once, therefore it's static
46 if( !theClass )
47 theClass = findMyClass("java/sql/Blob");
48 return theClass;
49}
50
51sal_Int64 SAL_CALL java_sql_Blob::length( )
52{
53 jlong out(0);
54 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
55
56 {
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);
67}
68css::uno::Sequence< sal_Int8 > SAL_CALL java_sql_Blob::getBytes( sal_Int64 pos, sal_Int32 count )
69{
70
71 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
72 css::uno::Sequence< sal_Int8 > aSeq;
73 {
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)
83 {
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);
88 }
89 } //t.pEnv
90 // WARNING: the caller becomes the owner of the returned pointer
91 return aSeq;
92}
93
94css::uno::Reference< css::io::XInputStream > SAL_CALL java_sql_Blob::getBinaryStream( )
95{
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 );
101}
102
103sal_Int64 SAL_CALL java_sql_Blob::position( const css::uno::Sequence< sal_Int8 >& pattern, sal_Int64 start )
104{
105 jlong out(0);
106 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
107
108 {
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);
131}
132
133sal_Int64 SAL_CALL java_sql_Blob::positionOfBlob( const css::uno::Reference< css::sdbc::XBlob >& /*pattern*/, sal_Int64 /*start*/ )
134{
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;
142}
143
144/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
static void releaseRef()
Definition: Object.cxx:81
jobject callObjectMethod(JNIEnv *pEnv, const char *_pMethodName, const char *_pSignature, jmethodID &_inout_MethodID) const
Definition: Object.cxx:404
static void ThrowSQLException(JNIEnv *pEnv, const css::uno::Reference< css::uno::XInterface > &_rContext)
Definition: Object.cxx:208
static jclass findMyClass(const char *_pClassName)
Definition: Object.cxx:466
void obtainMethodId_throwSQL(JNIEnv *_pEnv, const char *_pMethodName, const char *_pSignature, jmethodID &_inout_MethodID) const
Definition: Object.cxx:229
virtual sal_Int64 SAL_CALL positionOfBlob(const css::uno::Reference< css::sdbc::XBlob > &pattern, sal_Int64 start) override
Definition: jdbc/Blob.cxx:133
virtual ~java_sql_Blob() override
Definition: jdbc/Blob.cxx:38
virtual sal_Int64 SAL_CALL position(const css::uno::Sequence< sal_Int8 > &pattern, sal_Int64 start) override
Definition: jdbc/Blob.cxx:103
virtual sal_Int64 SAL_CALL length() override
Definition: jdbc/Blob.cxx:51
virtual jclass getMyClass() const override
Definition: jdbc/Blob.cxx:43
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getBytes(sal_Int64 pos, sal_Int32 length) override
Definition: jdbc/Blob.cxx:68
virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getBinaryStream() override
Definition: jdbc/Blob.cxx:94
void * p
Sequence< sal_Int8 > aSeq
void throwFeatureNotImplementedSQLException(const OUString &_rFeatureName, const Reference< XInterface > &_rxContext, const Any &_rNextException)
signed char sal_Int8
size_t pos