Line data Source code
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 : using namespace connectivity;
23 : using ::com::sun::star::uno::Sequence;
24 :
25 :
26 : //************ Class: java.io.Reader
27 :
28 :
29 : jclass java_io_Reader::theClass = 0;
30 0 : java_io_Reader::java_io_Reader( JNIEnv * pEnv, jobject myObj )
31 0 : : java_lang_Object( pEnv, myObj )
32 : {
33 0 : SDBThreadAttach::addRef();
34 0 : }
35 0 : java_io_Reader::~java_io_Reader()
36 : {
37 0 : SDBThreadAttach::releaseRef();
38 0 : }
39 :
40 0 : jclass java_io_Reader::getMyClass() const
41 : {
42 : // the class must be fetched only once, therefore static
43 0 : if( !theClass )
44 0 : theClass = findMyClass("java/io/Reader");
45 0 : return theClass;
46 : }
47 :
48 0 : sal_Int32 SAL_CALL java_io_Reader::readSomeBytes( ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
49 : {
50 0 : return readBytes(aData,nMaxBytesToRead);
51 : }
52 :
53 0 : void SAL_CALL java_io_Reader::skipBytes( sal_Int32 nBytesToSkip ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
54 : {
55 : static jmethodID mID(NULL);
56 0 : if(nBytesToSkip <= 0)
57 0 : return;
58 :
59 0 : if(m_buf != boost::none)
60 : {
61 0 : m_buf = boost::none;
62 0 : --nBytesToSkip;
63 : }
64 :
65 : static_assert(sizeof(jchar) == 2, "I thought Java characters were UTF16 code units?");
66 0 : sal_Int32 nCharsToSkip = nBytesToSkip / sizeof(jchar);
67 0 : callIntMethodWithIntArg_ThrowRuntime("skip",mID,nCharsToSkip);
68 0 : if(nBytesToSkip % sizeof(jchar) != 0)
69 : {
70 : assert(nBytesToSkip % sizeof(jchar) == 1);
71 0 : Sequence< sal_Int8 > aData(1);
72 : assert(m_buf == boost::none);
73 0 : readBytes(aData, 1);
74 : }
75 : }
76 :
77 0 : sal_Int32 SAL_CALL java_io_Reader::available( ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
78 : {
79 0 : if(m_buf != boost::none)
80 0 : return 1;
81 : jboolean out;
82 0 : SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
83 :
84 : {
85 : static const char * cSignature = "()Z";
86 : static const char * cMethodName = "ready";
87 : // Java-Call
88 : static jmethodID mID(NULL);
89 0 : obtainMethodId_throwRuntime(t.pEnv, cMethodName,cSignature, mID);
90 0 : out = t.pEnv->CallBooleanMethod( object, mID);
91 0 : ThrowRuntimeException(t.pEnv,*this);
92 : } //t.pEnv
93 0 : return (m_buf != boost::none ? 1 : 0) + (out ? 1 : 0); // no way to tell *how much* is ready
94 : }
95 :
96 0 : void SAL_CALL java_io_Reader::closeInput( ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
97 : {
98 : static jmethodID mID(NULL);
99 0 : callVoidMethod_ThrowRuntime("close", mID);
100 0 : }
101 :
102 0 : sal_Int32 SAL_CALL java_io_Reader::readBytes( ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
103 : {
104 : OSL_ENSURE(aData.getLength() >= nBytesToRead," Sequence is smaller than BytesToRead");
105 :
106 0 : if(nBytesToRead == 0)
107 0 : return 0;
108 :
109 0 : sal_Int8 *dst(aData.getArray());
110 0 : sal_Int32 nBytesWritten(0);
111 :
112 0 : if(m_buf != boost::none)
113 : {
114 0 : if(aData.getLength() == 0)
115 : {
116 0 : aData.realloc(1);
117 0 : dst = aData.getArray();
118 : }
119 0 : *dst = *m_buf;
120 0 : m_buf = boost::none;
121 0 : ++nBytesWritten;
122 0 : ++dst;
123 0 : --nBytesToRead;
124 : }
125 :
126 0 : if(nBytesToRead == 0)
127 0 : return 0;
128 :
129 0 : sal_Int32 nCharsToRead = (nBytesToRead + 1)/2;
130 :
131 0 : jint outChars(0);
132 0 : SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java Enviroment geloescht worden!");
133 :
134 : {
135 0 : jcharArray pCharArray = t.pEnv->NewCharArray(nCharsToRead);
136 : static const char * cSignature = "([CII)I";
137 : static const char * cMethodName = "read";
138 : // Java-Call
139 : static jmethodID mID(NULL);
140 0 : obtainMethodId_throwRuntime(t.pEnv, cMethodName,cSignature, mID);
141 0 : outChars = t.pEnv->CallIntMethod( object, mID, pCharArray, 0, nCharsToRead );
142 0 : if ( !outChars )
143 : {
144 0 : if(nBytesWritten==0)
145 0 : ThrowRuntimeException(t.pEnv,*this);
146 : else
147 0 : return 1;
148 : }
149 0 : if(outChars > 0)
150 : {
151 : static_assert(sizeof(jchar) == 2, "I thought Java characters were UTF16 code units?");
152 0 : const sal_Int32 jcs = sizeof(jchar);
153 0 : const sal_Int32 outBytes = std::min(nBytesToRead, outChars*jcs);
154 : assert(outBytes == outChars*jcs || outBytes == outChars*jcs - 1);
155 :
156 0 : jboolean p = JNI_FALSE;
157 0 : if(aData.getLength() < nBytesWritten + outBytes)
158 : {
159 0 : aData.realloc(nBytesWritten + outBytes);
160 0 : dst = aData.getArray() + nBytesWritten;
161 : }
162 0 : jchar *outBuf(t.pEnv->GetCharArrayElements(pCharArray,&p));
163 :
164 0 : memcpy(dst, outBuf, outBytes);
165 0 : nBytesWritten += outBytes;
166 0 : if(outBytes < outChars*jcs)
167 : {
168 : assert(outChars*jcs - outBytes == 1);
169 : assert(m_buf == boost::none);
170 0 : m_buf = reinterpret_cast<char*>(outBuf)[outBytes];
171 : }
172 : }
173 0 : t.pEnv->DeleteLocalRef(pCharArray);
174 : } //t.pEnv
175 0 : return nBytesWritten;
176 : }
177 :
178 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|