1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <sal/config.h>
21 #include <sal/log.hxx>
22
23 #include <com/sun/star/io/IOException.hpp>
24 #include <osl/diagnose.h>
25
26 #include "wrapstreamforshare.hxx"
27
28 using namespace ::com::sun::star;
29
30 #if OSL_DEBUG_LEVEL > 0
31 #define THROW_WHERE SAL_WHERE
32 #else
33 #define THROW_WHERE ""
34 #endif
35
WrapStreamForShare(const uno::Reference<io::XInputStream> & xInStream,const rtl::Reference<comphelper::RefCountedMutex> & rMutexRef)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 )
41 {
42 if ( !m_xMutex.is() || !m_xInStream.is() )
43 {
44 OSL_FAIL( "Wrong initialization of wrapping stream!" );
45 throw uno::RuntimeException(THROW_WHERE );
46 }
47 m_xSeekable.set( m_xInStream, uno::UNO_QUERY_THROW );
48 }
49
~WrapStreamForShare()50 WrapStreamForShare::~WrapStreamForShare()
51 {
52 }
53
54 // XInputStream
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)55 sal_Int32 SAL_CALL WrapStreamForShare::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
56 {
57 if ( !m_xInStream.is() )
58 throw io::IOException(THROW_WHERE );
59
60 m_xSeekable->seek( m_nCurPos );
61
62 sal_Int32 nRead = m_xInStream->readBytes( aData, nBytesToRead );
63 m_nCurPos += nRead;
64
65 return nRead;
66 }
67
readSomeBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)68 sal_Int32 SAL_CALL WrapStreamForShare::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
69 {
70 if ( !m_xInStream.is() )
71 throw io::IOException(THROW_WHERE );
72
73 m_xSeekable->seek( m_nCurPos );
74
75 sal_Int32 nRead = m_xInStream->readSomeBytes( aData, nMaxBytesToRead );
76 m_nCurPos += nRead;
77
78 return nRead;
79 }
80
skipBytes(sal_Int32 nBytesToSkip)81 void SAL_CALL WrapStreamForShare::skipBytes( sal_Int32 nBytesToSkip )
82 {
83 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
84
85 if ( !m_xInStream.is() )
86 throw io::IOException(THROW_WHERE );
87
88 m_xSeekable->seek( m_nCurPos );
89
90 m_xInStream->skipBytes( nBytesToSkip );
91 m_nCurPos = m_xSeekable->getPosition();
92 }
93
available()94 sal_Int32 SAL_CALL WrapStreamForShare::available()
95 {
96 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
97
98 if ( !m_xInStream.is() )
99 throw io::IOException(THROW_WHERE );
100
101 return m_xInStream->available();
102 }
103
closeInput()104 void SAL_CALL WrapStreamForShare::closeInput()
105 {
106 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
107
108 if ( !m_xInStream.is() )
109 throw io::IOException(THROW_WHERE );
110
111 // the package is the owner so it will close the stream
112 // m_xInStream->closeInput();
113 m_xInStream.clear();
114 m_xSeekable.clear();
115 }
116
117 // XSeekable
seek(sal_Int64 location)118 void SAL_CALL WrapStreamForShare::seek( sal_Int64 location )
119 {
120 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
121
122 if ( !m_xInStream.is() )
123 throw io::IOException(THROW_WHERE );
124
125 // let stream implementation do all the checking
126 m_xSeekable->seek( location );
127
128 m_nCurPos = m_xSeekable->getPosition();
129 }
130
getPosition()131 sal_Int64 SAL_CALL WrapStreamForShare::getPosition()
132 {
133 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
134
135 if ( !m_xInStream.is() )
136 throw io::IOException(THROW_WHERE );
137
138 return m_nCurPos;
139 }
140
getLength()141 sal_Int64 SAL_CALL WrapStreamForShare::getLength()
142 {
143 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
144
145 if ( !m_xInStream.is() )
146 throw io::IOException(THROW_WHERE );
147
148 return m_xSeekable->getLength();
149 }
150
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
152