1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*dde7d3faSAndrew Rist  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*dde7d3faSAndrew Rist  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19*dde7d3faSAndrew Rist  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir #include <comphelper/oslfile2streamwrap.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <algorithm>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir namespace comphelper
31cdf0e10cSrcweir {
32cdf0e10cSrcweir 	using namespace osl;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir //------------------------------------------------------------------
OSLInputStreamWrapper(File & _rFile)35cdf0e10cSrcweir OSLInputStreamWrapper::OSLInputStreamWrapper( File& _rFile )
36cdf0e10cSrcweir 				 :m_pFile(&_rFile)
37cdf0e10cSrcweir 				 ,m_bFileOwner(sal_False)
38cdf0e10cSrcweir {
39cdf0e10cSrcweir }
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //------------------------------------------------------------------
OSLInputStreamWrapper(File * pStream,sal_Bool bOwner)42cdf0e10cSrcweir OSLInputStreamWrapper::OSLInputStreamWrapper( File* pStream, sal_Bool bOwner )
43cdf0e10cSrcweir 				 :m_pFile( pStream )
44cdf0e10cSrcweir 				 ,m_bFileOwner( bOwner )
45cdf0e10cSrcweir {
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //------------------------------------------------------------------
~OSLInputStreamWrapper()49cdf0e10cSrcweir OSLInputStreamWrapper::~OSLInputStreamWrapper()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 	if( m_bFileOwner )
52cdf0e10cSrcweir 		delete m_pFile;
53cdf0e10cSrcweir }
54cdf0e10cSrcweir 
55cdf0e10cSrcweir //------------------------------------------------------------------------------
readBytes(staruno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)56cdf0e10cSrcweir sal_Int32 SAL_CALL OSLInputStreamWrapper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
57cdf0e10cSrcweir 				throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
58cdf0e10cSrcweir {
59cdf0e10cSrcweir 	if (!m_pFile)
60cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	if (nBytesToRead < 0)
63cdf0e10cSrcweir 		throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	aData.realloc(nBytesToRead);
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 	sal_uInt64 nRead = 0;
70cdf0e10cSrcweir 	FileBase::RC eError = m_pFile->read((void*)aData.getArray(), nBytesToRead, nRead);
71cdf0e10cSrcweir 	if (eError != FileBase::E_None)
72cdf0e10cSrcweir 		throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	// Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
75cdf0e10cSrcweir 	if (nRead < (sal_uInt32)nBytesToRead)
76cdf0e10cSrcweir 		aData.realloc( sal::static_int_cast< sal_Int32 >(nRead) );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	return sal::static_int_cast< sal_Int32 >(nRead);
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir //------------------------------------------------------------------------------
readSomeBytes(staruno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)82cdf0e10cSrcweir sal_Int32 SAL_CALL OSLInputStreamWrapper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
83cdf0e10cSrcweir {
84cdf0e10cSrcweir 	if (!m_pFile)
85cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 	if (nMaxBytesToRead < 0)
88cdf0e10cSrcweir 		throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	/*
91cdf0e10cSrcweir 	  if (m_pFile->IsEof())
92cdf0e10cSrcweir 	  {
93cdf0e10cSrcweir 	  aData.realloc(0);
94cdf0e10cSrcweir 	  return 0;
95cdf0e10cSrcweir 	  }
96cdf0e10cSrcweir 	  else
97cdf0e10cSrcweir 	*/
98cdf0e10cSrcweir 	return readBytes(aData, nMaxBytesToRead);
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir //------------------------------------------------------------------------------
skipBytes(sal_Int32 nBytesToSkip)102cdf0e10cSrcweir void SAL_CALL OSLInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
105cdf0e10cSrcweir 	if (!m_pFile)
106cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	sal_uInt64 nCurrentPos;
109cdf0e10cSrcweir 	m_pFile->getPos(nCurrentPos);
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	sal_uInt64 nNewPos = nCurrentPos + nBytesToSkip;
112cdf0e10cSrcweir 	FileBase::RC eError = m_pFile->setPos(osl_Pos_Absolut, nNewPos);
113cdf0e10cSrcweir 	if (eError != FileBase::E_None)
114cdf0e10cSrcweir 	{
115cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
116cdf0e10cSrcweir 	}
117cdf0e10cSrcweir 
118cdf0e10cSrcweir #ifdef DBG_UTIL
119cdf0e10cSrcweir 	m_pFile->getPos(nCurrentPos);
120cdf0e10cSrcweir //  volatile int dummy = 0;						 // to take a look at last changes ;-)
121cdf0e10cSrcweir #endif
122cdf0e10cSrcweir }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir //------------------------------------------------------------------------------
available()125cdf0e10cSrcweir sal_Int32 SAL_CALL OSLInputStreamWrapper::available() throw( stario::NotConnectedException, staruno::RuntimeException )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
128cdf0e10cSrcweir 	if (!m_pFile)
129cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	sal_uInt64 nPos;
132cdf0e10cSrcweir 	FileBase::RC eError = m_pFile->getPos(nPos);
133cdf0e10cSrcweir 	if (eError != FileBase::E_None)
134cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	sal_uInt64 nDummy = 0;
137cdf0e10cSrcweir 	eError = m_pFile->setPos(Pos_End, nDummy);
138cdf0e10cSrcweir 	if (eError != FileBase::E_None)
139cdf0e10cSrcweir 	   throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	sal_uInt64 nAvailable;
142cdf0e10cSrcweir 	eError = m_pFile->getPos(nAvailable);
143cdf0e10cSrcweir 	if (eError != FileBase::E_None)
144cdf0e10cSrcweir 	   throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 	nAvailable = nAvailable - nPos;
147cdf0e10cSrcweir 	eError = m_pFile->setPos(Pos_Absolut, nPos);
148cdf0e10cSrcweir 	if (eError != FileBase::E_None)
149cdf0e10cSrcweir 	   throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
150cdf0e10cSrcweir 	return sal::static_int_cast< sal_Int32 >(
151cdf0e10cSrcweir         std::max(nAvailable, sal::static_int_cast< sal_uInt64 >(SAL_MAX_INT32)));
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir //------------------------------------------------------------------------------
closeInput()155cdf0e10cSrcweir void SAL_CALL OSLInputStreamWrapper::closeInput() throw( stario::NotConnectedException, staruno::RuntimeException )
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	if (!m_pFile)
158cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 	m_pFile->close();
161cdf0e10cSrcweir 	if (m_bFileOwner)
162cdf0e10cSrcweir 		delete m_pFile;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 	m_pFile = NULL;
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir /*************************************************************************/
168cdf0e10cSrcweir // stario::XOutputStream
169cdf0e10cSrcweir //------------------------------------------------------------------------------
writeBytes(const staruno::Sequence<sal_Int8> & aData)170cdf0e10cSrcweir void SAL_CALL OSLOutputStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
171cdf0e10cSrcweir {
172cdf0e10cSrcweir 	sal_uInt64 nWritten;
173cdf0e10cSrcweir 	FileBase::RC eError = rFile.write(aData.getConstArray(),aData.getLength(), nWritten);
174cdf0e10cSrcweir 	if (eError != FileBase::E_None
175cdf0e10cSrcweir         || nWritten != sal::static_int_cast< sal_uInt32 >(aData.getLength()))
176cdf0e10cSrcweir 	{
177cdf0e10cSrcweir 		throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
178cdf0e10cSrcweir 	}
179cdf0e10cSrcweir }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir //------------------------------------------------------------------
flush()182cdf0e10cSrcweir void SAL_CALL OSLOutputStreamWrapper::flush() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
183cdf0e10cSrcweir {
184cdf0e10cSrcweir }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir //------------------------------------------------------------------
closeOutput()187cdf0e10cSrcweir void SAL_CALL OSLOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
188cdf0e10cSrcweir {
189cdf0e10cSrcweir 	rFile.close();
190cdf0e10cSrcweir }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir } // namespace comphelper
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 
195