/src/libreoffice/package/source/zippackage/wrapstreamforshare.cxx
Line | Count | Source |
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 | | |
27 | | #include "wrapstreamforshare.hxx" |
28 | | |
29 | | using namespace ::com::sun::star; |
30 | | |
31 | | WrapStreamForShare::WrapStreamForShare( uno::Reference< io::XInputStream > xInStream, |
32 | | rtl::Reference< comphelper::RefCountedMutex > xMutexRef ) |
33 | 970 | : m_xMutex(std::move( xMutexRef )) |
34 | 970 | , m_xInStream(std::move( xInStream )) |
35 | 970 | , m_nCurPos( 0 ) |
36 | 970 | { |
37 | 970 | if ( !m_xMutex.is() || !m_xInStream.is() ) |
38 | 0 | { |
39 | 0 | OSL_FAIL( "Wrong initialization of wrapping stream!" ); |
40 | 0 | throw uno::RuntimeException(); |
41 | 0 | } |
42 | 970 | m_xSeekable.set( m_xInStream, uno::UNO_QUERY_THROW ); |
43 | 970 | mpByteReader = dynamic_cast<comphelper::ByteReader*>(m_xInStream.get()); |
44 | 970 | assert(mpByteReader); |
45 | 970 | } |
46 | | |
47 | | WrapStreamForShare::~WrapStreamForShare() |
48 | 970 | { |
49 | 970 | } |
50 | | |
51 | | // XInputStream |
52 | | sal_Int32 SAL_CALL WrapStreamForShare::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) |
53 | 212 | { |
54 | 212 | if ( !m_xInStream.is() ) |
55 | 0 | throw io::IOException(); |
56 | | |
57 | 212 | m_xSeekable->seek( m_nCurPos ); |
58 | | |
59 | 212 | sal_Int32 nRead = m_xInStream->readBytes( aData, nBytesToRead ); |
60 | 212 | m_nCurPos += nRead; |
61 | | |
62 | 212 | return nRead; |
63 | 212 | } |
64 | | |
65 | | sal_Int32 SAL_CALL WrapStreamForShare::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) |
66 | 0 | { |
67 | 0 | if ( !m_xInStream.is() ) |
68 | 0 | throw io::IOException(); |
69 | | |
70 | 0 | m_xSeekable->seek( m_nCurPos ); |
71 | |
|
72 | 0 | sal_Int32 nRead = m_xInStream->readSomeBytes( aData, nMaxBytesToRead ); |
73 | 0 | m_nCurPos += nRead; |
74 | |
|
75 | 0 | return nRead; |
76 | 0 | } |
77 | | |
78 | | sal_Int32 WrapStreamForShare::readSomeBytes( sal_Int8* aData, sal_Int32 nMaxBytesToRead ) |
79 | 48 | { |
80 | 48 | if ( !m_xInStream.is() ) |
81 | 0 | throw io::IOException(); |
82 | | |
83 | 48 | m_xSeekable->seek( m_nCurPos ); |
84 | | |
85 | 48 | sal_Int32 nRead = mpByteReader->readSomeBytes( aData, nMaxBytesToRead ); |
86 | 48 | m_nCurPos += nRead; |
87 | | |
88 | 48 | return nRead; |
89 | 48 | } |
90 | | |
91 | | void SAL_CALL WrapStreamForShare::skipBytes( sal_Int32 nBytesToSkip ) |
92 | 0 | { |
93 | 0 | ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ); |
94 | |
|
95 | 0 | if ( !m_xInStream.is() ) |
96 | 0 | throw io::IOException(); |
97 | | |
98 | 0 | m_xSeekable->seek( m_nCurPos ); |
99 | |
|
100 | 0 | m_xInStream->skipBytes( nBytesToSkip ); |
101 | 0 | m_nCurPos = m_xSeekable->getPosition(); |
102 | 0 | } |
103 | | |
104 | | sal_Int32 SAL_CALL WrapStreamForShare::available() |
105 | 0 | { |
106 | 0 | ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ); |
107 | |
|
108 | 0 | if ( !m_xInStream.is() ) |
109 | 0 | throw io::IOException(); |
110 | | |
111 | 0 | return m_xInStream->available(); |
112 | 0 | } |
113 | | |
114 | | void SAL_CALL WrapStreamForShare::closeInput() |
115 | 758 | { |
116 | 758 | ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ); |
117 | | |
118 | 758 | if ( !m_xInStream.is() ) |
119 | 0 | throw io::IOException(); |
120 | | |
121 | | // the package is the owner so it will close the stream |
122 | | // m_xInStream->closeInput(); |
123 | 758 | m_xInStream.clear(); |
124 | 758 | m_xSeekable.clear(); |
125 | 758 | mpByteReader = nullptr; |
126 | 758 | } |
127 | | |
128 | | // XSeekable |
129 | | void SAL_CALL WrapStreamForShare::seek( sal_Int64 location ) |
130 | 0 | { |
131 | 0 | ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ); |
132 | |
|
133 | 0 | if ( !m_xInStream.is() ) |
134 | 0 | throw io::IOException(); |
135 | | |
136 | | // let stream implementation do all the checking |
137 | 0 | m_xSeekable->seek( location ); |
138 | |
|
139 | 0 | m_nCurPos = m_xSeekable->getPosition(); |
140 | 0 | } |
141 | | |
142 | | sal_Int64 SAL_CALL WrapStreamForShare::getPosition() |
143 | 0 | { |
144 | 0 | ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ); |
145 | |
|
146 | 0 | if ( !m_xInStream.is() ) |
147 | 0 | throw io::IOException(); |
148 | | |
149 | 0 | return m_nCurPos; |
150 | 0 | } |
151 | | |
152 | | sal_Int64 SAL_CALL WrapStreamForShare::getLength() |
153 | 0 | { |
154 | 0 | ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ); |
155 | |
|
156 | 0 | if ( !m_xInStream.is() ) |
157 | 0 | throw io::IOException(); |
158 | | |
159 | 0 | return m_xSeekable->getLength(); |
160 | 0 | } |
161 | | |
162 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |