Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / ucb / source / ucp / file / filinpstr.cxx
blob5838dcda39ac6109bbb462df60e92ac46c5e4508
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 <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
33 #else
34 #define THROW_WHERE ""
35 #endif
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;
43 if ( !bLock )
44 nFlags |= osl_File_OpenFlag_NoLock;
46 osl::FileBase::RC err = m_aFile.open( nFlags );
47 if( err != osl::FileBase::E_None )
49 m_nIsOpen = false;
50 m_aFile.close();
52 m_nErrorCode = TASKHANDLING_OPEN_FOR_INPUTSTREAM;
53 m_nMinorErrorCode = err;
55 else
56 m_nIsOpen = true;
60 XInputStream_impl::~XInputStream_impl()
62 try
64 closeInput();
66 catch (io::IOException const &)
68 OSL_FAIL("unexpected situation");
70 catch (uno::RuntimeException const &)
72 OSL_FAIL("unexpected situation");
76 sal_Int32 SAL_CALL
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
87 sal_uInt64 nrc(0);
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);
100 sal_Int32 SAL_CALL
101 XInputStream_impl::readSomeBytes(
102 uno::Sequence< sal_Int8 >& aData,
103 sal_Int32 nMaxBytesToRead )
105 return readBytes( aData,nMaxBytesToRead );
109 void SAL_CALL
110 XInputStream_impl::skipBytes( sal_Int32 nBytesToSkip )
112 m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) );
116 sal_Int32 SAL_CALL
117 XInputStream_impl::available()
119 sal_Int64 avail = getLength() - getPosition();
120 return std::min<sal_Int64>(avail, SAL_MAX_INT32);
124 void SAL_CALL
125 XInputStream_impl::closeInput()
127 if( m_nIsOpen )
129 osl::FileBase::RC err = m_aFile.close();
130 if( err != osl::FileBase::E_None )
131 throw io::IOException( THROW_WHERE );
132 m_nIsOpen = false;
137 void SAL_CALL
138 XInputStream_impl::seek( sal_Int64 location )
140 if( location < 0 )
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 );
147 sal_Int64 SAL_CALL
148 XInputStream_impl::getPosition()
150 sal_uInt64 uPos;
151 if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
152 throw io::IOException( THROW_WHERE );
153 return sal_Int64( uPos );
156 sal_Int64 SAL_CALL
157 XInputStream_impl::getLength()
159 sal_uInt64 uEndPos;
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: */