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 "filinpstr.hxx"
21 : #include "filerror.hxx"
22 : #include "shell.hxx"
23 : #include "prov.hxx"
24 :
25 :
26 : using namespace fileaccess;
27 : using namespace com::sun::star;
28 : using namespace com::sun::star::ucb;
29 :
30 :
31 :
32 850 : XInputStream_impl::XInputStream_impl( shell* pMyShell,const rtl::OUString& aUncPath, sal_Bool bLock )
33 : : m_xProvider( pMyShell->m_pProvider ),
34 : m_aFile( aUncPath ),
35 : m_nErrorCode( TASKHANDLER_NO_ERROR ),
36 850 : m_nMinorErrorCode( TASKHANDLER_NO_ERROR )
37 : {
38 850 : sal_uInt32 nFlags = osl_File_OpenFlag_Read;
39 850 : if ( !bLock )
40 240 : nFlags |= osl_File_OpenFlag_NoLock;
41 :
42 850 : osl::FileBase::RC err = m_aFile.open( nFlags );
43 850 : if( err != osl::FileBase::E_None )
44 : {
45 433 : m_nIsOpen = false;
46 433 : m_aFile.close();
47 :
48 433 : m_nErrorCode = TASKHANDLING_OPEN_FOR_INPUTSTREAM;
49 433 : m_nMinorErrorCode = err;
50 : }
51 : else
52 417 : m_nIsOpen = true;
53 850 : }
54 :
55 :
56 2532 : XInputStream_impl::~XInputStream_impl()
57 : {
58 : try
59 : {
60 844 : closeInput();
61 : }
62 0 : catch (io::IOException const &)
63 : {
64 : OSL_FAIL("unexpected situation");
65 : }
66 0 : catch (uno::RuntimeException const &)
67 : {
68 : OSL_FAIL("unexpected situation");
69 : }
70 1688 : }
71 :
72 :
73 850 : sal_Int32 SAL_CALL XInputStream_impl::CtorSuccess()
74 : {
75 850 : return m_nErrorCode;
76 : };
77 :
78 :
79 :
80 433 : sal_Int32 SAL_CALL XInputStream_impl::getMinorError()
81 : {
82 433 : return m_nMinorErrorCode;
83 : }
84 :
85 :
86 : //////////////////////////////////////////////////////////////////////////////////////////
87 : // XTypeProvider
88 : //////////////////////////////////////////////////////////////////////////////////////////
89 :
90 :
91 0 : XTYPEPROVIDER_IMPL_3( XInputStream_impl,
92 : lang::XTypeProvider,
93 : io::XSeekable,
94 : io::XInputStream )
95 :
96 :
97 :
98 : uno::Any SAL_CALL
99 4331 : XInputStream_impl::queryInterface(
100 : const uno::Type& rType )
101 : throw( uno::RuntimeException)
102 : {
103 : uno::Any aRet = cppu::queryInterface( rType,
104 : (static_cast< io::XInputStream* >(this)),
105 : (static_cast< lang::XTypeProvider* >(this)),
106 4331 : (static_cast< io::XSeekable* >(this)) );
107 4331 : return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
108 : }
109 :
110 :
111 : void SAL_CALL
112 59948 : XInputStream_impl::acquire(
113 : void )
114 : throw()
115 : {
116 59948 : OWeakObject::acquire();
117 59948 : }
118 :
119 :
120 : void SAL_CALL
121 59930 : XInputStream_impl::release(
122 : void )
123 : throw()
124 : {
125 59930 : OWeakObject::release();
126 59930 : }
127 :
128 :
129 :
130 : sal_Int32 SAL_CALL
131 34772 : XInputStream_impl::readBytes(
132 : uno::Sequence< sal_Int8 >& aData,
133 : sal_Int32 nBytesToRead )
134 : throw( io::NotConnectedException,
135 : io::BufferSizeExceededException,
136 : io::IOException,
137 : uno::RuntimeException)
138 : {
139 34772 : if( ! m_nIsOpen ) throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
140 :
141 34772 : aData.realloc(nBytesToRead);
142 : //TODO! translate memory exhaustion (if it were detectable...) into
143 : // io::BufferSizeExceededException
144 :
145 34772 : sal_uInt64 nrc(0);
146 34772 : if(m_aFile.read( aData.getArray(),sal_uInt64(nBytesToRead),nrc )
147 : != osl::FileBase::E_None)
148 0 : throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
149 :
150 : // Shrink aData in case we read less than nBytesToRead (XInputStream
151 : // documentation does not tell whether this is required, and I do not know
152 : // if any code relies on this, so be conservative---SB):
153 34772 : if (sal::static_int_cast<sal_Int32>(nrc) != nBytesToRead)
154 11622 : aData.realloc(sal_Int32(nrc));
155 34772 : return ( sal_Int32 ) nrc;
156 : }
157 :
158 : sal_Int32 SAL_CALL
159 13 : XInputStream_impl::readSomeBytes(
160 : uno::Sequence< sal_Int8 >& aData,
161 : sal_Int32 nMaxBytesToRead )
162 : throw( io::NotConnectedException,
163 : io::BufferSizeExceededException,
164 : io::IOException,
165 : uno::RuntimeException)
166 : {
167 13 : return readBytes( aData,nMaxBytesToRead );
168 : }
169 :
170 :
171 : void SAL_CALL
172 0 : XInputStream_impl::skipBytes(
173 : sal_Int32 nBytesToSkip )
174 : throw( io::NotConnectedException,
175 : io::BufferSizeExceededException,
176 : io::IOException,
177 : uno::RuntimeException)
178 : {
179 0 : m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) );
180 0 : }
181 :
182 :
183 : sal_Int32 SAL_CALL
184 0 : XInputStream_impl::available(
185 : void )
186 : throw( io::NotConnectedException,
187 : io::IOException,
188 : uno::RuntimeException)
189 : {
190 0 : return 0;
191 : }
192 :
193 :
194 : void SAL_CALL
195 862 : XInputStream_impl::closeInput(
196 : void )
197 : throw( io::NotConnectedException,
198 : io::IOException,
199 : uno::RuntimeException )
200 : {
201 862 : if( m_nIsOpen )
202 : {
203 411 : osl::FileBase::RC err = m_aFile.close();
204 411 : if( err != osl::FileBase::E_None )
205 0 : throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
206 411 : m_nIsOpen = false;
207 : }
208 862 : }
209 :
210 :
211 : void SAL_CALL
212 23085 : XInputStream_impl::seek(
213 : sal_Int64 location )
214 : throw( lang::IllegalArgumentException,
215 : io::IOException,
216 : uno::RuntimeException )
217 : {
218 23085 : if( location < 0 )
219 0 : throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 0 );
220 23085 : if( osl::FileBase::E_None != m_aFile.setPos( osl_Pos_Absolut, sal_uInt64( location ) ) )
221 0 : throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
222 23085 : }
223 :
224 :
225 : sal_Int64 SAL_CALL
226 1374 : XInputStream_impl::getPosition(
227 : void )
228 : throw( io::IOException,
229 : uno::RuntimeException )
230 : {
231 : sal_uInt64 uPos;
232 1374 : if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
233 0 : throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
234 1374 : return sal_Int64( uPos );
235 : }
236 :
237 : sal_Int64 SAL_CALL
238 4406 : XInputStream_impl::getLength(
239 : void )
240 : throw( io::IOException,
241 : uno::RuntimeException )
242 : {
243 : sal_uInt64 uEndPos;
244 4406 : if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None )
245 0 : throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
246 : else
247 4406 : return sal_Int64( uEndPos );
248 : }
249 :
250 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|