bump product version to 6.4.0.3
[LibreOffice.git] / package / source / zippackage / wrapstreamforshare.cxx
blobe5a47566551ea4022e8286855119698d507ae2e4
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 <osl/diagnose.h>
26 #include "wrapstreamforshare.hxx"
28 using namespace ::com::sun::star;
30 #if OSL_DEBUG_LEVEL > 0
31 #define THROW_WHERE SAL_WHERE
32 #else
33 #define THROW_WHERE ""
34 #endif
36 WrapStreamForShare::WrapStreamForShare( const uno::Reference< io::XInputStream >& xInStream,
37 const rtl::Reference< comphelper::RefCountedMutex >& rMutexRef )
38 : m_xMutex( rMutexRef )
39 , m_xInStream( xInStream )
40 , m_nCurPos( 0 )
42 if ( !m_xMutex.is() || !m_xInStream.is() )
44 OSL_FAIL( "Wrong initialization of wrapping stream!" );
45 throw uno::RuntimeException(THROW_WHERE );
47 m_xSeekable.set( m_xInStream, uno::UNO_QUERY_THROW );
50 WrapStreamForShare::~WrapStreamForShare()
54 // XInputStream
55 sal_Int32 SAL_CALL WrapStreamForShare::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
57 if ( !m_xInStream.is() )
58 throw io::IOException(THROW_WHERE );
60 m_xSeekable->seek( m_nCurPos );
62 sal_Int32 nRead = m_xInStream->readBytes( aData, nBytesToRead );
63 m_nCurPos += nRead;
65 return nRead;
68 sal_Int32 SAL_CALL WrapStreamForShare::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
70 if ( !m_xInStream.is() )
71 throw io::IOException(THROW_WHERE );
73 m_xSeekable->seek( m_nCurPos );
75 sal_Int32 nRead = m_xInStream->readSomeBytes( aData, nMaxBytesToRead );
76 m_nCurPos += nRead;
78 return nRead;
81 void SAL_CALL WrapStreamForShare::skipBytes( sal_Int32 nBytesToSkip )
83 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
85 if ( !m_xInStream.is() )
86 throw io::IOException(THROW_WHERE );
88 m_xSeekable->seek( m_nCurPos );
90 m_xInStream->skipBytes( nBytesToSkip );
91 m_nCurPos = m_xSeekable->getPosition();
94 sal_Int32 SAL_CALL WrapStreamForShare::available()
96 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
98 if ( !m_xInStream.is() )
99 throw io::IOException(THROW_WHERE );
101 return m_xInStream->available();
104 void SAL_CALL WrapStreamForShare::closeInput()
106 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
108 if ( !m_xInStream.is() )
109 throw io::IOException(THROW_WHERE );
111 // the package is the owner so it will close the stream
112 // m_xInStream->closeInput();
113 m_xInStream.clear();
114 m_xSeekable.clear();
117 // XSeekable
118 void SAL_CALL WrapStreamForShare::seek( sal_Int64 location )
120 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
122 if ( !m_xInStream.is() )
123 throw io::IOException(THROW_WHERE );
125 // let stream implementation do all the checking
126 m_xSeekable->seek( location );
128 m_nCurPos = m_xSeekable->getPosition();
131 sal_Int64 SAL_CALL WrapStreamForShare::getPosition()
133 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
135 if ( !m_xInStream.is() )
136 throw io::IOException(THROW_WHERE );
138 return m_nCurPos;
141 sal_Int64 SAL_CALL WrapStreamForShare::getLength()
143 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
145 if ( !m_xInStream.is() )
146 throw io::IOException(THROW_WHERE );
148 return m_xSeekable->getLength();
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */