LibreOffice Module comphelper (master) 1
oslfile2streamwrap.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#include <sal/config.h>
21
22#include <com/sun/star/io/BufferSizeExceededException.hpp>
23#include <com/sun/star/io/NotConnectedException.hpp>
25#include <o3tl/safeint.hxx>
26#include <osl/file.hxx>
27
28#include <algorithm>
29
30namespace comphelper
31{
32 using namespace osl;
33
34
36 : m_pFile(&_rFile)
37{
38}
39
40
42{
43}
44
45
46sal_Int32 SAL_CALL OSLInputStreamWrapper::readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
47{
48 if (!m_pFile)
49 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
50
51 if (nBytesToRead < 0)
52 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
53
54 aData.realloc(nBytesToRead);
55
56 std::scoped_lock aGuard( m_aMutex );
57
58 sal_uInt64 nRead = 0;
59 FileBase::RC eError = m_pFile->read(static_cast<void*>(aData.getArray()), nBytesToRead, nRead);
60 if (eError != FileBase::E_None)
61 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
62
63 // If the read character < MaxLength, adjust css::uno::Sequence
64 if (nRead < o3tl::make_unsigned(nBytesToRead))
65 aData.realloc( sal::static_int_cast< sal_Int32 >(nRead) );
66
67 return sal::static_int_cast< sal_Int32 >(nRead);
68}
69
70sal_Int32 SAL_CALL OSLInputStreamWrapper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead)
71{
72 if (!m_pFile)
73 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
74
75 if (nMaxBytesToRead < 0)
76 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
77
78 return readBytes(aData, nMaxBytesToRead);
79}
80
81void SAL_CALL OSLInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip)
82{
83 std::scoped_lock aGuard( m_aMutex );
84 if (!m_pFile)
85 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
86
87 sal_uInt64 nCurrentPos;
88 FileBase::RC eError = m_pFile->getPos(nCurrentPos);
89 if (eError != FileBase::E_None)
90 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
91
92 sal_uInt64 nNewPos = nCurrentPos + nBytesToSkip;
93 eError = m_pFile->setPos(osl_Pos_Absolut, nNewPos);
94 if (eError != FileBase::E_None)
95 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
96}
97
99{
100 std::scoped_lock aGuard( m_aMutex );
101 if (!m_pFile)
102 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
103
104 sal_uInt64 nPos;
105 FileBase::RC eError = m_pFile->getPos(nPos);
106 if (eError != FileBase::E_None)
107 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
108
109 eError = m_pFile->setPos(osl_Pos_End, 0);
110 if (eError != FileBase::E_None)
111 throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
112
113 sal_uInt64 nAvailable;
114 eError = m_pFile->getPos(nAvailable);
115 if (eError != FileBase::E_None)
116 throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
117
118 nAvailable = nAvailable - nPos;
119 eError = m_pFile->setPos(osl_Pos_Absolut, nPos);
120 if (eError != FileBase::E_None)
121 throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
122 return std::min<sal_Int64>(nAvailable, SAL_MAX_INT32);
123}
124
125
127{
128 if (!m_pFile)
129 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
130
131 m_pFile->close();
132
133 m_pFile = nullptr;
134}
135
136/*************************************************************************/
137// css::io::XOutputStream
138
139
141 rFile(_rFile)
142{}
143
145
146void SAL_CALL OSLOutputStreamWrapper::writeBytes(const css::uno::Sequence< sal_Int8 >& aData)
147{
148 sal_uInt64 nWritten;
149 FileBase::RC eError = rFile.write(aData.getConstArray(),aData.getLength(), nWritten);
150 if (eError != FileBase::E_None
151 || nWritten != sal::static_int_cast< sal_uInt32 >(aData.getLength()))
152 {
153 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
154 }
155}
156
157
159{
160}
161
162
164{
165 rFile.close();
166}
167
168} // namespace comphelper
169
170
171/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL closeInput() override
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
virtual sal_Int32 SAL_CALL available() override
COMPHELPER_DLLPUBLIC OSLInputStreamWrapper(::osl::File &_rStream)
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
virtual void SAL_CALL flush() override
virtual void SAL_CALL closeOutput() override
virtual void SAL_CALL writeBytes(const css::uno::Sequence< sal_Int8 > &aData) override
COMPHELPER_DLLPUBLIC OSLOutputStreamWrapper(::osl::File &_rFile)
std::unique_ptr< ::osl::File > m_pFile
sal_uInt16 nPos
constexpr OUStringLiteral aData
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
#define SAL_MAX_INT32