Update ooo320-m1
[ooovba.git] / comphelper / source / streaming / oslfile2streamwrap.cxx
blob599e26dcb403f7b3a8e352000dfb100392953f4c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: oslfile2streamwrap.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <comphelper/oslfile2streamwrap.hxx>
35 #include <algorithm>
37 namespace comphelper
39 using namespace osl;
41 //------------------------------------------------------------------
42 OSLInputStreamWrapper::OSLInputStreamWrapper( File& _rFile )
43 :m_pFile(&_rFile)
44 ,m_bFileOwner(sal_False)
48 //------------------------------------------------------------------
49 OSLInputStreamWrapper::OSLInputStreamWrapper( File* pStream, sal_Bool bOwner )
50 :m_pFile( pStream )
51 ,m_bFileOwner( bOwner )
55 //------------------------------------------------------------------
56 OSLInputStreamWrapper::~OSLInputStreamWrapper()
58 if( m_bFileOwner )
59 delete m_pFile;
62 //------------------------------------------------------------------------------
63 sal_Int32 SAL_CALL OSLInputStreamWrapper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
64 throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
66 if (!m_pFile)
67 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
69 if (nBytesToRead < 0)
70 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
72 ::osl::MutexGuard aGuard( m_aMutex );
74 aData.realloc(nBytesToRead);
76 sal_uInt64 nRead = 0;
77 FileBase::RC eError = m_pFile->read((void*)aData.getArray(), nBytesToRead, nRead);
78 if (eError != FileBase::E_None)
79 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
81 // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
82 if (nRead < (sal_uInt32)nBytesToRead)
83 aData.realloc( sal::static_int_cast< sal_Int32 >(nRead) );
85 return sal::static_int_cast< sal_Int32 >(nRead);
88 //------------------------------------------------------------------------------
89 sal_Int32 SAL_CALL OSLInputStreamWrapper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
91 if (!m_pFile)
92 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
94 if (nMaxBytesToRead < 0)
95 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
98 if (m_pFile->IsEof())
100 aData.realloc(0);
101 return 0;
103 else
105 return readBytes(aData, nMaxBytesToRead);
108 //------------------------------------------------------------------------------
109 void SAL_CALL OSLInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
111 ::osl::MutexGuard aGuard( m_aMutex );
112 if (!m_pFile)
113 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
115 sal_uInt64 nCurrentPos;
116 m_pFile->getPos(nCurrentPos);
118 sal_uInt64 nNewPos = nCurrentPos + nBytesToSkip;
119 FileBase::RC eError = m_pFile->setPos(osl_Pos_Absolut, nNewPos);
120 if (eError != FileBase::E_None)
122 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
125 #ifdef DBG_UTIL
126 m_pFile->getPos(nCurrentPos);
127 // volatile int dummy = 0; // to take a look at last changes ;-)
128 #endif
131 //------------------------------------------------------------------------------
132 sal_Int32 SAL_CALL OSLInputStreamWrapper::available() throw( stario::NotConnectedException, staruno::RuntimeException )
134 ::osl::MutexGuard aGuard( m_aMutex );
135 if (!m_pFile)
136 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
138 sal_uInt64 nPos;
139 FileBase::RC eError = m_pFile->getPos(nPos);
140 if (eError != FileBase::E_None)
141 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
143 sal_uInt64 nDummy = 0;
144 eError = m_pFile->setPos(Pos_End, nDummy);
145 if (eError != FileBase::E_None)
146 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
148 sal_uInt64 nAvailable;
149 eError = m_pFile->getPos(nAvailable);
150 if (eError != FileBase::E_None)
151 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
153 nAvailable = nAvailable - nPos;
154 eError = m_pFile->setPos(Pos_Absolut, nPos);
155 if (eError != FileBase::E_None)
156 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
157 return sal::static_int_cast< sal_Int32 >(
158 std::max(nAvailable, sal::static_int_cast< sal_uInt64 >(SAL_MAX_INT32)));
161 //------------------------------------------------------------------------------
162 void SAL_CALL OSLInputStreamWrapper::closeInput() throw( stario::NotConnectedException, staruno::RuntimeException )
164 if (!m_pFile)
165 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
167 m_pFile->close();
168 if (m_bFileOwner)
169 delete m_pFile;
171 m_pFile = NULL;
174 /*************************************************************************/
175 // stario::XOutputStream
176 //------------------------------------------------------------------------------
177 void SAL_CALL OSLOutputStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
179 sal_uInt64 nWritten;
180 FileBase::RC eError = rFile.write(aData.getConstArray(),aData.getLength(), nWritten);
181 if (eError != FileBase::E_None
182 || nWritten != sal::static_int_cast< sal_uInt32 >(aData.getLength()))
184 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
188 //------------------------------------------------------------------
189 void SAL_CALL OSLOutputStreamWrapper::flush() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
193 //------------------------------------------------------------------
194 void SAL_CALL OSLOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
196 rFile.close();
199 } // namespace comphelper