LibreOffice Module xmlhelp (master) 1
inputstream.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21#include "inputstream.hxx"
22
23#include <com/sun/star/io/IOException.hpp>
24#include <com/sun/star/lang/IllegalArgumentException.hpp>
26
27
28using namespace chelp;
29using namespace com::sun::star;
30
31
32XInputStream_impl::XInputStream_impl( const OUString& aUncPath )
33 : m_bIsOpen( false ),
34 m_aFile( aUncPath )
35{
36 m_bIsOpen = ( osl::FileBase::E_None == m_aFile.open( osl_File_OpenFlag_Read ) );
37}
38
40{
41 if (m_bIsOpen)
42 m_aFile.close();
43}
44
45uno::Any SAL_CALL
47{
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 );
52}
53
54
55void SAL_CALL
57 noexcept
58{
59 OWeakObject::acquire();
60}
61
62
63void SAL_CALL
65 noexcept
66{
67 OWeakObject::release();
68}
69
70
71sal_Int32 SAL_CALL
73 uno::Sequence< sal_Int8 >& aData,
74 sal_Int32 nBytesToRead )
75{
76 if( ! m_bIsOpen )
77 throw io::IOException();
78
79 if (aData.getLength() < nBytesToRead)
80 aData.realloc(nBytesToRead);
81 //TODO! translate memory exhaustion (if it were detectable...) into
82 // io::BufferSizeExceededException
83
84 sal_uInt64 nBytesRead;
85 m_aFile.read( aData.getArray(), sal_uInt64(nBytesToRead), nBytesRead );
86
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);
93}
94
95sal_Int32 SAL_CALL
97 uno::Sequence< sal_Int8 >& aData,
98 sal_Int32 nMaxBytesToRead )
99{
100 return readBytes( aData,nMaxBytesToRead );
101}
102
103
104void SAL_CALL
106 sal_Int32 nBytesToSkip )
107{
108 if (m_aFile.setPos(osl_Pos_Current, sal_uInt64(nBytesToSkip)) != osl::FileBase::E_None)
109 {
110 throw io::IOException("XInputStream_impl::skipBytes failed seek");
111 }
112}
113
114
115sal_Int32 SAL_CALL
117{
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);
125}
126
127
128void SAL_CALL
130{
131 if( m_bIsOpen )
132 {
133 osl::FileBase::RC err = m_aFile.close();
134 if( err != osl::FileBase::E_None )
135 throw io::IOException();
136 m_bIsOpen = false;
137 }
138}
139
140
141void SAL_CALL
142XInputStream_impl::seek( sal_Int64 location )
143{
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();
148}
149
150
151sal_Int64 SAL_CALL
153{
154 sal_uInt64 uPos;
155 if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
156 throw io::IOException();
157 return sal_Int64( uPos );
158}
159
160sal_Int64 SAL_CALL
162{
163 osl::FileBase::RC err;
164 sal_uInt64 uCurrentPos, uEndPos;
165
166 err = m_aFile.getPos( uCurrentPos );
167 if( err != osl::FileBase::E_None )
168 throw io::IOException();
169
170 err = m_aFile.setPos( osl_Pos_End, 0 );
171 if( err != osl::FileBase::E_None )
172 throw io::IOException();
173
174 err = m_aFile.getPos( uEndPos );
175 if( err != osl::FileBase::E_None )
176 throw io::IOException();
177
178 err = m_aFile.setPos( osl_Pos_Absolut, uCurrentPos );
179 if( err != osl::FileBase::E_None )
180 throw io::IOException();
181
182 return sal_Int64( uEndPos );
183}
184
185/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL closeInput() override
virtual void SAL_CALL acquire() noexcept override
Definition: inputstream.cxx:56
virtual sal_Int32 SAL_CALL available() override
virtual sal_Int64 SAL_CALL getPosition() override
virtual void SAL_CALL release() noexcept override
Definition: inputstream.cxx:64
virtual void SAL_CALL seek(sal_Int64 location) override
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
virtual ~XInputStream_impl() override
Definition: inputstream.cxx:39
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
Definition: inputstream.cxx:46
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
virtual sal_Int64 SAL_CALL getLength() override
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
constexpr OUStringLiteral aData
err
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
bool hasValue()
#define SAL_MAX_INT32