1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_unotools.hxx"
26 #include <unotools/streamhelper.hxx>
27 #include <tools/debug.hxx>
28
29 namespace utl
30 {
31
32 //------------------------------------------------------------------------------
acquire()33 void SAL_CALL OInputStreamHelper::acquire() throw ()
34 {
35 InputStreamHelper_Base::acquire();
36 }
37
38 //------------------------------------------------------------------------------
release()39 void SAL_CALL OInputStreamHelper::release() throw ()
40 {
41 InputStreamHelper_Base::release();
42 }
43
44 //------------------------------------------------------------------------------
readBytes(staruno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)45 sal_Int32 SAL_CALL OInputStreamHelper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
46 throw(stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
47 {
48 if (!m_xLockBytes.Is())
49 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
50
51 if (nBytesToRead < 0)
52 throw stario::BufferSizeExceededException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
53
54 ::osl::MutexGuard aGuard( m_aMutex );
55 aData.realloc(nBytesToRead);
56
57 sal_Size nRead;
58 ErrCode nError = m_xLockBytes->ReadAt(m_nActPos, (void*)aData.getArray(), nBytesToRead, &nRead);
59 // FIXME nRead could be truncated on 64-bit arches
60 m_nActPos += (sal_uInt32)nRead;
61
62 if (nError != ERRCODE_NONE)
63 throw stario::IOException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
64
65 // adjust sequence if data read is lower than the desired data
66 if (nRead < (sal_uInt32)nBytesToRead)
67 aData.realloc( nRead );
68
69 return nRead;
70 }
71
seek(sal_Int64 location)72 void SAL_CALL OInputStreamHelper::seek( sal_Int64 location ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
73 {
74 ::osl::MutexGuard aGuard( m_aMutex );
75 // cast is truncating, but position would be truncated as soon as
76 // put into SvLockBytes anyway
77 m_nActPos = sal::static_int_cast<sal_uInt32>(location);
78 }
79
getPosition()80 sal_Int64 SAL_CALL OInputStreamHelper::getPosition( ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
81 {
82 return m_nActPos;
83 }
84
getLength()85 sal_Int64 SAL_CALL OInputStreamHelper::getLength( ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
86 {
87 if (!m_xLockBytes.Is())
88 return 0;
89
90 ::osl::MutexGuard aGuard( m_aMutex );
91 SvLockBytesStat aStat;
92 m_xLockBytes->Stat( &aStat, SVSTATFLAG_DEFAULT );
93 return aStat.nSize;
94 }
95
96 //------------------------------------------------------------------------------
readSomeBytes(staruno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)97 sal_Int32 SAL_CALL OInputStreamHelper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData,
98 sal_Int32 nMaxBytesToRead)
99 throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
100 {
101 // read all data desired
102 return readBytes(aData, nMaxBytesToRead);
103 }
104
105 //------------------------------------------------------------------------------
skipBytes(sal_Int32 nBytesToSkip)106 void SAL_CALL OInputStreamHelper::skipBytes(sal_Int32 nBytesToSkip)
107 throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
108 {
109 ::osl::MutexGuard aGuard( m_aMutex );
110 if (!m_xLockBytes.Is())
111 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
112
113 if (nBytesToSkip < 0)
114 throw stario::BufferSizeExceededException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
115
116 m_nActPos += nBytesToSkip;
117 }
118
119 //------------------------------------------------------------------------------
available()120 sal_Int32 SAL_CALL OInputStreamHelper::available()
121 throw (stario::NotConnectedException, stario::IOException, staruno::RuntimeException)
122 {
123 ::osl::MutexGuard aGuard( m_aMutex );
124 if (!m_xLockBytes.Is())
125 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
126
127 return m_nAvailable;
128 }
129
130 //------------------------------------------------------------------------------
closeInput()131 void SAL_CALL OInputStreamHelper::closeInput()
132 throw (stario::NotConnectedException, stario::IOException, staruno::RuntimeException)
133 {
134 ::osl::MutexGuard aGuard( m_aMutex );
135 if (!m_xLockBytes.Is())
136 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
137
138 m_xLockBytes = NULL;
139 }
140
141 /*************************************************************************/
142 //------------------------------------------------------------------------------
acquire()143 void SAL_CALL OOutputStreamHelper::acquire() throw ()
144 {
145 OutputStreamHelper_Base::acquire();
146 }
147
148 //------------------------------------------------------------------------------
release()149 void SAL_CALL OOutputStreamHelper::release() throw ()
150 {
151 OutputStreamHelper_Base::release();
152 }
153 // stario::XOutputStream
154 //------------------------------------------------------------------------------
writeBytes(const staruno::Sequence<sal_Int8> & aData)155 void SAL_CALL OOutputStreamHelper::writeBytes(const staruno::Sequence< sal_Int8 >& aData)
156 throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
157 {
158 ::osl::MutexGuard aGuard( m_aMutex );
159 if (!m_xLockBytes.Is())
160 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
161
162 sal_Size nWritten;
163 ErrCode nError = m_xLockBytes->WriteAt( m_nActPos, aData.getConstArray(), aData.getLength(), &nWritten );
164 // FIXME nWritten could be truncated on 64-bit arches
165 m_nActPos += (sal_uInt32)nWritten;
166
167 if (nError != ERRCODE_NONE ||
168 sal::static_int_cast<sal_Int32>(nWritten) != aData.getLength())
169 {
170 throw stario::IOException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
171 }
172 }
173
174 //------------------------------------------------------------------
flush()175 void SAL_CALL OOutputStreamHelper::flush()
176 throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
177 {
178 ::osl::MutexGuard aGuard( m_aMutex );
179 if (!m_xLockBytes.Is())
180 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
181
182 ErrCode nError = m_xLockBytes->Flush();
183 if (nError != ERRCODE_NONE)
184 throw stario::IOException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
185 }
186
187 //------------------------------------------------------------------
closeOutput()188 void SAL_CALL OOutputStreamHelper::closeOutput( )
189 throw(stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
190 {
191 ::osl::MutexGuard aGuard( m_aMutex );
192 if (!m_xLockBytes.Is())
193 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
194
195 m_xLockBytes = NULL;
196 }
197
198 } // namespace utl
199
200
201