bump product version to 4.1.6.2
[LibreOffice.git] / comphelper / source / streaming / oslfile2streamwrap.cxx
blob23764161a316dbaef0e7e0d9ec9039beecc58ac8
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;
28 //------------------------------------------------------------------
29 OSLInputStreamWrapper::OSLInputStreamWrapper( File& _rFile )
30 : m_pFile(&_rFile)
34 //------------------------------------------------------------------
35 OSLInputStreamWrapper::~OSLInputStreamWrapper()
39 //------------------------------------------------------------------------------
40 sal_Int32 SAL_CALL OSLInputStreamWrapper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
41 throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
43 if (!m_pFile)
44 throw stario::NotConnectedException(OUString(), static_cast<staruno::XWeak*>(this));
46 if (nBytesToRead < 0)
47 throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::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 stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak*>(this));
58 // Wenn gelesene Zeichen < MaxLength, staruno::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);
65 //------------------------------------------------------------------------------
66 sal_Int32 SAL_CALL OSLInputStreamWrapper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
68 if (!m_pFile)
69 throw stario::NotConnectedException(OUString(), static_cast<staruno::XWeak*>(this));
71 if (nMaxBytesToRead < 0)
72 throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak*>(this));
74 return readBytes(aData, nMaxBytesToRead);
77 //------------------------------------------------------------------------------
78 void SAL_CALL OSLInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
80 ::osl::MutexGuard aGuard( m_aMutex );
81 if (!m_pFile)
82 throw stario::NotConnectedException(OUString(), static_cast<staruno::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 stario::NotConnectedException(OUString(), static_cast<staruno::XWeak*>(this));
95 //------------------------------------------------------------------------------
96 sal_Int32 SAL_CALL OSLInputStreamWrapper::available() throw( stario::NotConnectedException, staruno::RuntimeException )
98 ::osl::MutexGuard aGuard( m_aMutex );
99 if (!m_pFile)
100 throw stario::NotConnectedException(OUString(), static_cast<staruno::XWeak*>(this));
102 sal_uInt64 nPos;
103 FileBase::RC eError = m_pFile->getPos(nPos);
104 if (eError != FileBase::E_None)
105 throw stario::NotConnectedException(OUString(), static_cast<staruno::XWeak*>(this));
107 sal_uInt64 nDummy = 0;
108 eError = m_pFile->setPos(osl_Pos_End, nDummy);
109 if (eError != FileBase::E_None)
110 throw stario::NotConnectedException(OUString(),static_cast<staruno::XWeak*>(this));
112 sal_uInt64 nAvailable;
113 eError = m_pFile->getPos(nAvailable);
114 if (eError != FileBase::E_None)
115 throw stario::NotConnectedException(OUString(),static_cast<staruno::XWeak*>(this));
117 nAvailable = nAvailable - nPos;
118 eError = m_pFile->setPos(osl_Pos_Absolut, nPos);
119 if (eError != FileBase::E_None)
120 throw stario::NotConnectedException(OUString(),static_cast<staruno::XWeak*>(this));
121 return sal::static_int_cast< sal_Int32 >(
122 std::max(nAvailable, sal::static_int_cast< sal_uInt64 >(SAL_MAX_INT32)));
125 //------------------------------------------------------------------------------
126 void SAL_CALL OSLInputStreamWrapper::closeInput() throw( stario::NotConnectedException, staruno::RuntimeException )
128 if (!m_pFile)
129 throw stario::NotConnectedException(OUString(), static_cast<staruno::XWeak*>(this));
131 m_pFile->close();
133 m_pFile = NULL;
136 /*************************************************************************/
137 // stario::XOutputStream
138 //------------------------------------------------------------------------------
140 OSLOutputStreamWrapper::OSLOutputStreamWrapper(osl::File & _rFile):
141 rFile(_rFile)
144 OSLOutputStreamWrapper::~OSLOutputStreamWrapper() {}
146 void SAL_CALL OSLOutputStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
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 stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak*>(this));
157 //------------------------------------------------------------------
158 void SAL_CALL OSLOutputStreamWrapper::flush() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
162 //------------------------------------------------------------------
163 void SAL_CALL OSLOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
165 rFile.close();
168 } // namespace comphelper
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */