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_comphelper.hxx"
26 #include <comphelper/oslfile2streamwrap.hxx>
27
28 #include <algorithm>
29
30 namespace comphelper
31 {
32 using namespace osl;
33
34 //------------------------------------------------------------------
OSLInputStreamWrapper(File & _rFile)35 OSLInputStreamWrapper::OSLInputStreamWrapper( File& _rFile )
36 :m_pFile(&_rFile)
37 ,m_bFileOwner(sal_False)
38 {
39 }
40
41 //------------------------------------------------------------------
OSLInputStreamWrapper(File * pStream,sal_Bool bOwner)42 OSLInputStreamWrapper::OSLInputStreamWrapper( File* pStream, sal_Bool bOwner )
43 :m_pFile( pStream )
44 ,m_bFileOwner( bOwner )
45 {
46 }
47
48 //------------------------------------------------------------------
~OSLInputStreamWrapper()49 OSLInputStreamWrapper::~OSLInputStreamWrapper()
50 {
51 if( m_bFileOwner )
52 delete m_pFile;
53 }
54
55 //------------------------------------------------------------------------------
readBytes(staruno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)56 sal_Int32 SAL_CALL OSLInputStreamWrapper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
57 {
58 if (!m_pFile)
59 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
60
61 if (nBytesToRead < 0)
62 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
63
64 ::osl::MutexGuard aGuard( m_aMutex );
65
66 aData.realloc(nBytesToRead);
67
68 sal_uInt64 nRead = 0;
69 FileBase::RC eError = m_pFile->read((void*)aData.getArray(), nBytesToRead, nRead);
70 if (eError != FileBase::E_None)
71 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
72
73 // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
74 if (nRead < (sal_uInt32)nBytesToRead)
75 aData.realloc( sal::static_int_cast< sal_Int32 >(nRead) );
76
77 return sal::static_int_cast< sal_Int32 >(nRead);
78 }
79
80 //------------------------------------------------------------------------------
readSomeBytes(staruno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)81 sal_Int32 SAL_CALL OSLInputStreamWrapper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead)
82 {
83 if (!m_pFile)
84 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
85
86 if (nMaxBytesToRead < 0)
87 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
88
89 /*
90 if (m_pFile->IsEof())
91 {
92 aData.realloc(0);
93 return 0;
94 }
95 else
96 */
97 return readBytes(aData, nMaxBytesToRead);
98 }
99
100 //------------------------------------------------------------------------------
skipBytes(sal_Int32 nBytesToSkip)101 void SAL_CALL OSLInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip)
102 {
103 ::osl::MutexGuard aGuard( m_aMutex );
104 if (!m_pFile)
105 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
106
107 sal_uInt64 nCurrentPos;
108 m_pFile->getPos(nCurrentPos);
109
110 sal_uInt64 nNewPos = nCurrentPos + nBytesToSkip;
111 FileBase::RC eError = m_pFile->setPos(osl_Pos_Absolut, nNewPos);
112 if (eError != FileBase::E_None)
113 {
114 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
115 }
116
117 #ifdef DBG_UTIL
118 m_pFile->getPos(nCurrentPos);
119 // volatile int dummy = 0; // to take a look at last changes ;-)
120 #endif
121 }
122
123 //------------------------------------------------------------------------------
available()124 sal_Int32 SAL_CALL OSLInputStreamWrapper::available()
125 {
126 ::osl::MutexGuard aGuard( m_aMutex );
127 if (!m_pFile)
128 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
129
130 sal_uInt64 nPos;
131 FileBase::RC eError = m_pFile->getPos(nPos);
132 if (eError != FileBase::E_None)
133 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
134
135 sal_uInt64 nDummy = 0;
136 eError = m_pFile->setPos(Pos_End, nDummy);
137 if (eError != FileBase::E_None)
138 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
139
140 sal_uInt64 nAvailable;
141 eError = m_pFile->getPos(nAvailable);
142 if (eError != FileBase::E_None)
143 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
144
145 nAvailable = nAvailable - nPos;
146 eError = m_pFile->setPos(Pos_Absolut, nPos);
147 if (eError != FileBase::E_None)
148 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
149 return sal::static_int_cast< sal_Int32 >(
150 std::max(nAvailable, sal::static_int_cast< sal_uInt64 >(SAL_MAX_INT32)));
151 }
152
153 //------------------------------------------------------------------------------
closeInput()154 void SAL_CALL OSLInputStreamWrapper::closeInput()
155 {
156 if (!m_pFile)
157 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
158
159 m_pFile->close();
160 if (m_bFileOwner)
161 delete m_pFile;
162
163 m_pFile = NULL;
164 }
165
166 /*************************************************************************/
167 // stario::XOutputStream
168 //------------------------------------------------------------------------------
writeBytes(const staruno::Sequence<sal_Int8> & aData)169 void SAL_CALL OSLOutputStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData)
170 {
171 sal_uInt64 nWritten;
172 FileBase::RC eError = rFile.write(aData.getConstArray(),aData.getLength(), nWritten);
173 if (eError != FileBase::E_None
174 || nWritten != sal::static_int_cast< sal_uInt32 >(aData.getLength()))
175 {
176 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
177 }
178 }
179
180 //------------------------------------------------------------------
flush()181 void SAL_CALL OSLOutputStreamWrapper::flush()
182 {
183 }
184
185 //------------------------------------------------------------------
closeOutput()186 void SAL_CALL OSLOutputStreamWrapper::closeOutput()
187 {
188 rFile.close();
189 }
190
191 } // namespace comphelper
192