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