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