bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / streaming / seekableinput.cxx
blobda3b3690516f6b7b4952ab3e1c4b74ce9cfe45f1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/io/TempFile.hpp>
21 #include <com/sun/star/io/XOutputStream.hpp>
24 #include <comphelper/processfactory.hxx>
25 #include <comphelper/seekableinput.hxx>
27 using namespace ::com::sun::star;
29 namespace comphelper
32 const sal_Int32 nConstBufferSize = 32000;
35 void copyInputToOutput_Impl( const uno::Reference< io::XInputStream >& xIn,
36 const uno::Reference< io::XOutputStream >& xOut )
38 sal_Int32 nRead;
39 uno::Sequence< sal_Int8 > aSequence( nConstBufferSize );
43 nRead = xIn->readBytes( aSequence, nConstBufferSize );
44 if ( nRead < nConstBufferSize )
46 uno::Sequence< sal_Int8 > aTempBuf( aSequence.getConstArray(), nRead );
47 xOut->writeBytes( aTempBuf );
49 else
50 xOut->writeBytes( aSequence );
52 while ( nRead == nConstBufferSize );
56 OSeekableInputWrapper::OSeekableInputWrapper(
57 const uno::Reference< io::XInputStream >& xInStream,
58 const uno::Reference< uno::XComponentContext >& rxContext )
59 : m_xContext( rxContext )
60 , m_xOriginalStream( xInStream )
62 if ( !m_xContext.is() )
63 throw uno::RuntimeException();
67 OSeekableInputWrapper::~OSeekableInputWrapper()
72 uno::Reference< io::XInputStream > OSeekableInputWrapper::CheckSeekableCanWrap(
73 const uno::Reference< io::XInputStream >& xInStream,
74 const uno::Reference< uno::XComponentContext >& rxContext )
76 // check that the stream is seekable and just wrap it if it is not
77 uno::Reference< io::XSeekable > xSeek( xInStream, uno::UNO_QUERY );
78 if ( xSeek.is() )
79 return xInStream;
81 uno::Reference< io::XInputStream > xNewStream(
82 static_cast< io::XInputStream* >(
83 new OSeekableInputWrapper( xInStream, rxContext ) ) );
84 return xNewStream;
88 void OSeekableInputWrapper::PrepareCopy_Impl()
90 if ( !m_xCopyInput.is() )
92 if ( !m_xContext.is() )
93 throw uno::RuntimeException();
95 uno::Reference< io::XOutputStream > xTempOut(
96 io::TempFile::create(m_xContext),
97 uno::UNO_QUERY_THROW );
99 copyInputToOutput_Impl( m_xOriginalStream, xTempOut );
100 xTempOut->closeOutput();
102 uno::Reference< io::XSeekable > xTempSeek( xTempOut, uno::UNO_QUERY );
103 if ( xTempSeek.is() )
105 xTempSeek->seek( 0 );
106 m_xCopyInput = uno::Reference< io::XInputStream >( xTempOut, uno::UNO_QUERY );
107 if ( m_xCopyInput.is() )
108 m_xCopySeek = xTempSeek;
112 if ( !m_xCopyInput.is() )
113 throw io::IOException();
116 // XInputStream
118 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 )
124 ::osl::MutexGuard aGuard( m_aMutex );
126 if ( !m_xOriginalStream.is() )
127 throw io::NotConnectedException();
129 PrepareCopy_Impl();
131 return m_xCopyInput->readBytes( aData, nBytesToRead );
135 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 )
141 ::osl::MutexGuard aGuard( m_aMutex );
143 if ( !m_xOriginalStream.is() )
144 throw io::NotConnectedException();
146 PrepareCopy_Impl();
148 return m_xCopyInput->readSomeBytes( aData, nMaxBytesToRead );
152 void SAL_CALL OSeekableInputWrapper::skipBytes( sal_Int32 nBytesToSkip )
153 throw ( io::NotConnectedException,
154 io::BufferSizeExceededException,
155 io::IOException,
156 uno::RuntimeException, std::exception )
158 ::osl::MutexGuard aGuard( m_aMutex );
160 if ( !m_xOriginalStream.is() )
161 throw io::NotConnectedException();
163 PrepareCopy_Impl();
165 m_xCopyInput->skipBytes( nBytesToSkip );
169 sal_Int32 SAL_CALL OSeekableInputWrapper::available()
170 throw ( io::NotConnectedException,
171 io::IOException,
172 uno::RuntimeException, std::exception )
174 ::osl::MutexGuard aGuard( m_aMutex );
176 if ( !m_xOriginalStream.is() )
177 throw io::NotConnectedException();
179 PrepareCopy_Impl();
181 return m_xCopyInput->available();
185 void SAL_CALL OSeekableInputWrapper::closeInput()
186 throw ( io::NotConnectedException,
187 io::IOException,
188 uno::RuntimeException, std::exception )
190 ::osl::MutexGuard aGuard( m_aMutex );
192 if ( !m_xOriginalStream.is() )
193 throw io::NotConnectedException();
195 m_xOriginalStream->closeInput();
196 m_xOriginalStream = uno::Reference< io::XInputStream >();
198 if ( m_xCopyInput.is() )
200 m_xCopyInput->closeInput();
201 m_xCopyInput = uno::Reference< io::XInputStream >();
204 m_xCopySeek = uno::Reference< io::XSeekable >();
208 // XSeekable
210 void SAL_CALL OSeekableInputWrapper::seek( sal_Int64 location )
211 throw ( lang::IllegalArgumentException,
212 io::IOException,
213 uno::RuntimeException, std::exception )
215 ::osl::MutexGuard aGuard( m_aMutex );
217 if ( !m_xOriginalStream.is() )
218 throw io::NotConnectedException();
220 PrepareCopy_Impl();
222 m_xCopySeek->seek( location );
226 sal_Int64 SAL_CALL OSeekableInputWrapper::getPosition()
227 throw ( io::IOException,
228 uno::RuntimeException, std::exception )
230 ::osl::MutexGuard aGuard( m_aMutex );
232 if ( !m_xOriginalStream.is() )
233 throw io::NotConnectedException();
235 PrepareCopy_Impl();
237 return m_xCopySeek->getPosition();
241 sal_Int64 SAL_CALL OSeekableInputWrapper::getLength()
242 throw ( io::IOException,
243 uno::RuntimeException, std::exception )
245 ::osl::MutexGuard aGuard( m_aMutex );
247 if ( !m_xOriginalStream.is() )
248 throw io::NotConnectedException();
250 PrepareCopy_Impl();
252 return m_xCopySeek->getLength();
255 } // namespace comphelper
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */