cid#1636677 Uninitialized scalar field
[LibreOffice.git] / connectivity / source / drivers / jdbc / Reader.cxx
blob85d02e87a3e34e64ad52690d18ddf269a8660192
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/io/Reader.hxx>
21 #include <string.h>
22 #include <osl/diagnose.h>
23 using namespace connectivity;
24 using ::com::sun::star::uno::Sequence;
27 //************ Class: java.io.Reader
30 jclass java_io_Reader::theClass = nullptr;
31 java_io_Reader::java_io_Reader( JNIEnv * pEnv, jobject myObj )
32 : java_lang_Object( pEnv, myObj )
34 SDBThreadAttach::addRef();
36 java_io_Reader::~java_io_Reader()
38 SDBThreadAttach::releaseRef();
41 jclass java_io_Reader::getMyClass() const
43 // the class must be fetched only once, therefore static
44 if( !theClass )
45 theClass = findMyClass("java/io/Reader");
46 return theClass;
49 sal_Int32 SAL_CALL java_io_Reader::readSomeBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
51 return readBytes(aData,nMaxBytesToRead);
54 void SAL_CALL java_io_Reader::skipBytes( sal_Int32 nBytesToSkip )
56 static jmethodID mID(nullptr);
57 if(nBytesToSkip <= 0)
58 return;
60 if(m_buf)
62 m_buf.reset();
63 --nBytesToSkip;
66 static_assert(sizeof(jchar) == 2, "I thought Java characters were UTF16 code units?");
67 sal_Int32 nCharsToSkip = nBytesToSkip / sizeof(jchar);
68 callIntMethodWithIntArg_ThrowRuntime("skip",mID,nCharsToSkip);
69 if(nBytesToSkip % sizeof(jchar) != 0)
71 assert(nBytesToSkip % sizeof(jchar) == 1);
72 Sequence< sal_Int8 > aData(1);
73 assert(m_buf);
74 readBytes(aData, 1);
78 sal_Int32 SAL_CALL java_io_Reader::available( )
80 if(m_buf)
81 return 1;
82 bool out;
83 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
86 static const char * const cSignature = "()Z";
87 static const char * const cMethodName = "ready";
88 // Java-Call
89 static jmethodID mID(nullptr);
90 obtainMethodId_throwRuntime(t.pEnv, cMethodName,cSignature, mID);
91 out = t.pEnv->CallBooleanMethod( object, mID);
92 ThrowRuntimeException(t.pEnv,*this);
93 } //t.pEnv
94 return (m_buf ? 1 : 0) + (out ? 1 : 0); // no way to tell *how much* is ready
97 void SAL_CALL java_io_Reader::closeInput( )
99 static jmethodID mID(nullptr);
100 callVoidMethod_ThrowRuntime("close", mID);
103 sal_Int32 SAL_CALL java_io_Reader::readBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
105 OSL_ENSURE(aData.getLength() >= nBytesToRead," Sequence is smaller than BytesToRead");
107 if(nBytesToRead == 0)
108 return 0;
110 sal_Int8 *dst(aData.getArray());
111 sal_Int32 nBytesWritten(0);
113 if (m_buf)
115 if(!aData.hasElements())
117 aData.realloc(1);
118 dst = aData.getArray();
120 *dst = *m_buf;
121 m_buf.reset();
122 ++nBytesWritten;
123 ++dst;
124 --nBytesToRead;
127 if(nBytesToRead == 0)
128 return nBytesWritten;
130 sal_Int32 nCharsToRead = (nBytesToRead + 1)/2;
132 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
135 jcharArray pCharArray = t.pEnv->NewCharArray(nCharsToRead);
136 static const char * const cSignature = "([CII)I";
137 static const char * const cMethodName = "read";
138 // Java-Call
139 static jmethodID mID(nullptr);
140 obtainMethodId_throwRuntime(t.pEnv, cMethodName,cSignature, mID);
141 jint outChars = t.pEnv->CallIntMethod( object, mID, pCharArray, 0, nCharsToRead );
142 if ( !outChars )
144 if(nBytesWritten==0)
145 ThrowRuntimeException(t.pEnv,*this);
146 else
147 return 1;
149 if(outChars > 0)
151 static_assert(sizeof(jchar) == 2, "I thought Java characters were UTF16 code units?");
152 const sal_Int32 jcs = sizeof(jchar);
153 const sal_Int32 outBytes = std::min(nBytesToRead, outChars*jcs);
154 assert(outBytes == outChars*jcs || outBytes == outChars*jcs - 1);
156 jboolean p = JNI_FALSE;
157 if(aData.getLength() < nBytesWritten + outBytes)
159 aData.realloc(nBytesWritten + outBytes);
160 dst = aData.getArray() + nBytesWritten;
162 jchar *outBuf(t.pEnv->GetCharArrayElements(pCharArray,&p));
164 memcpy(dst, outBuf, outBytes);
165 nBytesWritten += outBytes;
166 if(outBytes < outChars*jcs)
168 assert(outChars*jcs - outBytes == 1);
169 assert(!m_buf);
170 m_buf = reinterpret_cast<char*>(outBuf)[outBytes];
173 t.pEnv->DeleteLocalRef(pCharArray);
174 } //t.pEnv
175 return nBytesWritten;
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */