LibreOffice Module comphelper (master) 1
seekableinput.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
22#include <com/sun/star/io/IOException.hpp>
23#include <com/sun/star/io/NotConnectedException.hpp>
24#include <com/sun/star/io/TempFile.hpp>
25#include <com/sun/star/io/XOutputStream.hpp>
26
27
29#include <utility>
30
31using namespace ::com::sun::star;
32
33namespace comphelper
34{
35
36const sal_Int32 nConstBufferSize = 32000;
37
38
39static void copyInputToOutput_Impl( const uno::Reference< io::XInputStream >& xIn,
40 const uno::Reference< io::XOutputStream >& xOut )
41{
42 sal_Int32 nRead;
43 uno::Sequence< sal_Int8 > aSequence( nConstBufferSize );
44
45 do
46 {
47 nRead = xIn->readBytes( aSequence, nConstBufferSize );
48 if ( nRead < nConstBufferSize )
49 {
50 uno::Sequence< sal_Int8 > aTempBuf( aSequence.getConstArray(), nRead );
51 xOut->writeBytes( aTempBuf );
52 }
53 else
54 xOut->writeBytes( aSequence );
55 }
56 while ( nRead == nConstBufferSize );
57}
58
59
61 uno::Reference< io::XInputStream > xInStream,
62 uno::Reference< uno::XComponentContext > xContext )
63: m_xContext(std::move( xContext ))
64, m_xOriginalStream(std::move( xInStream ))
65{
66 if ( !m_xContext.is() )
67 throw uno::RuntimeException();
68}
69
70
72{
73}
74
75
76uno::Reference< io::XInputStream > OSeekableInputWrapper::CheckSeekableCanWrap(
77 const uno::Reference< io::XInputStream >& xInStream,
78 const uno::Reference< uno::XComponentContext >& rxContext )
79{
80 // check that the stream is seekable and just wrap it if it is not
81 uno::Reference< io::XSeekable > xSeek( xInStream, uno::UNO_QUERY );
82 if ( xSeek.is() )
83 return xInStream;
84
85 return new OSeekableInputWrapper(xInStream, rxContext);
86}
87
88
90{
91 if ( !m_xCopyInput.is() )
92 {
93 if ( !m_xContext.is() )
94 throw uno::RuntimeException();
95
96 uno::Reference< io::XOutputStream > xTempOut(
97 io::TempFile::create(m_xContext),
98 uno::UNO_QUERY_THROW );
99
101 xTempOut->closeOutput();
102
103 uno::Reference< io::XSeekable > xTempSeek( xTempOut, uno::UNO_QUERY );
104 if ( xTempSeek.is() )
105 {
106 xTempSeek->seek( 0 );
107 m_xCopyInput.set( xTempOut, uno::UNO_QUERY );
108 if ( m_xCopyInput.is() )
109 m_xCopySeek = xTempSeek;
110 }
111 }
112
113 if ( !m_xCopyInput.is() )
114 throw io::IOException("no m_xCopyInput");
115}
116
117// XInputStream
118
119sal_Int32 SAL_CALL OSeekableInputWrapper::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
120{
121 std::scoped_lock aGuard( m_aMutex );
122
123 if ( !m_xOriginalStream.is() )
124 throw io::NotConnectedException();
125
127
128 return m_xCopyInput->readBytes( aData, nBytesToRead );
129}
130
131
132sal_Int32 SAL_CALL OSeekableInputWrapper::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
133{
134 std::scoped_lock aGuard( m_aMutex );
135
136 if ( !m_xOriginalStream.is() )
137 throw io::NotConnectedException();
138
140
141 return m_xCopyInput->readSomeBytes( aData, nMaxBytesToRead );
142}
143
144
145void SAL_CALL OSeekableInputWrapper::skipBytes( sal_Int32 nBytesToSkip )
146{
147 std::scoped_lock aGuard( m_aMutex );
148
149 if ( !m_xOriginalStream.is() )
150 throw io::NotConnectedException();
151
153
154 m_xCopyInput->skipBytes( nBytesToSkip );
155}
156
157
159{
160 std::scoped_lock aGuard( m_aMutex );
161
162 if ( !m_xOriginalStream.is() )
163 throw io::NotConnectedException();
164
166
167 return m_xCopyInput->available();
168}
169
170
172{
173 std::scoped_lock aGuard( m_aMutex );
174
175 if ( !m_xOriginalStream.is() )
176 throw io::NotConnectedException();
177
178 m_xOriginalStream->closeInput();
179 m_xOriginalStream.clear();
180
181 if ( m_xCopyInput.is() )
182 {
183 m_xCopyInput->closeInput();
184 m_xCopyInput.clear();
185 }
186
187 m_xCopySeek.clear();
188}
189
190
191// XSeekable
192
193void SAL_CALL OSeekableInputWrapper::seek( sal_Int64 location )
194{
195 std::scoped_lock aGuard( m_aMutex );
196
197 if ( !m_xOriginalStream.is() )
198 throw io::NotConnectedException();
199
201
202 m_xCopySeek->seek( location );
203}
204
205
207{
208 std::scoped_lock aGuard( m_aMutex );
209
210 if ( !m_xOriginalStream.is() )
211 throw io::NotConnectedException();
212
214
215 return m_xCopySeek->getPosition();
216}
217
218
220{
221 std::scoped_lock aGuard( m_aMutex );
222
223 if ( !m_xOriginalStream.is() )
224 throw io::NotConnectedException();
225
227
228 return m_xCopySeek->getLength();
229}
230
231} // namespace comphelper
232
233/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
OSeekableInputWrapper(css::uno::Reference< css::io::XInputStream > xInStream, css::uno::Reference< css::uno::XComponentContext > xContext)
css::uno::Reference< css::io::XSeekable > m_xCopySeek
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual sal_Int64 SAL_CALL getPosition() override
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
COMPHELPER_DLLPRIVATE void PrepareCopy_Impl()
static css::uno::Reference< css::io::XInputStream > CheckSeekableCanWrap(const css::uno::Reference< css::io::XInputStream > &xInStream, const css::uno::Reference< css::uno::XComponentContext > &rxContext)
virtual ~OSeekableInputWrapper() override
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
css::uno::Reference< css::io::XInputStream > m_xOriginalStream
virtual sal_Int32 SAL_CALL available() override
virtual sal_Int64 SAL_CALL getLength() override
css::uno::Reference< css::io::XInputStream > m_xCopyInput
virtual void SAL_CALL seek(sal_Int64 location) override
virtual void SAL_CALL closeInput() override
constexpr OUStringLiteral aData
static void copyInputToOutput_Impl(const uno::Reference< io::XInputStream > &xIn, const uno::Reference< io::XOutputStream > &xOut)
const sal_Int32 nConstBufferSize