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>
21 #include <sal/log.hxx>
23 #include <com/sun/star/io/IOException.hpp>
25 #include <osl/diagnose.h>
27 #include "wrapstreamforshare.hxx"
29 using namespace ::com::sun::star
;
31 #if OSL_DEBUG_LEVEL > 0
32 #define THROW_WHERE SAL_WHERE
34 #define THROW_WHERE ""
37 WrapStreamForShare::WrapStreamForShare( uno::Reference
< io::XInputStream
> xInStream
,
38 rtl::Reference
< comphelper::RefCountedMutex
> xMutexRef
)
39 : m_xMutex(std::move( xMutexRef
))
40 , m_xInStream(std::move( xInStream
))
43 if ( !m_xMutex
.is() || !m_xInStream
.is() )
45 OSL_FAIL( "Wrong initialization of wrapping stream!" );
46 throw uno::RuntimeException(THROW_WHERE
);
48 m_xSeekable
.set( m_xInStream
, uno::UNO_QUERY_THROW
);
49 mpByteReader
= dynamic_cast<comphelper::ByteReader
*>(m_xInStream
.get());
53 WrapStreamForShare::~WrapStreamForShare()
58 sal_Int32 SAL_CALL
WrapStreamForShare::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
60 if ( !m_xInStream
.is() )
61 throw io::IOException(THROW_WHERE
);
63 m_xSeekable
->seek( m_nCurPos
);
65 sal_Int32 nRead
= m_xInStream
->readBytes( aData
, nBytesToRead
);
71 sal_Int32 SAL_CALL
WrapStreamForShare::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
73 if ( !m_xInStream
.is() )
74 throw io::IOException(THROW_WHERE
);
76 m_xSeekable
->seek( m_nCurPos
);
78 sal_Int32 nRead
= m_xInStream
->readSomeBytes( aData
, nMaxBytesToRead
);
84 sal_Int32
WrapStreamForShare::readSomeBytes( sal_Int8
* aData
, sal_Int32 nMaxBytesToRead
)
86 if ( !m_xInStream
.is() )
87 throw io::IOException(THROW_WHERE
);
89 m_xSeekable
->seek( m_nCurPos
);
91 sal_Int32 nRead
= mpByteReader
->readSomeBytes( aData
, nMaxBytesToRead
);
97 void SAL_CALL
WrapStreamForShare::skipBytes( sal_Int32 nBytesToSkip
)
99 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
101 if ( !m_xInStream
.is() )
102 throw io::IOException(THROW_WHERE
);
104 m_xSeekable
->seek( m_nCurPos
);
106 m_xInStream
->skipBytes( nBytesToSkip
);
107 m_nCurPos
= m_xSeekable
->getPosition();
110 sal_Int32 SAL_CALL
WrapStreamForShare::available()
112 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
114 if ( !m_xInStream
.is() )
115 throw io::IOException(THROW_WHERE
);
117 return m_xInStream
->available();
120 void SAL_CALL
WrapStreamForShare::closeInput()
122 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
124 if ( !m_xInStream
.is() )
125 throw io::IOException(THROW_WHERE
);
127 // the package is the owner so it will close the stream
128 // m_xInStream->closeInput();
131 mpByteReader
= nullptr;
135 void SAL_CALL
WrapStreamForShare::seek( sal_Int64 location
)
137 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
139 if ( !m_xInStream
.is() )
140 throw io::IOException(THROW_WHERE
);
142 // let stream implementation do all the checking
143 m_xSeekable
->seek( location
);
145 m_nCurPos
= m_xSeekable
->getPosition();
148 sal_Int64 SAL_CALL
WrapStreamForShare::getPosition()
150 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
152 if ( !m_xInStream
.is() )
153 throw io::IOException(THROW_WHERE
);
158 sal_Int64 SAL_CALL
WrapStreamForShare::getLength()
160 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
162 if ( !m_xInStream
.is() )
163 throw io::IOException(THROW_WHERE
);
165 return m_xSeekable
->getLength();
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */