22#include <osl/diagnose.h>
24using ::com::sun::star::uno::Sequence;
30jclass java_io_Reader::theClass =
nullptr;
31java_io_Reader::java_io_Reader( JNIEnv * pEnv, jobject myObj )
56 static jmethodID mID(
nullptr);
66 static_assert(
sizeof(jchar) == 2,
"I thought Java characters were UTF16 code units?");
67 sal_Int32 nCharsToSkip = nBytesToSkip /
sizeof(jchar);
69 if(nBytesToSkip %
sizeof(jchar) != 0)
71 assert(nBytesToSkip %
sizeof(jchar) == 1);
72 Sequence< sal_Int8 >
aData(1);
86 static const char *
const cSignature =
"()Z";
87 static const char *
const cMethodName =
"ready";
89 static jmethodID mID(
nullptr);
91 out =
t.pEnv->CallBooleanMethod(
object, mID);
94 return (
m_buf ? 1 : 0) + (out ? 1 : 0);
99 static jmethodID mID(
nullptr);
105 OSL_ENSURE(
aData.getLength() >= nBytesToRead,
" Sequence is smaller than BytesToRead");
107 if(nBytesToRead == 0)
111 sal_Int32 nBytesWritten(0);
115 if(!
aData.hasElements())
118 dst =
aData.getArray();
127 if(nBytesToRead == 0)
128 return nBytesWritten;
130 sal_Int32 nCharsToRead = (nBytesToRead + 1)/2;
135 jcharArray pCharArray =
t.pEnv->NewCharArray(nCharsToRead);
136 static const char *
const cSignature =
"([CII)I";
137 static const char *
const cMethodName =
"read";
139 static jmethodID mID(
nullptr);
141 jint outChars =
t.pEnv->CallIntMethod(
object, mID, pCharArray, 0, nCharsToRead );
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);
170 m_buf =
reinterpret_cast<char*
>(outBuf)[outBytes];
173 t.pEnv->DeleteLocalRef(pCharArray);
175 return nBytesWritten;
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
virtual ~java_io_Reader() override
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
virtual jclass getMyClass() const override
std::optional< char > m_buf
virtual sal_Int32 SAL_CALL available() override
virtual void SAL_CALL closeInput() override
static jclass findMyClass(const char *_pClassName)
void obtainMethodId_throwRuntime(JNIEnv *_pEnv, const char *_pMethodName, const char *_pSignature, jmethodID &_inout_MethodID) const
static void ThrowRuntimeException(JNIEnv *pEnv, const css::uno::Reference< css::uno::XInterface > &_rContext)
void callVoidMethod_ThrowRuntime(const char *_pMethodName, jmethodID &_inout_MethodID) const
sal_Int32 callIntMethodWithIntArg_ThrowRuntime(const char *_pMethodName, jmethodID &_inout_MethodID, sal_Int32 _nArgument) const
constexpr OUStringLiteral aData