Fix GNU C++ version check
[LibreOffice.git] / package / source / zippackage / wrapstreamforshare.cxx
blob026aee989b013e8b14aafd39ffce20adfb3c2869
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>
21 #include <sal/log.hxx>
23 #include <com/sun/star/io/IOException.hpp>
24 #include <utility>
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
33 #else
34 #define THROW_WHERE ""
35 #endif
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 ))
41 , m_nCurPos( 0 )
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());
50 assert(mpByteReader);
53 WrapStreamForShare::~WrapStreamForShare()
57 // XInputStream
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 );
66 m_nCurPos += nRead;
68 return nRead;
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 );
79 m_nCurPos += nRead;
81 return nRead;
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 );
92 m_nCurPos += nRead;
94 return nRead;
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();
129 m_xInStream.clear();
130 m_xSeekable.clear();
131 mpByteReader = nullptr;
134 // XSeekable
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 );
155 return m_nCurPos;
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: */