merged tag ooo/OOO330_m14
[LibreOffice.git] / comphelper / source / streaming / oslfile2streamwrap.cxx
blob9cf8eeaaf08a005b73b36df0edb61aa0078ddf32
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/oslfile2streamwrap.hxx>
32 #include <algorithm>
34 namespace comphelper
36 using namespace osl;
38 //------------------------------------------------------------------
39 OSLInputStreamWrapper::OSLInputStreamWrapper( File& _rFile )
40 :m_pFile(&_rFile)
41 ,m_bFileOwner(sal_False)
45 //------------------------------------------------------------------
46 OSLInputStreamWrapper::OSLInputStreamWrapper( File* pStream, sal_Bool bOwner )
47 :m_pFile( pStream )
48 ,m_bFileOwner( bOwner )
52 //------------------------------------------------------------------
53 OSLInputStreamWrapper::~OSLInputStreamWrapper()
55 if( m_bFileOwner )
56 delete m_pFile;
59 //------------------------------------------------------------------------------
60 sal_Int32 SAL_CALL OSLInputStreamWrapper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
61 throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
63 if (!m_pFile)
64 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
66 if (nBytesToRead < 0)
67 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
69 ::osl::MutexGuard aGuard( m_aMutex );
71 aData.realloc(nBytesToRead);
73 sal_uInt64 nRead = 0;
74 FileBase::RC eError = m_pFile->read((void*)aData.getArray(), nBytesToRead, nRead);
75 if (eError != FileBase::E_None)
76 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
78 // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
79 if (nRead < (sal_uInt32)nBytesToRead)
80 aData.realloc( sal::static_int_cast< sal_Int32 >(nRead) );
82 return sal::static_int_cast< sal_Int32 >(nRead);
85 //------------------------------------------------------------------------------
86 sal_Int32 SAL_CALL OSLInputStreamWrapper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
88 if (!m_pFile)
89 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
91 if (nMaxBytesToRead < 0)
92 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
95 if (m_pFile->IsEof())
97 aData.realloc(0);
98 return 0;
100 else
102 return readBytes(aData, nMaxBytesToRead);
105 //------------------------------------------------------------------------------
106 void SAL_CALL OSLInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
108 ::osl::MutexGuard aGuard( m_aMutex );
109 if (!m_pFile)
110 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
112 sal_uInt64 nCurrentPos;
113 m_pFile->getPos(nCurrentPos);
115 sal_uInt64 nNewPos = nCurrentPos + nBytesToSkip;
116 FileBase::RC eError = m_pFile->setPos(osl_Pos_Absolut, nNewPos);
117 if (eError != FileBase::E_None)
119 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
122 #ifdef DBG_UTIL
123 m_pFile->getPos(nCurrentPos);
124 // volatile int dummy = 0; // to take a look at last changes ;-)
125 #endif
128 //------------------------------------------------------------------------------
129 sal_Int32 SAL_CALL OSLInputStreamWrapper::available() throw( stario::NotConnectedException, staruno::RuntimeException )
131 ::osl::MutexGuard aGuard( m_aMutex );
132 if (!m_pFile)
133 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
135 sal_uInt64 nPos;
136 FileBase::RC eError = m_pFile->getPos(nPos);
137 if (eError != FileBase::E_None)
138 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
140 sal_uInt64 nDummy = 0;
141 eError = m_pFile->setPos(Pos_End, nDummy);
142 if (eError != FileBase::E_None)
143 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
145 sal_uInt64 nAvailable;
146 eError = m_pFile->getPos(nAvailable);
147 if (eError != FileBase::E_None)
148 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
150 nAvailable = nAvailable - nPos;
151 eError = m_pFile->setPos(Pos_Absolut, nPos);
152 if (eError != FileBase::E_None)
153 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
154 return sal::static_int_cast< sal_Int32 >(
155 std::max(nAvailable, sal::static_int_cast< sal_uInt64 >(SAL_MAX_INT32)));
158 //------------------------------------------------------------------------------
159 void SAL_CALL OSLInputStreamWrapper::closeInput() throw( stario::NotConnectedException, staruno::RuntimeException )
161 if (!m_pFile)
162 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
164 m_pFile->close();
165 if (m_bFileOwner)
166 delete m_pFile;
168 m_pFile = NULL;
171 /*************************************************************************/
172 // stario::XOutputStream
173 //------------------------------------------------------------------------------
174 void SAL_CALL OSLOutputStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
176 sal_uInt64 nWritten;
177 FileBase::RC eError = rFile.write(aData.getConstArray(),aData.getLength(), nWritten);
178 if (eError != FileBase::E_None
179 || nWritten != sal::static_int_cast< sal_uInt32 >(aData.getLength()))
181 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
185 //------------------------------------------------------------------
186 void SAL_CALL OSLOutputStreamWrapper::flush() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
190 //------------------------------------------------------------------
191 void SAL_CALL OSLOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
193 rFile.close();
196 } // namespace comphelper