LibreOffice Module connectivity (master) 1
InputStream.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 <sal/config.h>
21#include <sal/log.hxx>
22
23#include <com/sun/star/io/BufferSizeExceededException.hpp>
25#include <osl/diagnose.h>
26
27#include <string.h>
28
29using namespace connectivity;
30
31#if OSL_DEBUG_LEVEL > 0
32#define THROW_WHERE SAL_WHERE
33#else
34#define THROW_WHERE ""
35#endif
36
37
38//************ Class: java.io.InputStream
39
40
41jclass java_io_InputStream::theClass = nullptr;
42java_io_InputStream::java_io_InputStream( JNIEnv * pEnv, jobject myObj )
43 : java_lang_Object( pEnv, myObj )
44{
46}
48{
50}
51
53{
54 // the class must be fetched only once, therefore static
55 if( !theClass )
56 theClass = findMyClass("java/io/InputStream");
57 return theClass;
58}
59
60
61sal_Int32 SAL_CALL java_io_InputStream::readSomeBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
62{
63 return readBytes(aData,nMaxBytesToRead);
64}
65
66void SAL_CALL java_io_InputStream::skipBytes( sal_Int32 nBytesToSkip )
67{
68 static jmethodID mID(nullptr);
69 callIntMethodWithIntArg_ThrowRuntime("skip",mID,nBytesToSkip);
70}
71
72sal_Int32 SAL_CALL java_io_InputStream::available( )
73{
74 static jmethodID mID(nullptr);
75 return callIntMethod_ThrowRuntime("available", mID);
76}
77
79{
80 static jmethodID mID(nullptr);
81 callVoidMethod_ThrowRuntime("close",mID);
82}
83
84sal_Int32 SAL_CALL java_io_InputStream::readBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
85{
86 if (nBytesToRead < 0)
87 throw css::io::BufferSizeExceededException( THROW_WHERE, *this );
88
89 jint out(0);
90 SDBThreadAttach t; OSL_ENSURE(t.pEnv,"Java environment has been deleted!");
91
92 {
93 jbyteArray pByteArray = t.pEnv->NewByteArray(nBytesToRead);
94 static const char * const cSignature = "([BII)I";
95 static const char * const cMethodName = "read";
96 // execute Java-Call
97 static jmethodID mID(nullptr);
98 obtainMethodId_throwRuntime(t.pEnv, cMethodName,cSignature, mID);
99 out = t.pEnv->CallIntMethod( object, mID, pByteArray, 0, nBytesToRead );
100 if ( !out )
101 ThrowRuntimeException(t.pEnv,*this);
102 if(out > 0)
103 {
104 jboolean p = false;
105 aData.realloc ( out );
106 memcpy(aData.getArray(),t.pEnv->GetByteArrayElements(pByteArray,&p),out);
107 }
108 t.pEnv->DeleteLocalRef(pByteArray);
109 } //t.pEnv
110 return out;
111}
112
113/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define THROW_WHERE
Definition: InputStream.cxx:32
XPropertyListType t
static void releaseRef()
Definition: Object.cxx:81
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
Definition: InputStream.cxx:66
virtual sal_Int32 SAL_CALL available() override
Definition: InputStream.cxx:72
virtual jclass getMyClass() const override
Definition: InputStream.cxx:52
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
Definition: InputStream.cxx:84
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
Definition: InputStream.cxx:61
virtual void SAL_CALL closeInput() override
Definition: InputStream.cxx:78
virtual ~java_io_InputStream() override
Definition: InputStream.cxx:47
sal_Int32 callIntMethod_ThrowRuntime(const char *_pMethodName, jmethodID &_inout_MethodID) const
Definition: Object.cxx:297
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