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