Line data Source code
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 <osl/diagnose.h>
21 :
22 : #include "wrapstreamforshare.hxx"
23 :
24 : using namespace ::com::sun::star;
25 :
26 : #if OSL_DEBUG_LEVEL > 0
27 : #define THROW_WHERE SAL_WHERE
28 : #else
29 : #define THROW_WHERE ""
30 : #endif
31 :
32 19708 : WrapStreamForShare::WrapStreamForShare( const uno::Reference< io::XInputStream >& xInStream,
33 : const SotMutexHolderRef& rMutexRef )
34 : : m_rMutexRef( rMutexRef )
35 : , m_xInStream( xInStream )
36 19708 : , m_nCurPos( 0 )
37 : {
38 19708 : m_xSeekable = uno::Reference< io::XSeekable >( m_xInStream, uno::UNO_QUERY );
39 19708 : if ( !m_rMutexRef.Is() || !m_xInStream.is() || !m_xSeekable.is() )
40 : {
41 : OSL_FAIL( "Wrong initialization of wrapping stream!\n" );
42 0 : throw uno::RuntimeException(THROW_WHERE );
43 : }
44 19708 : }
45 :
46 39416 : WrapStreamForShare::~WrapStreamForShare()
47 : {
48 39416 : }
49 :
50 : // XInputStream
51 18922 : sal_Int32 SAL_CALL WrapStreamForShare::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
52 : throw ( io::NotConnectedException,
53 : io::BufferSizeExceededException,
54 : io::IOException,
55 : uno::RuntimeException, std::exception )
56 : {
57 18922 : ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
58 :
59 18922 : if ( !m_xInStream.is() )
60 0 : throw io::IOException(THROW_WHERE );
61 :
62 18922 : m_xSeekable->seek( m_nCurPos );
63 :
64 18922 : sal_Int32 nRead = m_xInStream->readBytes( aData, nBytesToRead );
65 18922 : m_nCurPos += nRead;
66 :
67 18922 : return nRead;
68 : }
69 :
70 980 : sal_Int32 SAL_CALL WrapStreamForShare::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
71 : throw ( io::NotConnectedException,
72 : io::BufferSizeExceededException,
73 : io::IOException,
74 : uno::RuntimeException, std::exception )
75 : {
76 980 : ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
77 :
78 980 : if ( !m_xInStream.is() )
79 0 : throw io::IOException(THROW_WHERE );
80 :
81 980 : m_xSeekable->seek( m_nCurPos );
82 :
83 980 : sal_Int32 nRead = m_xInStream->readSomeBytes( aData, nMaxBytesToRead );
84 980 : m_nCurPos += nRead;
85 :
86 980 : return nRead;
87 : }
88 :
89 2 : void SAL_CALL WrapStreamForShare::skipBytes( sal_Int32 nBytesToSkip )
90 : throw ( io::NotConnectedException,
91 : io::BufferSizeExceededException,
92 : io::IOException,
93 : uno::RuntimeException, std::exception )
94 : {
95 2 : ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
96 :
97 2 : if ( !m_xInStream.is() )
98 0 : throw io::IOException(THROW_WHERE );
99 :
100 2 : m_xSeekable->seek( m_nCurPos );
101 :
102 2 : m_xInStream->skipBytes( nBytesToSkip );
103 2 : m_nCurPos = m_xSeekable->getPosition();
104 2 : }
105 :
106 0 : sal_Int32 SAL_CALL WrapStreamForShare::available()
107 : throw ( io::NotConnectedException,
108 : io::IOException,
109 : uno::RuntimeException, std::exception )
110 : {
111 0 : ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
112 :
113 0 : if ( !m_xInStream.is() )
114 0 : throw io::IOException(THROW_WHERE );
115 :
116 0 : return m_xInStream->available();
117 : }
118 :
119 818 : void SAL_CALL WrapStreamForShare::closeInput()
120 : throw ( io::NotConnectedException,
121 : io::IOException,
122 : uno::RuntimeException, std::exception )
123 : {
124 818 : ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
125 :
126 818 : if ( !m_xInStream.is() )
127 0 : throw io::IOException(THROW_WHERE );
128 :
129 : // the package is the owner so it will close the stream
130 : // m_xInStream->closeInput();
131 818 : m_xInStream = uno::Reference< io::XInputStream >();
132 818 : m_xSeekable = uno::Reference< io::XSeekable >();
133 818 : }
134 :
135 : // XSeekable
136 16390 : void SAL_CALL WrapStreamForShare::seek( sal_Int64 location )
137 : throw ( lang::IllegalArgumentException,
138 : io::IOException,
139 : uno::RuntimeException, std::exception )
140 : {
141 16390 : ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
142 :
143 16390 : if ( !m_xInStream.is() )
144 0 : throw io::IOException(THROW_WHERE );
145 :
146 : // let stream implementation do all the checking
147 16390 : m_xSeekable->seek( location );
148 :
149 16390 : m_nCurPos = m_xSeekable->getPosition();
150 16390 : }
151 :
152 0 : sal_Int64 SAL_CALL WrapStreamForShare::getPosition()
153 : throw ( io::IOException,
154 : uno::RuntimeException, std::exception)
155 : {
156 0 : ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
157 :
158 0 : if ( !m_xInStream.is() )
159 0 : throw io::IOException(THROW_WHERE );
160 :
161 0 : return m_nCurPos;
162 : }
163 :
164 10 : sal_Int64 SAL_CALL WrapStreamForShare::getLength()
165 : throw ( io::IOException,
166 : uno::RuntimeException, std::exception )
167 : {
168 10 : ::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
169 :
170 10 : if ( !m_xInStream.is() )
171 0 : throw io::IOException(THROW_WHERE );
172 :
173 10 : return m_xSeekable->getLength();
174 : }
175 :
176 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|