tdf#161775 sfx2: SfxRedactionHelper: actually use font for layout
[LibreOffice.git] / comphelper / source / streaming / seekableinput.cxx
bloba90df21e3d80b8663b310a46b771960a71ba6797
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>
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 #include <comphelper/seekableinput.hxx>
30 #include <utility>
32 using namespace ::com::sun::star;
34 namespace comphelper
37 const sal_Int32 nConstBufferSize = 32000;
40 static void copyInputToOutput_Impl( const uno::Reference< io::XInputStream >& xIn,
41 const uno::Reference< io::XOutputStream >& xOut )
43 sal_Int32 nRead;
44 uno::Sequence< sal_Int8 > aSequence( nConstBufferSize );
48 nRead = xIn->readBytes( aSequence, nConstBufferSize );
49 if ( nRead < nConstBufferSize )
51 uno::Sequence< sal_Int8 > aTempBuf( aSequence.getConstArray(), nRead );
52 xOut->writeBytes( aTempBuf );
54 else
55 xOut->writeBytes( aSequence );
57 while ( nRead == nConstBufferSize );
61 OSeekableInputWrapper::OSeekableInputWrapper(
62 uno::Reference< io::XInputStream > xInStream,
63 uno::Reference< uno::XComponentContext > xContext )
64 : m_xContext(std::move( xContext ))
65 , m_xOriginalStream(std::move( xInStream ))
67 if ( !m_xContext.is() )
68 throw lang::IllegalArgumentException(u"no component context"_ustr, *this, 1);
72 OSeekableInputWrapper::~OSeekableInputWrapper()
77 uno::Reference< io::XInputStream > OSeekableInputWrapper::CheckSeekableCanWrap(
78 const uno::Reference< io::XInputStream >& xInStream,
79 const uno::Reference< uno::XComponentContext >& rxContext )
81 // check that the stream is seekable and just wrap it if it is not
82 uno::Reference< io::XSeekable > xSeek( xInStream, uno::UNO_QUERY );
83 if ( xSeek.is() )
84 return xInStream;
86 return new OSeekableInputWrapper(xInStream, rxContext);
90 void OSeekableInputWrapper::PrepareCopy_Impl()
92 if ( !m_xCopyInput.is() )
94 if ( !m_xContext.is() )
95 throw uno::RuntimeException(u"no component context"_ustr);
97 uno::Reference< io::XOutputStream > xTempOut(
98 io::TempFile::create(m_xContext),
99 uno::UNO_QUERY_THROW );
101 copyInputToOutput_Impl( m_xOriginalStream, xTempOut );
102 xTempOut->closeOutput();
104 uno::Reference< io::XSeekable > xTempSeek( xTempOut, uno::UNO_QUERY );
105 if ( xTempSeek.is() )
107 xTempSeek->seek( 0 );
108 m_xCopyInput.set( xTempOut, uno::UNO_QUERY );
109 if ( m_xCopyInput.is() )
111 m_xCopySeek = std::move(xTempSeek);
112 m_pCopyByteReader = dynamic_cast<comphelper::ByteReader*>(xTempOut.get());
113 assert(m_pCopyByteReader);
118 if ( !m_xCopyInput.is() )
119 throw io::IOException(u"no m_xCopyInput"_ustr);
122 // XInputStream
124 sal_Int32 SAL_CALL OSeekableInputWrapper::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
126 std::scoped_lock aGuard( m_aMutex );
128 if ( !m_xOriginalStream.is() )
129 throw io::NotConnectedException();
131 PrepareCopy_Impl();
133 return m_xCopyInput->readBytes( aData, nBytesToRead );
137 sal_Int32 SAL_CALL OSeekableInputWrapper::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
139 std::scoped_lock aGuard( m_aMutex );
141 if ( !m_xOriginalStream.is() )
142 throw io::NotConnectedException();
144 PrepareCopy_Impl();
146 return m_xCopyInput->readSomeBytes( aData, nMaxBytesToRead );
149 sal_Int32 OSeekableInputWrapper::readSomeBytes( sal_Int8* aData, sal_Int32 nMaxBytesToRead )
151 std::scoped_lock aGuard( m_aMutex );
153 if ( !m_xOriginalStream.is() )
154 throw io::NotConnectedException();
156 PrepareCopy_Impl();
158 return m_pCopyByteReader->readSomeBytes( aData, nMaxBytesToRead );
162 void SAL_CALL OSeekableInputWrapper::skipBytes( sal_Int32 nBytesToSkip )
164 std::scoped_lock aGuard( m_aMutex );
166 if ( !m_xOriginalStream.is() )
167 throw io::NotConnectedException();
169 PrepareCopy_Impl();
171 m_xCopyInput->skipBytes( nBytesToSkip );
175 sal_Int32 SAL_CALL OSeekableInputWrapper::available()
177 std::scoped_lock aGuard( m_aMutex );
179 if ( !m_xOriginalStream.is() )
180 throw io::NotConnectedException();
182 PrepareCopy_Impl();
184 return m_xCopyInput->available();
188 void SAL_CALL OSeekableInputWrapper::closeInput()
190 std::scoped_lock aGuard( m_aMutex );
192 if ( !m_xOriginalStream.is() )
193 throw io::NotConnectedException();
195 m_xOriginalStream->closeInput();
196 m_xOriginalStream.clear();
198 if ( m_xCopyInput.is() )
200 m_xCopyInput->closeInput();
201 m_xCopyInput.clear();
204 m_xCopySeek.clear();
205 m_pCopyByteReader = nullptr;
209 // XSeekable
211 void SAL_CALL OSeekableInputWrapper::seek( sal_Int64 location )
213 std::scoped_lock aGuard( m_aMutex );
215 if ( !m_xOriginalStream.is() )
216 throw io::NotConnectedException();
218 PrepareCopy_Impl();
220 m_xCopySeek->seek( location );
224 sal_Int64 SAL_CALL OSeekableInputWrapper::getPosition()
226 std::scoped_lock aGuard( m_aMutex );
228 if ( !m_xOriginalStream.is() )
229 throw io::NotConnectedException();
231 PrepareCopy_Impl();
233 return m_xCopySeek->getPosition();
237 sal_Int64 SAL_CALL OSeekableInputWrapper::getLength()
239 std::scoped_lock aGuard( m_aMutex );
241 if ( !m_xOriginalStream.is() )
242 throw io::NotConnectedException();
244 PrepareCopy_Impl();
246 return m_xCopySeek->getLength();
249 } // namespace comphelper
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */