bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / streaming / oslfile2streamwrap.cxx
blob4b9aad443b6c88afa56fd37ea25f6252405e278c
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 <comphelper/oslfile2streamwrap.hxx>
22 #include <algorithm>
24 namespace comphelper
26 using namespace osl;
29 OSLInputStreamWrapper::OSLInputStreamWrapper( File& _rFile )
30 : m_pFile(&_rFile)
35 OSLInputStreamWrapper::~OSLInputStreamWrapper()
40 sal_Int32 SAL_CALL OSLInputStreamWrapper::readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
41 throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
43 if (!m_pFile)
44 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
46 if (nBytesToRead < 0)
47 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
49 ::osl::MutexGuard aGuard( m_aMutex );
51 aData.realloc(nBytesToRead);
53 sal_uInt64 nRead = 0;
54 FileBase::RC eError = m_pFile->read((void*)aData.getArray(), nBytesToRead, nRead);
55 if (eError != FileBase::E_None)
56 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
58 // Wenn gelesene Zeichen < MaxLength, css::uno::Sequence anpassen
59 if (nRead < (sal_uInt32)nBytesToRead)
60 aData.realloc( sal::static_int_cast< sal_Int32 >(nRead) );
62 return sal::static_int_cast< sal_Int32 >(nRead);
66 sal_Int32 SAL_CALL OSLInputStreamWrapper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
68 if (!m_pFile)
69 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
71 if (nMaxBytesToRead < 0)
72 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
74 return readBytes(aData, nMaxBytesToRead);
78 void SAL_CALL OSLInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
80 ::osl::MutexGuard aGuard( m_aMutex );
81 if (!m_pFile)
82 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
84 sal_uInt64 nCurrentPos;
85 m_pFile->getPos(nCurrentPos);
87 sal_uInt64 nNewPos = nCurrentPos + nBytesToSkip;
88 FileBase::RC eError = m_pFile->setPos(osl_Pos_Absolut, nNewPos);
89 if (eError != FileBase::E_None)
91 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
96 sal_Int32 SAL_CALL OSLInputStreamWrapper::available() throw( css::io::NotConnectedException, css::uno::RuntimeException, std::exception )
98 ::osl::MutexGuard aGuard( m_aMutex );
99 if (!m_pFile)
100 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
102 sal_uInt64 nPos;
103 FileBase::RC eError = m_pFile->getPos(nPos);
104 if (eError != FileBase::E_None)
105 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
107 sal_uInt64 nDummy = 0;
108 eError = m_pFile->setPos(osl_Pos_End, nDummy);
109 if (eError != FileBase::E_None)
110 throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
112 sal_uInt64 nAvailable;
113 eError = m_pFile->getPos(nAvailable);
114 if (eError != FileBase::E_None)
115 throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
117 nAvailable = nAvailable - nPos;
118 eError = m_pFile->setPos(osl_Pos_Absolut, nPos);
119 if (eError != FileBase::E_None)
120 throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
121 return sal::static_int_cast< sal_Int32 >(
122 std::max(nAvailable, sal::static_int_cast< sal_uInt64 >(SAL_MAX_INT32)));
126 void SAL_CALL OSLInputStreamWrapper::closeInput() throw( css::io::NotConnectedException, css::uno::RuntimeException, std::exception )
128 if (!m_pFile)
129 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
131 m_pFile->close();
133 m_pFile = NULL;
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) throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
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() throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
163 void SAL_CALL OSLOutputStreamWrapper::closeOutput() throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
165 rFile.close();
168 } // namespace comphelper
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */