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 <osl/diagnose.h>
22 #include "wrapstreamforshare.hxx"
24 using namespace ::com::sun::star
;
26 #if OSL_DEBUG_LEVEL > 0
27 #define THROW_WHERE SAL_WHERE
29 #define THROW_WHERE ""
32 WrapStreamForShare::WrapStreamForShare( const uno::Reference
< io::XInputStream
>& xInStream
,
33 const SotMutexHolderRef
& rMutexRef
)
34 : m_rMutexRef( rMutexRef
)
35 , m_xInStream( xInStream
)
38 m_xSeekable
= uno::Reference
< io::XSeekable
>( m_xInStream
, uno::UNO_QUERY
);
39 if ( !m_rMutexRef
.Is() || !m_xInStream
.is() || !m_xSeekable
.is() )
41 OSL_FAIL( "Wrong initialization of wrapping stream!\n" );
42 throw uno::RuntimeException(THROW_WHERE
);
46 WrapStreamForShare::~WrapStreamForShare()
51 sal_Int32 SAL_CALL
WrapStreamForShare::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
52 throw ( io::NotConnectedException
,
53 io::BufferSizeExceededException
,
55 uno::RuntimeException
, std::exception
)
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
);
68 sal_Int32 SAL_CALL
WrapStreamForShare::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
69 throw ( io::NotConnectedException
,
70 io::BufferSizeExceededException
,
72 uno::RuntimeException
, std::exception
)
74 if ( !m_xInStream
.is() )
75 throw io::IOException(THROW_WHERE
);
77 m_xSeekable
->seek( m_nCurPos
);
79 sal_Int32 nRead
= m_xInStream
->readSomeBytes( aData
, nMaxBytesToRead
);
85 void SAL_CALL
WrapStreamForShare::skipBytes( sal_Int32 nBytesToSkip
)
86 throw ( io::NotConnectedException
,
87 io::BufferSizeExceededException
,
89 uno::RuntimeException
, std::exception
)
91 ::osl::MutexGuard
aGuard( m_rMutexRef
->GetMutex() );
93 if ( !m_xInStream
.is() )
94 throw io::IOException(THROW_WHERE
);
96 m_xSeekable
->seek( m_nCurPos
);
98 m_xInStream
->skipBytes( nBytesToSkip
);
99 m_nCurPos
= m_xSeekable
->getPosition();
102 sal_Int32 SAL_CALL
WrapStreamForShare::available()
103 throw ( io::NotConnectedException
,
105 uno::RuntimeException
, std::exception
)
107 ::osl::MutexGuard
aGuard( m_rMutexRef
->GetMutex() );
109 if ( !m_xInStream
.is() )
110 throw io::IOException(THROW_WHERE
);
112 return m_xInStream
->available();
115 void SAL_CALL
WrapStreamForShare::closeInput()
116 throw ( io::NotConnectedException
,
118 uno::RuntimeException
, std::exception
)
120 ::osl::MutexGuard
aGuard( m_rMutexRef
->GetMutex() );
122 if ( !m_xInStream
.is() )
123 throw io::IOException(THROW_WHERE
);
125 // the package is the owner so it will close the stream
126 // m_xInStream->closeInput();
127 m_xInStream
= uno::Reference
< io::XInputStream
>();
128 m_xSeekable
= uno::Reference
< io::XSeekable
>();
132 void SAL_CALL
WrapStreamForShare::seek( sal_Int64 location
)
133 throw ( lang::IllegalArgumentException
,
135 uno::RuntimeException
, std::exception
)
137 ::osl::MutexGuard
aGuard( m_rMutexRef
->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()
149 throw ( io::IOException
,
150 uno::RuntimeException
, std::exception
)
152 ::osl::MutexGuard
aGuard( m_rMutexRef
->GetMutex() );
154 if ( !m_xInStream
.is() )
155 throw io::IOException(THROW_WHERE
);
160 sal_Int64 SAL_CALL
WrapStreamForShare::getLength()
161 throw ( io::IOException
,
162 uno::RuntimeException
, std::exception
)
164 ::osl::MutexGuard
aGuard( m_rMutexRef
->GetMutex() );
166 if ( !m_xInStream
.is() )
167 throw io::IOException(THROW_WHERE
);
169 return m_xSeekable
->getLength();
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */