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