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 <sal/config.h>
21 #include <sal/log.hxx>
23 #include <com/sun/star/io/IOException.hpp>
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
34 #define THROW_WHERE ""
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
))
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
);
51 WrapStreamForShare::~WrapStreamForShare()
56 sal_Int32 SAL_CALL
WrapStreamForShare::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
58 if ( !m_xInStream
.is() )
59 throw io::IOException(THROW_WHERE
);
61 m_xSeekable
->seek( m_nCurPos
);
63 sal_Int32 nRead
= m_xInStream
->readBytes( aData
, nBytesToRead
);
69 sal_Int32 SAL_CALL
WrapStreamForShare::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
71 if ( !m_xInStream
.is() )
72 throw io::IOException(THROW_WHERE
);
74 m_xSeekable
->seek( m_nCurPos
);
76 sal_Int32 nRead
= m_xInStream
->readSomeBytes( aData
, nMaxBytesToRead
);
82 void SAL_CALL
WrapStreamForShare::skipBytes( sal_Int32 nBytesToSkip
)
84 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
86 if ( !m_xInStream
.is() )
87 throw io::IOException(THROW_WHERE
);
89 m_xSeekable
->seek( m_nCurPos
);
91 m_xInStream
->skipBytes( nBytesToSkip
);
92 m_nCurPos
= m_xSeekable
->getPosition();
95 sal_Int32 SAL_CALL
WrapStreamForShare::available()
97 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
99 if ( !m_xInStream
.is() )
100 throw io::IOException(THROW_WHERE
);
102 return m_xInStream
->available();
105 void SAL_CALL
WrapStreamForShare::closeInput()
107 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
109 if ( !m_xInStream
.is() )
110 throw io::IOException(THROW_WHERE
);
112 // the package is the owner so it will close the stream
113 // m_xInStream->closeInput();
119 void SAL_CALL
WrapStreamForShare::seek( sal_Int64 location
)
121 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
123 if ( !m_xInStream
.is() )
124 throw io::IOException(THROW_WHERE
);
126 // let stream implementation do all the checking
127 m_xSeekable
->seek( location
);
129 m_nCurPos
= m_xSeekable
->getPosition();
132 sal_Int64 SAL_CALL
WrapStreamForShare::getPosition()
134 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
136 if ( !m_xInStream
.is() )
137 throw io::IOException(THROW_WHERE
);
142 sal_Int64 SAL_CALL
WrapStreamForShare::getLength()
144 ::osl::MutexGuard
aGuard( m_xMutex
->GetMutex() );
146 if ( !m_xInStream
.is() )
147 throw io::IOException(THROW_WHERE
);
149 return m_xSeekable
->getLength();
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */