Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / package / source / zippackage / wrapstreamforshare.cxx
blob62734f9240e38f842d62d858e2df6a12decda462
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 <osl/diagnose.h>
25 #include "wrapstreamforshare.hxx"
27 using namespace ::com::sun::star;
29 #if OSL_DEBUG_LEVEL > 0
30 #define THROW_WHERE SAL_WHERE
31 #else
32 #define THROW_WHERE ""
33 #endif
35 WrapStreamForShare::WrapStreamForShare( const uno::Reference< io::XInputStream >& xInStream,
36 const rtl::Reference< comphelper::RefCountedMutex >& rMutexRef )
37 : m_xMutex( rMutexRef )
38 , m_xInStream( xInStream )
39 , m_nCurPos( 0 )
41 if ( !m_xMutex.is() || !m_xInStream.is() )
43 OSL_FAIL( "Wrong initialization of wrapping stream!" );
44 throw uno::RuntimeException(THROW_WHERE );
46 m_xSeekable.set( m_xInStream, uno::UNO_QUERY_THROW );
49 WrapStreamForShare::~WrapStreamForShare()
53 // XInputStream
54 sal_Int32 SAL_CALL WrapStreamForShare::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
56 if ( !m_xInStream.is() )
57 throw io::IOException(THROW_WHERE );
59 m_xSeekable->seek( m_nCurPos );
61 sal_Int32 nRead = m_xInStream->readBytes( aData, nBytesToRead );
62 m_nCurPos += nRead;
64 return nRead;
67 sal_Int32 SAL_CALL WrapStreamForShare::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
69 if ( !m_xInStream.is() )
70 throw io::IOException(THROW_WHERE );
72 m_xSeekable->seek( m_nCurPos );
74 sal_Int32 nRead = m_xInStream->readSomeBytes( aData, nMaxBytesToRead );
75 m_nCurPos += nRead;
77 return nRead;
80 void SAL_CALL WrapStreamForShare::skipBytes( sal_Int32 nBytesToSkip )
82 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
84 if ( !m_xInStream.is() )
85 throw io::IOException(THROW_WHERE );
87 m_xSeekable->seek( m_nCurPos );
89 m_xInStream->skipBytes( nBytesToSkip );
90 m_nCurPos = m_xSeekable->getPosition();
93 sal_Int32 SAL_CALL WrapStreamForShare::available()
95 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
97 if ( !m_xInStream.is() )
98 throw io::IOException(THROW_WHERE );
100 return m_xInStream->available();
103 void SAL_CALL WrapStreamForShare::closeInput()
105 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
107 if ( !m_xInStream.is() )
108 throw io::IOException(THROW_WHERE );
110 // the package is the owner so it will close the stream
111 // m_xInStream->closeInput();
112 m_xInStream.clear();
113 m_xSeekable.clear();
116 // XSeekable
117 void SAL_CALL WrapStreamForShare::seek( sal_Int64 location )
119 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
121 if ( !m_xInStream.is() )
122 throw io::IOException(THROW_WHERE );
124 // let stream implementation do all the checking
125 m_xSeekable->seek( location );
127 m_nCurPos = m_xSeekable->getPosition();
130 sal_Int64 SAL_CALL WrapStreamForShare::getPosition()
132 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
134 if ( !m_xInStream.is() )
135 throw io::IOException(THROW_WHERE );
137 return m_nCurPos;
140 sal_Int64 SAL_CALL WrapStreamForShare::getLength()
142 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
144 if ( !m_xInStream.is() )
145 throw io::IOException(THROW_WHERE );
147 return m_xSeekable->getLength();
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */