1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_unotools.hxx"
30 #include <unotools/streamhelper.hxx>
31 #include <tools/debug.hxx>
32 
33 namespace utl
34 {
35 
36 //------------------------------------------------------------------------------
37 void SAL_CALL OInputStreamHelper::acquire() throw ()
38 {
39 	InputStreamHelper_Base::acquire();
40 }
41 
42 //------------------------------------------------------------------------------
43 void SAL_CALL OInputStreamHelper::release() throw ()
44 {
45 	InputStreamHelper_Base::release();
46 }
47 
48 //------------------------------------------------------------------------------
49 sal_Int32 SAL_CALL OInputStreamHelper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
50 	throw(stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
51 {
52 	if (!m_xLockBytes.Is())
53 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
54 
55 	if (nBytesToRead < 0)
56 		throw stario::BufferSizeExceededException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
57 
58 	::osl::MutexGuard aGuard( m_aMutex );
59 	aData.realloc(nBytesToRead);
60 
61 	sal_Size nRead;
62 	ErrCode nError = m_xLockBytes->ReadAt(m_nActPos, (void*)aData.getArray(), nBytesToRead, &nRead);
63 	// FIXME  nRead could be truncated on 64-bit arches
64 	m_nActPos += (sal_uInt32)nRead;
65 
66 	if (nError != ERRCODE_NONE)
67 		throw stario::IOException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
68 
69 	// adjust sequence if data read is lower than the desired data
70 	if (nRead < (sal_uInt32)nBytesToRead)
71 		aData.realloc( nRead );
72 
73 	return nRead;
74 }
75 
76 void SAL_CALL OInputStreamHelper::seek( sal_Int64 location ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
77 {
78 	::osl::MutexGuard aGuard( m_aMutex );
79     // cast is truncating, but position would be truncated as soon as
80     // put into SvLockBytes anyway
81     m_nActPos = sal::static_int_cast<sal_uInt32>(location);
82 }
83 
84 sal_Int64 SAL_CALL OInputStreamHelper::getPosition(  ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
85 {
86     return m_nActPos;
87 }
88 
89 sal_Int64 SAL_CALL OInputStreamHelper::getLength(  ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
90 {
91 	if (!m_xLockBytes.Is())
92         return 0;
93 
94 	::osl::MutexGuard aGuard( m_aMutex );
95     SvLockBytesStat aStat;
96     m_xLockBytes->Stat( &aStat, SVSTATFLAG_DEFAULT );
97 	return aStat.nSize;
98 }
99 
100 //------------------------------------------------------------------------------
101 sal_Int32 SAL_CALL OInputStreamHelper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData,
102 													 sal_Int32 nMaxBytesToRead)
103 	throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
104 {
105 	// read all data desired
106 	return readBytes(aData, nMaxBytesToRead);
107 }
108 
109 //------------------------------------------------------------------------------
110 void SAL_CALL OInputStreamHelper::skipBytes(sal_Int32 nBytesToSkip)
111 	throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
112 {
113 	::osl::MutexGuard aGuard( m_aMutex );
114 	if (!m_xLockBytes.Is())
115 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
116 
117 	if (nBytesToSkip < 0)
118 		throw stario::BufferSizeExceededException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
119 
120 	m_nActPos += nBytesToSkip;
121 }
122 
123 //------------------------------------------------------------------------------
124 sal_Int32 SAL_CALL OInputStreamHelper::available()
125 	throw (stario::NotConnectedException, stario::IOException, staruno::RuntimeException)
126 {
127 	::osl::MutexGuard aGuard( m_aMutex );
128 	if (!m_xLockBytes.Is())
129 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
130 
131 	return m_nAvailable;
132 }
133 
134 //------------------------------------------------------------------------------
135 void SAL_CALL OInputStreamHelper::closeInput()
136 	throw (stario::NotConnectedException, stario::IOException, staruno::RuntimeException)
137 {
138 	::osl::MutexGuard aGuard( m_aMutex );
139 	if (!m_xLockBytes.Is())
140 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
141 
142 	m_xLockBytes = NULL;
143 }
144 
145 /*************************************************************************/
146 //------------------------------------------------------------------------------
147 void SAL_CALL OOutputStreamHelper::acquire() throw ()
148 {
149 	OutputStreamHelper_Base::acquire();
150 }
151 
152 //------------------------------------------------------------------------------
153 void SAL_CALL OOutputStreamHelper::release() throw ()
154 {
155 	OutputStreamHelper_Base::release();
156 }
157 // stario::XOutputStream
158 //------------------------------------------------------------------------------
159 void SAL_CALL OOutputStreamHelper::writeBytes(const staruno::Sequence< sal_Int8 >& aData)
160 	throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
161 {
162 	::osl::MutexGuard aGuard( m_aMutex );
163 	if (!m_xLockBytes.Is())
164 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
165 
166 	sal_Size nWritten;
167 	ErrCode nError = m_xLockBytes->WriteAt( m_nActPos, aData.getConstArray(), aData.getLength(), &nWritten );
168 	// FIXME  nWritten could be truncated on 64-bit arches
169 	m_nActPos += (sal_uInt32)nWritten;
170 
171 	if (nError != ERRCODE_NONE ||
172 		sal::static_int_cast<sal_Int32>(nWritten) != aData.getLength())
173 	{
174 		throw stario::IOException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
175 	}
176 }
177 
178 //------------------------------------------------------------------
179 void SAL_CALL OOutputStreamHelper::flush()
180 	throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
181 {
182 	::osl::MutexGuard aGuard( m_aMutex );
183 	if (!m_xLockBytes.Is())
184 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
185 
186 	ErrCode nError = m_xLockBytes->Flush();
187 	if (nError != ERRCODE_NONE)
188 		throw stario::IOException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
189 }
190 
191 //------------------------------------------------------------------
192 void SAL_CALL OOutputStreamHelper::closeOutput(  )
193 	throw(stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
194 {
195 	::osl::MutexGuard aGuard( m_aMutex );
196 	if (!m_xLockBytes.Is())
197 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
198 
199 	m_xLockBytes = NULL;
200 }
201 
202 } // namespace utl
203 
204 
205