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>
24 #include <comphelper/oslfile2streamwrap.hxx>
25 #include <o3tl/safeint.hxx>
26 #include <osl/file.hxx>
27 
28 #include <algorithm>
29 
30 namespace comphelper
31 {
32     using namespace osl;
33 
34 
OSLInputStreamWrapper(File & _rFile)35 OSLInputStreamWrapper::OSLInputStreamWrapper( File& _rFile )
36     : m_pFile(&_rFile)
37 {
38 }
39 
40 
~OSLInputStreamWrapper()41 OSLInputStreamWrapper::~OSLInputStreamWrapper()
42 {
43 }
44 
45 
readBytes(css::uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)46 sal_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     ::osl::MutexGuard aGuard( m_aMutex );
55 
56     aData.realloc(nBytesToRead);
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 
readSomeBytes(css::uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)70 sal_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 
skipBytes(sal_Int32 nBytesToSkip)81 void SAL_CALL OSLInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip)
82 {
83     ::osl::MutexGuard 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 
available()98 sal_Int32 SAL_CALL OSLInputStreamWrapper::available()
99 {
100     ::osl::MutexGuard 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 
closeInput()126 void SAL_CALL OSLInputStreamWrapper::closeInput()
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 
OSLOutputStreamWrapper(osl::File & _rFile)140 OSLOutputStreamWrapper::OSLOutputStreamWrapper(osl::File & _rFile):
141     rFile(_rFile)
142 {}
143 
~OSLOutputStreamWrapper()144 OSLOutputStreamWrapper::~OSLOutputStreamWrapper() {}
145 
writeBytes(const css::uno::Sequence<sal_Int8> & aData)146 void 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 
flush()158 void SAL_CALL OSLOutputStreamWrapper::flush()
159 {
160 }
161 
162 
closeOutput()163 void SAL_CALL OSLOutputStreamWrapper::closeOutput()
164 {
165     rFile.close();
166 }
167 
168 } // namespace comphelper
169 
170 
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
172