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 0 : void copyInputToOutput_Impl( const uno::Reference< io::XInputStream >& xIn,
36 : const uno::Reference< io::XOutputStream >& xOut )
37 : {
38 : sal_Int32 nRead;
39 0 : uno::Sequence< sal_Int8 > aSequence( nConstBufferSize );
40 :
41 0 : do
42 : {
43 0 : nRead = xIn->readBytes( aSequence, nConstBufferSize );
44 0 : if ( nRead < nConstBufferSize )
45 : {
46 0 : uno::Sequence< sal_Int8 > aTempBuf( aSequence.getConstArray(), nRead );
47 0 : xOut->writeBytes( aTempBuf );
48 : }
49 : else
50 0 : xOut->writeBytes( aSequence );
51 : }
52 0 : while ( nRead == nConstBufferSize );
53 0 : }
54 :
55 :
56 0 : OSeekableInputWrapper::OSeekableInputWrapper(
57 : const uno::Reference< io::XInputStream >& xInStream,
58 : const uno::Reference< uno::XComponentContext >& rxContext )
59 : : m_xContext( rxContext )
60 0 : , m_xOriginalStream( xInStream )
61 : {
62 0 : if ( !m_xContext.is() )
63 0 : throw uno::RuntimeException();
64 0 : }
65 :
66 :
67 0 : OSeekableInputWrapper::~OSeekableInputWrapper()
68 : {
69 0 : }
70 :
71 :
72 0 : 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 0 : uno::Reference< io::XSeekable > xSeek( xInStream, uno::UNO_QUERY );
78 0 : if ( xSeek.is() )
79 0 : return xInStream;
80 :
81 : uno::Reference< io::XInputStream > xNewStream(
82 : static_cast< io::XInputStream* >(
83 0 : new OSeekableInputWrapper( xInStream, rxContext ) ) );
84 0 : return xNewStream;
85 : }
86 :
87 :
88 0 : void OSeekableInputWrapper::PrepareCopy_Impl()
89 : {
90 0 : if ( !m_xCopyInput.is() )
91 : {
92 0 : if ( !m_xContext.is() )
93 0 : throw uno::RuntimeException();
94 :
95 : uno::Reference< io::XOutputStream > xTempOut(
96 : io::TempFile::create(m_xContext),
97 0 : uno::UNO_QUERY_THROW );
98 :
99 0 : copyInputToOutput_Impl( m_xOriginalStream, xTempOut );
100 0 : xTempOut->closeOutput();
101 :
102 0 : uno::Reference< io::XSeekable > xTempSeek( xTempOut, uno::UNO_QUERY );
103 0 : if ( xTempSeek.is() )
104 : {
105 0 : xTempSeek->seek( 0 );
106 0 : m_xCopyInput = uno::Reference< io::XInputStream >( xTempOut, uno::UNO_QUERY );
107 0 : if ( m_xCopyInput.is() )
108 0 : m_xCopySeek = xTempSeek;
109 0 : }
110 : }
111 :
112 0 : if ( !m_xCopyInput.is() )
113 0 : throw io::IOException();
114 0 : }
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 0 : 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 0 : ::osl::MutexGuard aGuard( m_aMutex );
142 :
143 0 : if ( !m_xOriginalStream.is() )
144 0 : throw io::NotConnectedException();
145 :
146 0 : PrepareCopy_Impl();
147 :
148 0 : 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 0 : void SAL_CALL OSeekableInputWrapper::seek( sal_Int64 location )
211 : throw ( lang::IllegalArgumentException,
212 : io::IOException,
213 : uno::RuntimeException, std::exception )
214 : {
215 0 : ::osl::MutexGuard aGuard( m_aMutex );
216 :
217 0 : if ( !m_xOriginalStream.is() )
218 0 : throw io::NotConnectedException();
219 :
220 0 : PrepareCopy_Impl();
221 :
222 0 : m_xCopySeek->seek( location );
223 0 : }
224 :
225 :
226 0 : sal_Int64 SAL_CALL OSeekableInputWrapper::getPosition()
227 : throw ( io::IOException,
228 : uno::RuntimeException, std::exception )
229 : {
230 0 : ::osl::MutexGuard aGuard( m_aMutex );
231 :
232 0 : if ( !m_xOriginalStream.is() )
233 0 : throw io::NotConnectedException();
234 :
235 0 : PrepareCopy_Impl();
236 :
237 0 : return m_xCopySeek->getPosition();
238 : }
239 :
240 :
241 0 : sal_Int64 SAL_CALL OSeekableInputWrapper::getLength()
242 : throw ( io::IOException,
243 : uno::RuntimeException, std::exception )
244 : {
245 0 : ::osl::MutexGuard aGuard( m_aMutex );
246 :
247 0 : if ( !m_xOriginalStream.is() )
248 0 : throw io::NotConnectedException();
249 :
250 0 : PrepareCopy_Impl();
251 :
252 0 : return m_xCopySeek->getLength();
253 : }
254 :
255 : } // namespace comphelper
256 :
257 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|