merge the formfield patch from ooo-build
[ooovba.git] / configmgr / source / misc / oslstream.cxx
blobed4c7056c12ab4eb747f22e6631e5921b2b0f64f
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: oslstream.cxx,v $
10 * $Revision: 1.10 $
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_configmgr.hxx"
33 #include "oslstream.hxx"
35 #include "filehelper.hxx"
37 namespace configmgr
39 static void raiseIOException(osl::File::RC error, staruno::Reference<staruno::XInterface> const & context)
41 const rtl::OUString message = FileHelper::createOSLErrorString(error);
42 switch (error)
44 case osl::File::E_NOMEM:
45 throw stario::BufferSizeExceededException(message, context);
47 case osl::File::E_BADF:
48 throw stario::NotConnectedException(message, context);
50 default:
51 throw stario::IOException(message, context);
54 //------------------------------------------------------------------
55 OSLInputStreamWrapper::OSLInputStreamWrapper( osl::File& _rFile )
56 :m_pFile(&_rFile)
57 ,m_bFileOwner(sal_False)
61 //------------------------------------------------------------------
62 OSLInputStreamWrapper::OSLInputStreamWrapper( osl::File* pStream, sal_Bool bOwner )
63 :m_pFile( pStream )
64 ,m_bFileOwner( bOwner )
68 //------------------------------------------------------------------
69 OSLInputStreamWrapper::~OSLInputStreamWrapper()
71 if( m_bFileOwner )
72 delete m_pFile;
75 //------------------------------------------------------------------------------
76 sal_Int32 SAL_CALL OSLInputStreamWrapper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
77 throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
79 if (!m_pFile)
80 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
82 if (nBytesToRead < 0)
83 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
85 osl::MutexGuard aGuard( m_aMutex );
87 aData.realloc(nBytesToRead);
89 sal_uInt64 nRead = 0;
90 osl::File::RC eError = m_pFile->read(aData.getArray(), nBytesToRead, nRead);
91 if (eError != osl::File::E_None)
92 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
94 // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
95 if (nRead < (sal_uInt64)nBytesToRead)
96 aData.realloc( sal::static_int_cast<sal_Int32>( nRead ));
98 return sal::static_int_cast<sal_Int32>( nRead );
101 //------------------------------------------------------------------------------
102 sal_Int32 SAL_CALL OSLInputStreamWrapper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
104 if (!m_pFile)
105 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
107 if (nMaxBytesToRead < 0)
108 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
111 if (m_pFile->IsEof())
113 aData.realloc(0);
114 return 0;
116 else
118 return readBytes(aData, nMaxBytesToRead);
121 //------------------------------------------------------------------------------
122 void SAL_CALL OSLInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
124 osl::MutexGuard aGuard( m_aMutex );
125 if (!m_pFile)
126 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
128 sal_uInt64 nCurrentPos;
129 m_pFile->getPos(nCurrentPos);
131 sal_uInt64 nNewPos = nCurrentPos + nBytesToSkip;
132 osl::File::RC eError = m_pFile->setPos(osl_Pos_Absolut, nNewPos);
133 if (eError != osl::File::E_None)
135 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
139 //------------------------------------------------------------------------------
140 sal_Int32 SAL_CALL OSLInputStreamWrapper::available() throw( stario::NotConnectedException, staruno::RuntimeException )
142 ::osl::MutexGuard aGuard( m_aMutex );
143 if (!m_pFile)
144 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
146 sal_uInt64 nPos;
147 osl::File::RC eError = m_pFile->getPos(nPos);
148 if (eError != osl::File::E_None)
149 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
151 sal_uInt64 nDummy = 0;
152 eError = m_pFile->setPos(Pos_End, nDummy);
153 if (eError != osl::File::E_None)
154 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
156 sal_uInt64 nAvailable;
157 eError = m_pFile->getPos(nAvailable);
158 if (eError != osl::File::E_None)
159 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
161 nAvailable = nAvailable - nPos;
162 eError = m_pFile->setPos(Pos_Absolut, nPos);
163 if (eError != osl::File::E_None)
164 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
165 return sal::static_int_cast<sal_Int32>( nAvailable );
168 //------------------------------------------------------------------------------
169 void SAL_CALL OSLInputStreamWrapper::closeInput() throw( stario::NotConnectedException, staruno::RuntimeException )
171 if (!m_pFile)
172 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
174 m_pFile->close();
175 if (m_bFileOwner)
176 delete m_pFile;
178 m_pFile = NULL;
181 /*************************************************************************/
182 // stario::XOutputStream
183 //------------------------------------------------------------------------------
184 void SAL_CALL OSLOutputStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
186 sal_uInt32 const nLength = sal_uInt32(aData.getLength());
187 if (nLength != 0)
189 sal_uInt64 nWritten;
190 osl::File::RC eError = rFile.write(aData.getConstArray(),nLength, nWritten);
191 if (eError != osl::File::E_None || nWritten != nLength)
193 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
199 //------------------------------------------------------------------
200 void SAL_CALL OSLOutputStreamWrapper::flush() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
204 //------------------------------------------------------------------
205 void SAL_CALL OSLOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
207 rFile.close();
210 //------------------------------------------------------------------
211 //------------------------------------------------------------------
212 BufferedFileOutputStream::BufferedFileOutputStream( rtl::OUString const & aFileURL, bool bCreate, sal_uInt32 nBufferSizeHint)
213 : m_aFile( aFileURL, nBufferSizeHint )
215 sal_Int32 flags = bCreate ? OpenFlag_Write|OpenFlag_Create : OpenFlag_Write;
217 osl::File::RC rc = m_aFile.open(flags);
218 if (rc != osl::File::E_None)
219 raiseIOException(rc,NULL);
222 //------------------------------------------------------------------
223 BufferedFileOutputStream::~BufferedFileOutputStream()
227 //------------------------------------------------------------------------------
228 void SAL_CALL BufferedFileOutputStream::writeBytes(const staruno::Sequence< sal_Int8 >& aData)
229 throw( stario::NotConnectedException, stario::BufferSizeExceededException,
230 stario::IOException, staruno::RuntimeException )
232 const sal_uInt64 size = sal_uInt64(aData.getLength());
233 sal_uInt64 written = 0;
235 osl::File::RC rc = m_aFile.write(aData.getConstArray(), size, written);
236 if (rc != osl::File::E_None)
237 raiseIOException(rc,*this);
239 // we don't support special files where multiple write passes are needed
240 if (written < size)
241 raiseIOException(osl::File::E_IO,*this);
244 void SAL_CALL BufferedFileOutputStream::flush()
245 throw( stario::NotConnectedException, stario::BufferSizeExceededException,
246 stario::IOException, staruno::RuntimeException )
248 osl::File::RC rc = m_aFile.sync();
249 if (rc != osl::File::E_None)
250 raiseIOException(rc,*this);
253 void SAL_CALL BufferedFileOutputStream::closeOutput()
254 throw( stario::NotConnectedException, stario::BufferSizeExceededException,
255 stario::IOException, staruno::RuntimeException )
257 osl::File::RC rc = m_aFile.close();
258 if (rc != osl::File::E_None)
259 raiseIOException(rc,*this);
262 } // namespace configmgr