LibreOffice Module package (master) 1
wrapstreamforshare.cxx
Go to the documentation of this file.
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 <utility>
25#include <osl/diagnose.h>
26
28
29using namespace ::com::sun::star;
30
31#if OSL_DEBUG_LEVEL > 0
32#define THROW_WHERE SAL_WHERE
33#else
34#define THROW_WHERE ""
35#endif
36
37WrapStreamForShare::WrapStreamForShare( uno::Reference< io::XInputStream > xInStream,
39: m_xMutex(std::move( xMutexRef ))
40, m_xInStream(std::move( xInStream ))
41, m_nCurPos( 0 )
42{
43 if ( !m_xMutex.is() || !m_xInStream.is() )
44 {
45 OSL_FAIL( "Wrong initialization of wrapping stream!" );
46 throw uno::RuntimeException(THROW_WHERE );
47 }
48 m_xSeekable.set( m_xInStream, uno::UNO_QUERY_THROW );
49}
50
52{
53}
54
55// XInputStream
56sal_Int32 SAL_CALL WrapStreamForShare::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
57{
58 if ( !m_xInStream.is() )
59 throw io::IOException(THROW_WHERE );
60
61 m_xSeekable->seek( m_nCurPos );
62
63 sal_Int32 nRead = m_xInStream->readBytes( aData, nBytesToRead );
64 m_nCurPos += nRead;
65
66 return nRead;
67}
68
69sal_Int32 SAL_CALL WrapStreamForShare::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
70{
71 if ( !m_xInStream.is() )
72 throw io::IOException(THROW_WHERE );
73
74 m_xSeekable->seek( m_nCurPos );
75
76 sal_Int32 nRead = m_xInStream->readSomeBytes( aData, nMaxBytesToRead );
77 m_nCurPos += nRead;
78
79 return nRead;
80}
81
82void SAL_CALL WrapStreamForShare::skipBytes( sal_Int32 nBytesToSkip )
83{
84 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
85
86 if ( !m_xInStream.is() )
87 throw io::IOException(THROW_WHERE );
88
89 m_xSeekable->seek( m_nCurPos );
90
91 m_xInStream->skipBytes( nBytesToSkip );
92 m_nCurPos = m_xSeekable->getPosition();
93}
94
96{
97 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
98
99 if ( !m_xInStream.is() )
100 throw io::IOException(THROW_WHERE );
101
102 return m_xInStream->available();
103}
104
106{
107 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
108
109 if ( !m_xInStream.is() )
110 throw io::IOException(THROW_WHERE );
111
112 // the package is the owner so it will close the stream
113 // m_xInStream->closeInput();
114 m_xInStream.clear();
115 m_xSeekable.clear();
116}
117
118// XSeekable
119void SAL_CALL WrapStreamForShare::seek( sal_Int64 location )
120{
121 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
122
123 if ( !m_xInStream.is() )
124 throw io::IOException(THROW_WHERE );
125
126 // let stream implementation do all the checking
127 m_xSeekable->seek( location );
128
129 m_nCurPos = m_xSeekable->getPosition();
130}
131
133{
134 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
135
136 if ( !m_xInStream.is() )
137 throw io::IOException(THROW_WHERE );
138
139 return m_nCurPos;
140}
141
143{
144 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
145
146 if ( !m_xInStream.is() )
147 throw io::IOException(THROW_WHERE );
148
149 return m_xSeekable->getLength();
150}
151
152/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
virtual ~WrapStreamForShare() override
css::uno::Reference< css::io::XSeekable > m_xSeekable
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
virtual sal_Int64 SAL_CALL getLength() override
css::uno::Reference< css::io::XInputStream > m_xInStream
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
virtual void SAL_CALL seek(sal_Int64 location) override
rtl::Reference< comphelper::RefCountedMutex > m_xMutex
virtual sal_Int64 SAL_CALL getPosition() override
virtual sal_Int32 SAL_CALL available() override
WrapStreamForShare(css::uno::Reference< css::io::XInputStream > xInStream, rtl::Reference< comphelper::RefCountedMutex > xMutexRef)
virtual void SAL_CALL closeInput() override
constexpr OUStringLiteral aData
sal_Int32 m_nCurPos
#define THROW_WHERE