Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / comphelper / source / streaming / oslfile2streamwrap.cxx
blob243634610c2b1e1716f3687a7d77b2b2c32453a3
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/BufferSizeExceededException.hpp>
23 #include <com/sun/star/io/NotConnectedException.hpp>
24 #include <comphelper/oslfile2streamwrap.hxx>
25 #include <o3tl/safeint.hxx>
26 #include <osl/file.hxx>
28 #include <algorithm>
30 namespace comphelper
32 using namespace osl;
35 OSLInputStreamWrapper::OSLInputStreamWrapper( File& _rFile )
36 : m_pFile(&_rFile)
41 OSLInputStreamWrapper::~OSLInputStreamWrapper()
46 sal_Int32 SAL_CALL OSLInputStreamWrapper::readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
48 if (!m_pFile)
49 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
51 if (nBytesToRead < 0)
52 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
54 aData.realloc(nBytesToRead);
56 std::scoped_lock aGuard( m_aMutex );
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));
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) );
67 return sal::static_int_cast< sal_Int32 >(nRead);
70 sal_Int32 SAL_CALL OSLInputStreamWrapper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead)
72 if (!m_pFile)
73 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
75 if (nMaxBytesToRead < 0)
76 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
78 return readBytes(aData, nMaxBytesToRead);
81 void SAL_CALL OSLInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip)
83 std::scoped_lock aGuard( m_aMutex );
84 if (!m_pFile)
85 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
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));
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));
98 sal_Int32 SAL_CALL OSLInputStreamWrapper::available()
100 std::scoped_lock aGuard( m_aMutex );
101 if (!m_pFile)
102 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
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));
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));
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));
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);
126 void SAL_CALL OSLInputStreamWrapper::closeInput()
128 if (!m_pFile)
129 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
131 m_pFile->close();
133 m_pFile = nullptr;
136 /*************************************************************************/
137 // css::io::XOutputStream
140 OSLOutputStreamWrapper::OSLOutputStreamWrapper(osl::File & _rFile):
141 rFile(_rFile)
144 OSLOutputStreamWrapper::~OSLOutputStreamWrapper() {}
146 void SAL_CALL OSLOutputStreamWrapper::writeBytes(const css::uno::Sequence< sal_Int8 >& aData)
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()))
153 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
158 void SAL_CALL OSLOutputStreamWrapper::flush()
163 void SAL_CALL OSLOutputStreamWrapper::closeOutput()
165 rFile.close();
168 } // namespace comphelper
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */