LibreOffice Module connectivity (master) 1
Reader.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/io/Reader.hxx>
21#include <string.h>
22#include <osl/diagnose.h>
23using namespace connectivity;
24using ::com::sun::star::uno::Sequence;
25
26
27//************ Class: java.io.Reader
28
29
30jclass java_io_Reader::theClass = nullptr;
31java_io_Reader::java_io_Reader( JNIEnv * pEnv, jobject myObj )
32 : java_lang_Object( pEnv, myObj )
33{
35}
37{
39}
40
42{
43 // the class must be fetched only once, therefore static
44 if( !theClass )
45 theClass = findMyClass("java/io/Reader");
46 return theClass;
47}
48
49sal_Int32 SAL_CALL java_io_Reader::readSomeBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
50{
51 return readBytes(aData,nMaxBytesToRead);
52}
53
54void SAL_CALL java_io_Reader::skipBytes( sal_Int32 nBytesToSkip )
55{
56 static jmethodID mID(nullptr);
57 if(nBytesToSkip <= 0)
58 return;
59
60 if(m_buf)
61 {
62 m_buf.reset();
63 --nBytesToSkip;
64 }
65
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)
70 {
71 assert(nBytesToSkip % sizeof(jchar) == 1);
72 Sequence< sal_Int8 > aData(1);
73 assert(m_buf);
74 readBytes(aData, 1);
75 }
76}
77
78sal_Int32 SAL_CALL java_io_Reader::available( )
79{
80 if(m_buf)
81 return 1;
82 bool out;
83 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
84
85 {
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
95}
96
98{
99 static jmethodID mID(nullptr);
100 callVoidMethod_ThrowRuntime("close", mID);
101}
102
103sal_Int32 SAL_CALL java_io_Reader::readBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
104{
105 OSL_ENSURE(aData.getLength() >= nBytesToRead," Sequence is smaller than BytesToRead");
106
107 if(nBytesToRead == 0)
108 return 0;
109
110 sal_Int8 *dst(aData.getArray());
111 sal_Int32 nBytesWritten(0);
112
113 if (m_buf)
114 {
115 if(!aData.hasElements())
116 {
117 aData.realloc(1);
118 dst = aData.getArray();
119 }
120 *dst = *m_buf;
121 m_buf.reset();
122 ++nBytesWritten;
123 ++dst;
124 --nBytesToRead;
125 }
126
127 if(nBytesToRead == 0)
128 return nBytesWritten;
129
130 sal_Int32 nCharsToRead = (nBytesToRead + 1)/2;
131
132 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
133
134 {
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 )
143 {
144 if(nBytesWritten==0)
145 ThrowRuntimeException(t.pEnv,*this);
146 else
147 return 1;
148 }
149 if(outChars > 0)
150 {
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);
155
156 jboolean p = JNI_FALSE;
157 if(aData.getLength() < nBytesWritten + outBytes)
158 {
159 aData.realloc(nBytesWritten + outBytes);
160 dst = aData.getArray() + nBytesWritten;
161 }
162 jchar *outBuf(t.pEnv->GetCharArrayElements(pCharArray,&p));
163
164 memcpy(dst, outBuf, outBytes);
165 nBytesWritten += outBytes;
166 if(outBytes < outChars*jcs)
167 {
168 assert(outChars*jcs - outBytes == 1);
169 assert(!m_buf);
170 m_buf = reinterpret_cast<char*>(outBuf)[outBytes];
171 }
172 }
173 t.pEnv->DeleteLocalRef(pCharArray);
174 } //t.pEnv
175 return nBytesWritten;
176}
177
178/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
static void releaseRef()
Definition: Object.cxx:81
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
Definition: Reader.cxx:54
virtual ~java_io_Reader() override
Definition: Reader.cxx:36
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
Definition: Reader.cxx:49
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
Definition: Reader.cxx:103
virtual jclass getMyClass() const override
Definition: Reader.cxx:41
std::optional< char > m_buf
Definition: Reader.hxx:38
virtual sal_Int32 SAL_CALL available() override
Definition: Reader.cxx:78
virtual void SAL_CALL closeInput() override
Definition: Reader.cxx:97
static jclass theClass
Definition: Reader.hxx:36
static jclass findMyClass(const char *_pClassName)
Definition: Object.cxx:466
void obtainMethodId_throwRuntime(JNIEnv *_pEnv, const char *_pMethodName, const char *_pSignature, jmethodID &_inout_MethodID) const
Definition: Object.cxx:240
static void ThrowRuntimeException(JNIEnv *pEnv, const css::uno::Reference< css::uno::XInterface > &_rContext)
Definition: Object.cxx:215
void callVoidMethod_ThrowRuntime(const char *_pMethodName, jmethodID &_inout_MethodID) const
Definition: Object.cxx:341
sal_Int32 callIntMethodWithIntArg_ThrowRuntime(const char *_pMethodName, jmethodID &_inout_MethodID, sal_Int32 _nArgument) const
Definition: Object.cxx:319
void * p
constexpr OUStringLiteral aData
signed char sal_Int8