Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/ucb/source/ucp/file/filinpstr.cxx
Line
Count
Source (jump to first uncovered line)
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
#include <sal/config.h>
21
22
#include <com/sun/star/io/IOException.hpp>
23
#include <com/sun/star/lang/IllegalArgumentException.hpp>
24
25
#include <osl/diagnose.h>
26
27
#include "filinpstr.hxx"
28
#include "filerror.hxx"
29
30
using namespace fileaccess;
31
using namespace com::sun::star;
32
33
#if OSL_DEBUG_LEVEL > 0
34
#define THROW_WHERE SAL_WHERE
35
#else
36
0
#define THROW_WHERE ""
37
#endif
38
39
XInputStream_impl::XInputStream_impl( const OUString& aUncPath, bool bLock )
40
8.47k
    : m_aFile( aUncPath ),
41
8.47k
      m_nErrorCode( TaskHandlerErr::NO_ERROR ),
42
8.47k
      m_nMinorErrorCode( 0 )
43
8.47k
{
44
8.47k
    sal_uInt32 nFlags = osl_File_OpenFlag_Read;
45
8.47k
    if ( !bLock )
46
0
        nFlags |= osl_File_OpenFlag_NoLock;
47
48
8.47k
    osl::FileBase::RC err = m_aFile.open( nFlags );
49
8.47k
    if( err != osl::FileBase::E_None )
50
0
    {
51
0
        m_nIsOpen = false;
52
0
        m_aFile.close();
53
54
0
        m_nErrorCode = TaskHandlerErr::OPEN_FOR_INPUTSTREAM;
55
0
        m_nMinorErrorCode = err;
56
0
    }
57
8.47k
    else
58
8.47k
        m_nIsOpen = true;
59
8.47k
}
60
61
62
XInputStream_impl::~XInputStream_impl()
63
8.47k
{
64
8.47k
    try
65
8.47k
    {
66
8.47k
        closeInput();
67
8.47k
    }
68
8.47k
    catch (io::IOException const &)
69
8.47k
    {
70
0
        OSL_FAIL("unexpected situation");
71
0
    }
72
8.47k
    catch (uno::RuntimeException const &)
73
8.47k
    {
74
0
        OSL_FAIL("unexpected situation");
75
0
    }
76
8.47k
}
77
78
sal_Int32 SAL_CALL
79
XInputStream_impl::readBytes(
80
                 uno::Sequence< sal_Int8 >& aData,
81
                 sal_Int32 nBytesToRead )
82
0
{
83
0
    if( ! m_nIsOpen ) throw io::IOException( THROW_WHERE );
84
85
0
    aData.realloc(nBytesToRead);
86
        //TODO! translate memory exhaustion (if it were detectable...) into
87
        // io::BufferSizeExceededException
88
89
0
    sal_uInt64 nrc(0);
90
0
    if(m_aFile.read( aData.getArray(),sal_uInt64(nBytesToRead),nrc )
91
0
       != osl::FileBase::E_None)
92
0
        throw io::IOException( THROW_WHERE );
93
94
    // Shrink aData in case we read less than nBytesToRead (XInputStream
95
    // documentation does not tell whether this is required, and I do not know
96
    // if any code relies on this, so be conservative---SB):
97
0
    if (sal::static_int_cast<sal_Int32>(nrc) != nBytesToRead)
98
0
        aData.realloc(sal_Int32(nrc));
99
0
    return static_cast<sal_Int32>(nrc);
100
0
}
101
102
sal_Int32
103
XInputStream_impl::readSomeBytes(
104
                 sal_Int8* aData,
105
                 sal_Int32 nBytesToRead )
106
9.55k
{
107
9.55k
    if( ! m_nIsOpen ) throw io::IOException( THROW_WHERE );
108
109
        //TODO! translate memory exhaustion (if it were detectable...) into
110
        // io::BufferSizeExceededException
111
112
9.55k
    sal_uInt64 nrc(0);
113
9.55k
    if(m_aFile.read( aData, sal_uInt64(nBytesToRead),nrc )
114
9.55k
       != osl::FileBase::E_None)
115
0
        throw io::IOException( THROW_WHERE );
116
117
9.55k
    return static_cast<sal_Int32>(nrc);
118
9.55k
}
119
120
sal_Int32 SAL_CALL
121
XInputStream_impl::readSomeBytes(
122
    uno::Sequence< sal_Int8 >& aData,
123
    sal_Int32 nMaxBytesToRead )
124
0
{
125
0
    return readBytes( aData,nMaxBytesToRead );
126
0
}
127
128
129
void SAL_CALL
130
XInputStream_impl::skipBytes( sal_Int32 nBytesToSkip )
131
0
{
132
0
    m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) );
133
0
}
134
135
136
sal_Int32 SAL_CALL
137
XInputStream_impl::available()
138
0
{
139
0
    sal_Int64 avail = getLength() - getPosition();
140
0
    return std::min<sal_Int64>(avail, SAL_MAX_INT32);
141
0
}
142
143
144
void SAL_CALL
145
XInputStream_impl::closeInput()
146
8.47k
{
147
8.47k
    if( m_nIsOpen )
148
8.47k
    {
149
8.47k
        osl::FileBase::RC err = m_aFile.close();
150
8.47k
        if( err != osl::FileBase::E_None )
151
0
            throw io::IOException( THROW_WHERE );
152
8.47k
        m_nIsOpen = false;
153
8.47k
    }
154
8.47k
}
155
156
157
void SAL_CALL
158
XInputStream_impl::seek( sal_Int64 location )
159
9.55k
{
160
9.55k
    if( location < 0 )
161
0
        throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 0 );
162
9.55k
    if( osl::FileBase::E_None != m_aFile.setPos( osl_Pos_Absolut, sal_uInt64( location ) ) )
163
0
        throw io::IOException( THROW_WHERE );
164
9.55k
}
165
166
167
sal_Int64 SAL_CALL
168
XInputStream_impl::getPosition()
169
0
{
170
0
    sal_uInt64 uPos;
171
0
    if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
172
0
        throw io::IOException( THROW_WHERE );
173
0
    return sal_Int64( uPos );
174
0
}
175
176
sal_Int64 SAL_CALL
177
XInputStream_impl::getLength()
178
0
{
179
0
    sal_uInt64 uEndPos;
180
0
    if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None )
181
0
        throw io::IOException( THROW_WHERE );
182
0
    return sal_Int64( uEndPos );
183
0
}
184
185
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */