Simplify using designated initializers
[LibreOffice.git] / xmlhelp / source / cxxhelp / provider / inputstream.cxx
blob30b2e1e2e4dbba0ca820a855901172aa699641d7
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 .
21 #include "inputstream.hxx"
23 #include <com/sun/star/io/IOException.hpp>
24 #include <com/sun/star/lang/IllegalArgumentException.hpp>
25 #include <cppuhelper/queryinterface.hxx>
28 using namespace chelp;
29 using namespace com::sun::star;
32 XInputStream_impl::XInputStream_impl( const OUString& aUncPath )
33 : m_bIsOpen( false ),
34 m_aFile( aUncPath )
36 m_bIsOpen = ( osl::FileBase::E_None == m_aFile.open( osl_File_OpenFlag_Read ) );
39 XInputStream_impl::~XInputStream_impl()
41 if (m_bIsOpen)
42 m_aFile.close();
45 uno::Any SAL_CALL
46 XInputStream_impl::queryInterface( const uno::Type& rType )
48 uno::Any aRet = cppu::queryInterface( rType,
49 static_cast< io::XInputStream* >(this),
50 static_cast< io::XSeekable* >(this) );
51 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
55 void SAL_CALL
56 XInputStream_impl::acquire()
57 noexcept
59 OWeakObject::acquire();
63 void SAL_CALL
64 XInputStream_impl::release()
65 noexcept
67 OWeakObject::release();
71 sal_Int32 SAL_CALL
72 XInputStream_impl::readBytes(
73 uno::Sequence< sal_Int8 >& aData,
74 sal_Int32 nBytesToRead )
76 if( ! m_bIsOpen )
77 throw io::IOException();
79 if (aData.getLength() < nBytesToRead)
80 aData.realloc(nBytesToRead);
81 //TODO! translate memory exhaustion (if it were detectable...) into
82 // io::BufferSizeExceededException
84 sal_uInt64 nBytesRead;
85 m_aFile.read( aData.getArray(), sal_uInt64(nBytesToRead), nBytesRead );
87 // Shrink aData in case we read less than nBytesToRead (XInputStream
88 // documentation does not tell whether this is required, and I do not know
89 // if any code relies on this, so be conservative---SB):
90 if (nBytesRead != sal::static_int_cast<sal_uInt64>(nBytesToRead) )
91 aData.realloc(sal_Int32(nBytesRead));
92 return static_cast<sal_Int32>(nBytesRead);
95 sal_Int32 SAL_CALL
96 XInputStream_impl::readSomeBytes(
97 uno::Sequence< sal_Int8 >& aData,
98 sal_Int32 nMaxBytesToRead )
100 return readBytes( aData,nMaxBytesToRead );
104 void SAL_CALL
105 XInputStream_impl::skipBytes(
106 sal_Int32 nBytesToSkip )
108 if (m_aFile.setPos(osl_Pos_Current, sal_uInt64(nBytesToSkip)) != osl::FileBase::E_None)
110 throw io::IOException(u"XInputStream_impl::skipBytes failed seek"_ustr);
115 sal_Int32 SAL_CALL
116 XInputStream_impl::available()
118 sal_uInt64 uPos;
119 if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
120 throw io::IOException();
121 sal_uInt64 uSize;
122 if( osl::FileBase::E_None != m_aFile.getSize( uSize ) )
123 throw io::IOException();
124 return std::min<sal_uInt64>(SAL_MAX_INT32, uSize - uPos);
128 void SAL_CALL
129 XInputStream_impl::closeInput()
131 if( m_bIsOpen )
133 osl::FileBase::RC err = m_aFile.close();
134 if( err != osl::FileBase::E_None )
135 throw io::IOException();
136 m_bIsOpen = false;
141 void SAL_CALL
142 XInputStream_impl::seek( sal_Int64 location )
144 if( location < 0 )
145 throw lang::IllegalArgumentException();
146 if( osl::FileBase::E_None != m_aFile.setPos( osl_Pos_Absolut, sal_uInt64( location ) ) )
147 throw io::IOException();
151 sal_Int64 SAL_CALL
152 XInputStream_impl::getPosition()
154 sal_uInt64 uPos;
155 if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
156 throw io::IOException();
157 return sal_Int64( uPos );
160 sal_Int64 SAL_CALL
161 XInputStream_impl::getLength()
163 osl::FileBase::RC err;
164 sal_uInt64 uCurrentPos, uEndPos;
166 err = m_aFile.getPos( uCurrentPos );
167 if( err != osl::FileBase::E_None )
168 throw io::IOException();
170 err = m_aFile.setPos( osl_Pos_End, 0 );
171 if( err != osl::FileBase::E_None )
172 throw io::IOException();
174 err = m_aFile.getPos( uEndPos );
175 if( err != osl::FileBase::E_None )
176 throw io::IOException();
178 err = m_aFile.setPos( osl_Pos_Absolut, uCurrentPos );
179 if( err != osl::FileBase::E_None )
180 throw io::IOException();
182 return sal_Int64( uEndPos );
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */