bump product version to 6.3.0.0.beta1
[LibreOffice.git] / comphelper / source / streaming / seekableinput.cxx
blobe4f654043dc78c90fb60fb9d0fd2a64625c97114
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 <sal/config.h>
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>
28 #include <comphelper/seekableinput.hxx>
30 using namespace ::com::sun::star;
32 namespace comphelper
35 const sal_Int32 nConstBufferSize = 32000;
38 static void copyInputToOutput_Impl( const uno::Reference< io::XInputStream >& xIn,
39 const uno::Reference< io::XOutputStream >& xOut )
41 sal_Int32 nRead;
42 uno::Sequence< sal_Int8 > aSequence( nConstBufferSize );
46 nRead = xIn->readBytes( aSequence, nConstBufferSize );
47 if ( nRead < nConstBufferSize )
49 uno::Sequence< sal_Int8 > aTempBuf( aSequence.getConstArray(), nRead );
50 xOut->writeBytes( aTempBuf );
52 else
53 xOut->writeBytes( aSequence );
55 while ( nRead == nConstBufferSize );
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 )
65 if ( !m_xContext.is() )
66 throw uno::RuntimeException();
70 OSeekableInputWrapper::~OSeekableInputWrapper()
75 uno::Reference< io::XInputStream > OSeekableInputWrapper::CheckSeekableCanWrap(
76 const uno::Reference< io::XInputStream >& xInStream,
77 const uno::Reference< uno::XComponentContext >& rxContext )
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;
84 return new OSeekableInputWrapper(xInStream, rxContext);
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.set( 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 )
120 ::osl::MutexGuard aGuard( m_aMutex );
122 if ( !m_xOriginalStream.is() )
123 throw io::NotConnectedException();
125 PrepareCopy_Impl();
127 return m_xCopyInput->readBytes( aData, nBytesToRead );
131 sal_Int32 SAL_CALL OSeekableInputWrapper::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
133 ::osl::MutexGuard aGuard( m_aMutex );
135 if ( !m_xOriginalStream.is() )
136 throw io::NotConnectedException();
138 PrepareCopy_Impl();
140 return m_xCopyInput->readSomeBytes( aData, nMaxBytesToRead );
144 void SAL_CALL OSeekableInputWrapper::skipBytes( sal_Int32 nBytesToSkip )
146 ::osl::MutexGuard aGuard( m_aMutex );
148 if ( !m_xOriginalStream.is() )
149 throw io::NotConnectedException();
151 PrepareCopy_Impl();
153 m_xCopyInput->skipBytes( nBytesToSkip );
157 sal_Int32 SAL_CALL OSeekableInputWrapper::available()
159 ::osl::MutexGuard aGuard( m_aMutex );
161 if ( !m_xOriginalStream.is() )
162 throw io::NotConnectedException();
164 PrepareCopy_Impl();
166 return m_xCopyInput->available();
170 void SAL_CALL OSeekableInputWrapper::closeInput()
172 ::osl::MutexGuard aGuard( m_aMutex );
174 if ( !m_xOriginalStream.is() )
175 throw io::NotConnectedException();
177 m_xOriginalStream->closeInput();
178 m_xOriginalStream.clear();
180 if ( m_xCopyInput.is() )
182 m_xCopyInput->closeInput();
183 m_xCopyInput.clear();
186 m_xCopySeek.clear();
190 // XSeekable
192 void SAL_CALL OSeekableInputWrapper::seek( sal_Int64 location )
194 ::osl::MutexGuard aGuard( m_aMutex );
196 if ( !m_xOriginalStream.is() )
197 throw io::NotConnectedException();
199 PrepareCopy_Impl();
201 m_xCopySeek->seek( location );
205 sal_Int64 SAL_CALL OSeekableInputWrapper::getPosition()
207 ::osl::MutexGuard aGuard( m_aMutex );
209 if ( !m_xOriginalStream.is() )
210 throw io::NotConnectedException();
212 PrepareCopy_Impl();
214 return m_xCopySeek->getPosition();
218 sal_Int64 SAL_CALL OSeekableInputWrapper::getLength()
220 ::osl::MutexGuard aGuard( m_aMutex );
222 if ( !m_xOriginalStream.is() )
223 throw io::NotConnectedException();
225 PrepareCopy_Impl();
227 return m_xCopySeek->getLength();
230 } // namespace comphelper
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */