1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
45 theClass
= findMyClass("java/io/Reader");
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);
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);
78 sal_Int32 SAL_CALL
java_io_Reader::available( )
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";
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);
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)
110 sal_Int8
*dst(aData
.getArray());
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;
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";
139 static jmethodID
mID(nullptr);
140 obtainMethodId_throwRuntime(t
.pEnv
, cMethodName
,cSignature
, mID
);
141 jint outChars
= t
.pEnv
->CallIntMethod( object
, mID
, pCharArray
, 0, nCharsToRead
);
145 ThrowRuntimeException(t
.pEnv
,*this);
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
;
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */