Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / ucb / source / ucp / file / filinpstr.cxx
blobbc9afd01603751a1d881981eeaac133c28037e04
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "filinpstr.hxx"
21 #include "filerror.hxx"
22 #include "shell.hxx"
23 #include "prov.hxx"
26 using namespace fileaccess;
27 using namespace com::sun::star;
28 using namespace com::sun::star::ucb;
32 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 m_nMinorErrorCode( TASKHANDLER_NO_ERROR )
38 sal_uInt32 nFlags = osl_File_OpenFlag_Read;
39 if ( !bLock )
40 nFlags |= osl_File_OpenFlag_NoLock;
42 osl::FileBase::RC err = m_aFile.open( nFlags );
43 if( err != osl::FileBase::E_None )
45 m_nIsOpen = false;
46 m_aFile.close();
48 m_nErrorCode = TASKHANDLING_OPEN_FOR_INPUTSTREAM;
49 m_nMinorErrorCode = err;
51 else
52 m_nIsOpen = true;
56 XInputStream_impl::~XInputStream_impl()
58 try
60 closeInput();
62 catch (io::IOException const &)
64 OSL_FAIL("unexpected situation");
66 catch (uno::RuntimeException const &)
68 OSL_FAIL("unexpected situation");
73 sal_Int32 SAL_CALL XInputStream_impl::CtorSuccess()
75 return m_nErrorCode;
80 sal_Int32 SAL_CALL XInputStream_impl::getMinorError()
82 return m_nMinorErrorCode;
86 //////////////////////////////////////////////////////////////////////////////////////////
87 // XTypeProvider
88 //////////////////////////////////////////////////////////////////////////////////////////
91 XTYPEPROVIDER_IMPL_3( XInputStream_impl,
92 lang::XTypeProvider,
93 io::XSeekable,
94 io::XInputStream )
98 uno::Any SAL_CALL
99 XInputStream_impl::queryInterface(
100 const uno::Type& rType )
101 throw( uno::RuntimeException)
103 uno::Any aRet = cppu::queryInterface( rType,
104 (static_cast< io::XInputStream* >(this)),
105 (static_cast< lang::XTypeProvider* >(this)),
106 (static_cast< io::XSeekable* >(this)) );
107 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
111 void SAL_CALL
112 XInputStream_impl::acquire(
113 void )
114 throw()
116 OWeakObject::acquire();
120 void SAL_CALL
121 XInputStream_impl::release(
122 void )
123 throw()
125 OWeakObject::release();
130 sal_Int32 SAL_CALL
131 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)
139 if( ! m_nIsOpen ) throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
141 aData.realloc(nBytesToRead);
142 //TODO! translate memory exhaustion (if it were detectable...) into
143 // io::BufferSizeExceededException
145 sal_uInt64 nrc(0);
146 if(m_aFile.read( aData.getArray(),sal_uInt64(nBytesToRead),nrc )
147 != osl::FileBase::E_None)
148 throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
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 if (sal::static_int_cast<sal_Int32>(nrc) != nBytesToRead)
154 aData.realloc(sal_Int32(nrc));
155 return ( sal_Int32 ) nrc;
158 sal_Int32 SAL_CALL
159 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)
167 return readBytes( aData,nMaxBytesToRead );
171 void SAL_CALL
172 XInputStream_impl::skipBytes(
173 sal_Int32 nBytesToSkip )
174 throw( io::NotConnectedException,
175 io::BufferSizeExceededException,
176 io::IOException,
177 uno::RuntimeException)
179 m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) );
183 sal_Int32 SAL_CALL
184 XInputStream_impl::available(
185 void )
186 throw( io::NotConnectedException,
187 io::IOException,
188 uno::RuntimeException)
190 return 0;
194 void SAL_CALL
195 XInputStream_impl::closeInput(
196 void )
197 throw( io::NotConnectedException,
198 io::IOException,
199 uno::RuntimeException )
201 if( m_nIsOpen )
203 osl::FileBase::RC err = m_aFile.close();
204 if( err != osl::FileBase::E_None )
205 throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
206 m_nIsOpen = false;
211 void SAL_CALL
212 XInputStream_impl::seek(
213 sal_Int64 location )
214 throw( lang::IllegalArgumentException,
215 io::IOException,
216 uno::RuntimeException )
218 if( location < 0 )
219 throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 0 );
220 if( osl::FileBase::E_None != m_aFile.setPos( osl_Pos_Absolut, sal_uInt64( location ) ) )
221 throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
225 sal_Int64 SAL_CALL
226 XInputStream_impl::getPosition(
227 void )
228 throw( io::IOException,
229 uno::RuntimeException )
231 sal_uInt64 uPos;
232 if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
233 throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
234 return sal_Int64( uPos );
237 sal_Int64 SAL_CALL
238 XInputStream_impl::getLength(
239 void )
240 throw( io::IOException,
241 uno::RuntimeException )
243 sal_uInt64 uEndPos;
244 if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None )
245 throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
246 else
247 return sal_Int64( uEndPos );
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */