update dev300-m58
[ooovba.git] / unotools / source / streaming / streamhelper.cxx
blob3b55457bc1b51eaa8db66ffaba057c1abf7a0200
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: streamhelper.cxx,v $
10 * $Revision: 1.8 $
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_unotools.hxx"
33 #include <unotools/streamhelper.hxx>
34 #include <tools/debug.hxx>
36 namespace utl
39 //------------------------------------------------------------------------------
40 void SAL_CALL OInputStreamHelper::acquire() throw ()
42 InputStreamHelper_Base::acquire();
45 //------------------------------------------------------------------------------
46 void SAL_CALL OInputStreamHelper::release() throw ()
48 InputStreamHelper_Base::release();
51 //------------------------------------------------------------------------------
52 sal_Int32 SAL_CALL OInputStreamHelper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
53 throw(stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
55 if (!m_xLockBytes.Is())
56 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
58 if (nBytesToRead < 0)
59 throw stario::BufferSizeExceededException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
61 ::osl::MutexGuard aGuard( m_aMutex );
62 aData.realloc(nBytesToRead);
64 sal_Size nRead;
65 ErrCode nError = m_xLockBytes->ReadAt(m_nActPos, (void*)aData.getArray(), nBytesToRead, &nRead);
66 // FIXME nRead could be truncated on 64-bit arches
67 m_nActPos += (sal_uInt32)nRead;
69 if (nError != ERRCODE_NONE)
70 throw stario::IOException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
72 // adjust sequence if data read is lower than the desired data
73 if (nRead < (sal_uInt32)nBytesToRead)
74 aData.realloc( nRead );
76 return nRead;
79 void SAL_CALL OInputStreamHelper::seek( sal_Int64 location ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
81 ::osl::MutexGuard aGuard( m_aMutex );
82 // cast is truncating, but position would be truncated as soon as
83 // put into SvLockBytes anyway
84 m_nActPos = sal::static_int_cast<sal_uInt32>(location);
87 sal_Int64 SAL_CALL OInputStreamHelper::getPosition( ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
89 return m_nActPos;
92 sal_Int64 SAL_CALL OInputStreamHelper::getLength( ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
94 if (!m_xLockBytes.Is())
95 return 0;
97 ::osl::MutexGuard aGuard( m_aMutex );
98 SvLockBytesStat aStat;
99 m_xLockBytes->Stat( &aStat, SVSTATFLAG_DEFAULT );
100 return aStat.nSize;
103 //------------------------------------------------------------------------------
104 sal_Int32 SAL_CALL OInputStreamHelper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData,
105 sal_Int32 nMaxBytesToRead)
106 throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
108 // read all data desired
109 return readBytes(aData, nMaxBytesToRead);
112 //------------------------------------------------------------------------------
113 void SAL_CALL OInputStreamHelper::skipBytes(sal_Int32 nBytesToSkip)
114 throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
116 ::osl::MutexGuard aGuard( m_aMutex );
117 if (!m_xLockBytes.Is())
118 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
120 if (nBytesToSkip < 0)
121 throw stario::BufferSizeExceededException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
123 m_nActPos += nBytesToSkip;
126 //------------------------------------------------------------------------------
127 sal_Int32 SAL_CALL OInputStreamHelper::available()
128 throw (stario::NotConnectedException, stario::IOException, staruno::RuntimeException)
130 ::osl::MutexGuard aGuard( m_aMutex );
131 if (!m_xLockBytes.Is())
132 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
134 return m_nAvailable;
137 //------------------------------------------------------------------------------
138 void SAL_CALL OInputStreamHelper::closeInput()
139 throw (stario::NotConnectedException, stario::IOException, staruno::RuntimeException)
141 ::osl::MutexGuard aGuard( m_aMutex );
142 if (!m_xLockBytes.Is())
143 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
145 m_xLockBytes = NULL;
148 /*************************************************************************/
149 //------------------------------------------------------------------------------
150 void SAL_CALL OOutputStreamHelper::acquire() throw ()
152 OutputStreamHelper_Base::acquire();
155 //------------------------------------------------------------------------------
156 void SAL_CALL OOutputStreamHelper::release() throw ()
158 OutputStreamHelper_Base::release();
160 // stario::XOutputStream
161 //------------------------------------------------------------------------------
162 void SAL_CALL OOutputStreamHelper::writeBytes(const staruno::Sequence< sal_Int8 >& aData)
163 throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
165 ::osl::MutexGuard aGuard( m_aMutex );
166 if (!m_xLockBytes.Is())
167 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
169 sal_Size nWritten;
170 ErrCode nError = m_xLockBytes->WriteAt( m_nActPos, aData.getConstArray(), aData.getLength(), &nWritten );
171 // FIXME nWritten could be truncated on 64-bit arches
172 m_nActPos += (sal_uInt32)nWritten;
174 if (nError != ERRCODE_NONE ||
175 sal::static_int_cast<sal_Int32>(nWritten) != aData.getLength())
177 throw stario::IOException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
181 //------------------------------------------------------------------
182 void SAL_CALL OOutputStreamHelper::flush()
183 throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
185 ::osl::MutexGuard aGuard( m_aMutex );
186 if (!m_xLockBytes.Is())
187 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
189 ErrCode nError = m_xLockBytes->Flush();
190 if (nError != ERRCODE_NONE)
191 throw stario::IOException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
194 //------------------------------------------------------------------
195 void SAL_CALL OOutputStreamHelper::closeOutput( )
196 throw(stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
198 ::osl::MutexGuard aGuard( m_aMutex );
199 if (!m_xLockBytes.Is())
200 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
202 m_xLockBytes = NULL;
205 } // namespace utl