1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
31 using namespace ::com::sun::star
;
36 const sal_Int32 nConstBufferSize
= 32000;
39 static void copyInputToOutput_Impl( const uno::Reference
< io::XInputStream
>& xIn
,
40 const uno::Reference
< io::XOutputStream
>& xOut
)
43 uno::Sequence
< sal_Int8
> aSequence( nConstBufferSize
);
47 nRead
= xIn
->readBytes( aSequence
, nConstBufferSize
);
48 if ( nRead
< nConstBufferSize
)
50 uno::Sequence
< sal_Int8
> aTempBuf( aSequence
.getConstArray(), nRead
);
51 xOut
->writeBytes( aTempBuf
);
54 xOut
->writeBytes( aSequence
);
56 while ( nRead
== nConstBufferSize
);
60 OSeekableInputWrapper::OSeekableInputWrapper(
61 uno::Reference
< io::XInputStream
> xInStream
,
62 uno::Reference
< uno::XComponentContext
> xContext
)
63 : m_xContext(std::move( xContext
))
64 , m_xOriginalStream(std::move( xInStream
))
66 if ( !m_xContext
.is() )
67 throw uno::RuntimeException();
71 OSeekableInputWrapper::~OSeekableInputWrapper()
76 uno::Reference
< io::XInputStream
> OSeekableInputWrapper::CheckSeekableCanWrap(
77 const uno::Reference
< io::XInputStream
>& xInStream
,
78 const uno::Reference
< uno::XComponentContext
>& rxContext
)
80 // check that the stream is seekable and just wrap it if it is not
81 uno::Reference
< io::XSeekable
> xSeek( xInStream
, uno::UNO_QUERY
);
85 return new OSeekableInputWrapper(xInStream
, rxContext
);
89 void OSeekableInputWrapper::PrepareCopy_Impl()
91 if ( !m_xCopyInput
.is() )
93 if ( !m_xContext
.is() )
94 throw uno::RuntimeException();
96 uno::Reference
< io::XOutputStream
> xTempOut(
97 io::TempFile::create(m_xContext
),
98 uno::UNO_QUERY_THROW
);
100 copyInputToOutput_Impl( m_xOriginalStream
, xTempOut
);
101 xTempOut
->closeOutput();
103 uno::Reference
< io::XSeekable
> xTempSeek( xTempOut
, uno::UNO_QUERY
);
104 if ( xTempSeek
.is() )
106 xTempSeek
->seek( 0 );
107 m_xCopyInput
.set( xTempOut
, uno::UNO_QUERY
);
108 if ( m_xCopyInput
.is() )
109 m_xCopySeek
= xTempSeek
;
113 if ( !m_xCopyInput
.is() )
114 throw io::IOException("no m_xCopyInput");
119 sal_Int32 SAL_CALL
OSeekableInputWrapper::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
121 std::scoped_lock
aGuard( m_aMutex
);
123 if ( !m_xOriginalStream
.is() )
124 throw io::NotConnectedException();
128 return m_xCopyInput
->readBytes( aData
, nBytesToRead
);
132 sal_Int32 SAL_CALL
OSeekableInputWrapper::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
134 std::scoped_lock
aGuard( m_aMutex
);
136 if ( !m_xOriginalStream
.is() )
137 throw io::NotConnectedException();
141 return m_xCopyInput
->readSomeBytes( aData
, nMaxBytesToRead
);
145 void SAL_CALL
OSeekableInputWrapper::skipBytes( sal_Int32 nBytesToSkip
)
147 std::scoped_lock
aGuard( m_aMutex
);
149 if ( !m_xOriginalStream
.is() )
150 throw io::NotConnectedException();
154 m_xCopyInput
->skipBytes( nBytesToSkip
);
158 sal_Int32 SAL_CALL
OSeekableInputWrapper::available()
160 std::scoped_lock
aGuard( m_aMutex
);
162 if ( !m_xOriginalStream
.is() )
163 throw io::NotConnectedException();
167 return m_xCopyInput
->available();
171 void SAL_CALL
OSeekableInputWrapper::closeInput()
173 std::scoped_lock
aGuard( m_aMutex
);
175 if ( !m_xOriginalStream
.is() )
176 throw io::NotConnectedException();
178 m_xOriginalStream
->closeInput();
179 m_xOriginalStream
.clear();
181 if ( m_xCopyInput
.is() )
183 m_xCopyInput
->closeInput();
184 m_xCopyInput
.clear();
193 void SAL_CALL
OSeekableInputWrapper::seek( sal_Int64 location
)
195 std::scoped_lock
aGuard( m_aMutex
);
197 if ( !m_xOriginalStream
.is() )
198 throw io::NotConnectedException();
202 m_xCopySeek
->seek( location
);
206 sal_Int64 SAL_CALL
OSeekableInputWrapper::getPosition()
208 std::scoped_lock
aGuard( m_aMutex
);
210 if ( !m_xOriginalStream
.is() )
211 throw io::NotConnectedException();
215 return m_xCopySeek
->getPosition();
219 sal_Int64 SAL_CALL
OSeekableInputWrapper::getLength()
221 std::scoped_lock
aGuard( m_aMutex
);
223 if ( !m_xOriginalStream
.is() )
224 throw io::NotConnectedException();
228 return m_xCopySeek
->getLength();
231 } // namespace comphelper
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */