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 .
20 #include <sal/config.h>
22 #include <com/sun/star/io/IOException.hpp>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
25 #include "filinpstr.hxx"
26 #include "filerror.hxx"
28 using namespace fileaccess
;
29 using namespace com::sun::star
;
31 #if OSL_DEBUG_LEVEL > 0
32 #define THROW_WHERE SAL_WHERE
34 #define THROW_WHERE ""
37 XInputStream_impl::XInputStream_impl( const OUString
& aUncPath
, bool bLock
)
38 : m_aFile( aUncPath
),
39 m_nErrorCode( TASKHANDLER_NO_ERROR
),
40 m_nMinorErrorCode( TASKHANDLER_NO_ERROR
)
42 sal_uInt32 nFlags
= osl_File_OpenFlag_Read
;
44 nFlags
|= osl_File_OpenFlag_NoLock
;
46 osl::FileBase::RC err
= m_aFile
.open( nFlags
);
47 if( err
!= osl::FileBase::E_None
)
52 m_nErrorCode
= TASKHANDLING_OPEN_FOR_INPUTSTREAM
;
53 m_nMinorErrorCode
= err
;
60 XInputStream_impl::~XInputStream_impl()
66 catch (io::IOException
const &)
68 OSL_FAIL("unexpected situation");
70 catch (uno::RuntimeException
const &)
72 OSL_FAIL("unexpected situation");
77 XInputStream_impl::readBytes(
78 uno::Sequence
< sal_Int8
>& aData
,
79 sal_Int32 nBytesToRead
)
81 if( ! m_nIsOpen
) throw io::IOException( THROW_WHERE
);
83 aData
.realloc(nBytesToRead
);
84 //TODO! translate memory exhaustion (if it were detectable...) into
85 // io::BufferSizeExceededException
88 if(m_aFile
.read( aData
.getArray(),sal_uInt64(nBytesToRead
),nrc
)
89 != osl::FileBase::E_None
)
90 throw io::IOException( THROW_WHERE
);
92 // Shrink aData in case we read less than nBytesToRead (XInputStream
93 // documentation does not tell whether this is required, and I do not know
94 // if any code relies on this, so be conservative---SB):
95 if (sal::static_int_cast
<sal_Int32
>(nrc
) != nBytesToRead
)
96 aData
.realloc(sal_Int32(nrc
));
97 return static_cast<sal_Int32
>(nrc
);
101 XInputStream_impl::readSomeBytes(
102 uno::Sequence
< sal_Int8
>& aData
,
103 sal_Int32 nMaxBytesToRead
)
105 return readBytes( aData
,nMaxBytesToRead
);
110 XInputStream_impl::skipBytes( sal_Int32 nBytesToSkip
)
112 m_aFile
.setPos( osl_Pos_Current
, sal_uInt64( nBytesToSkip
) );
117 XInputStream_impl::available()
119 sal_Int64 avail
= getLength() - getPosition();
120 return std::min
<sal_Int64
>(avail
, SAL_MAX_INT32
);
125 XInputStream_impl::closeInput()
129 osl::FileBase::RC err
= m_aFile
.close();
130 if( err
!= osl::FileBase::E_None
)
131 throw io::IOException( THROW_WHERE
);
138 XInputStream_impl::seek( sal_Int64 location
)
141 throw lang::IllegalArgumentException( THROW_WHERE
, uno::Reference
< uno::XInterface
>(), 0 );
142 if( osl::FileBase::E_None
!= m_aFile
.setPos( osl_Pos_Absolut
, sal_uInt64( location
) ) )
143 throw io::IOException( THROW_WHERE
);
148 XInputStream_impl::getPosition()
151 if( osl::FileBase::E_None
!= m_aFile
.getPos( uPos
) )
152 throw io::IOException( THROW_WHERE
);
153 return sal_Int64( uPos
);
157 XInputStream_impl::getLength()
160 if ( m_aFile
.getSize(uEndPos
) != osl::FileBase::E_None
)
161 throw io::IOException( THROW_WHERE
);
162 return sal_Int64( uEndPos
);
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */