1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include "inputstream.hxx"
23 #include <cppuhelper/queryinterface.hxx>
26 using namespace chelp
;
27 using namespace com::sun::star
;
28 using namespace com::sun::star::ucb
;
32 XInputStream_impl::XInputStream_impl( const OUString
& aUncPath
)
36 m_bIsOpen
= ( osl::FileBase::E_None
== m_aFile
.open( osl_File_OpenFlag_Read
) );
40 XInputStream_impl::~XInputStream_impl()
47 XInputStream_impl::queryInterface(
48 const uno::Type
& rType
)
49 throw( uno::RuntimeException
, std::exception
)
51 uno::Any aRet
= cppu::queryInterface( rType
,
52 (static_cast< io::XInputStream
* >(this)),
53 (static_cast< io::XSeekable
* >(this)) );
54 return aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
);
59 XInputStream_impl::acquire(
63 OWeakObject::acquire();
68 XInputStream_impl::release(
72 OWeakObject::release();
78 XInputStream_impl::readBytes(
79 uno::Sequence
< sal_Int8
>& aData
,
80 sal_Int32 nBytesToRead
)
81 throw( io::NotConnectedException
,
82 io::BufferSizeExceededException
,
84 uno::RuntimeException
, std::exception
)
87 throw io::IOException();
89 aData
.realloc(nBytesToRead
);
90 //TODO! translate memory exhaustion (if it were detectable...) into
91 // io::BufferSizeExceededException
94 m_aFile
.read( aData
.getArray(),sal_uInt64(nBytesToRead
),nrc
);
96 // Shrink aData in case we read less than nBytesToRead (XInputStream
97 // documentation does not tell whether this is required, and I do not know
98 // if any code relies on this, so be conservative---SB):
99 if (nrc
!= sal::static_int_cast
<sal_uInt64
>( nBytesToRead
) )
100 aData
.realloc(sal_Int32(nrc
));
101 return ( sal_Int32
) nrc
;
105 XInputStream_impl::readSomeBytes(
106 uno::Sequence
< sal_Int8
>& aData
,
107 sal_Int32 nMaxBytesToRead
)
108 throw( io::NotConnectedException
,
109 io::BufferSizeExceededException
,
111 uno::RuntimeException
, std::exception
)
113 return readBytes( aData
,nMaxBytesToRead
);
118 XInputStream_impl::skipBytes(
119 sal_Int32 nBytesToSkip
)
120 throw( io::NotConnectedException
,
121 io::BufferSizeExceededException
,
123 uno::RuntimeException
, std::exception
)
125 if (m_aFile
.setPos(osl_Pos_Current
, sal_uInt64(nBytesToSkip
)) != osl::FileBase::E_None
)
127 throw io::IOException("XInputStream_impl::skipBytes failed seek");
133 XInputStream_impl::available(
135 throw( io::NotConnectedException
,
137 uno::RuntimeException
, std::exception
)
144 XInputStream_impl::closeInput(
146 throw( io::NotConnectedException
,
148 uno::RuntimeException
, std::exception
)
152 osl::FileBase::RC err
= m_aFile
.close();
153 if( err
!= osl::FileBase::E_None
)
154 throw io::IOException();
161 XInputStream_impl::seek(
163 throw( lang::IllegalArgumentException
,
165 uno::RuntimeException
, std::exception
)
168 throw lang::IllegalArgumentException();
169 if( osl::FileBase::E_None
!= m_aFile
.setPos( osl_Pos_Absolut
, sal_uInt64( location
) ) )
170 throw io::IOException();
175 XInputStream_impl::getPosition(
177 throw( io::IOException
,
178 uno::RuntimeException
, std::exception
)
181 if( osl::FileBase::E_None
!= m_aFile
.getPos( uPos
) )
182 throw io::IOException();
183 return sal_Int64( uPos
);
187 XInputStream_impl::getLength(
189 throw( io::IOException
,
190 uno::RuntimeException
, std::exception
)
192 osl::FileBase::RC err
;
193 sal_uInt64 uCurrentPos
, uEndPos
;
195 err
= m_aFile
.getPos( uCurrentPos
);
196 if( err
!= osl::FileBase::E_None
)
197 throw io::IOException();
199 err
= m_aFile
.setPos( osl_Pos_End
, 0 );
200 if( err
!= osl::FileBase::E_None
)
201 throw io::IOException();
203 err
= m_aFile
.getPos( uEndPos
);
204 if( err
!= osl::FileBase::E_None
)
205 throw io::IOException();
207 err
= m_aFile
.setPos( osl_Pos_Absolut
, uCurrentPos
);
208 if( err
!= osl::FileBase::E_None
)
209 throw io::IOException();
211 return sal_Int64( uEndPos
);
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */