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