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/streamsection.hxx>
27cdf0e10cSrcweir #include <osl/diagnose.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir namespace comphelper
30cdf0e10cSrcweir {
31cdf0e10cSrcweir 
32cdf0e10cSrcweir //-------------------------------------------------------------------------
OStreamSection(const staruno::Reference<stario::XDataInputStream> & _rxInput)33cdf0e10cSrcweir OStreamSection::OStreamSection(const staruno::Reference< stario::XDataInputStream >& _rxInput)
34cdf0e10cSrcweir 	:m_xMarkStream(_rxInput, ::com::sun::star::uno::UNO_QUERY)
35cdf0e10cSrcweir 	,m_xInStream(_rxInput)
36cdf0e10cSrcweir 	,m_nBlockStart(-1)
37cdf0e10cSrcweir 	,m_nBlockLen(-1)
38cdf0e10cSrcweir {
39cdf0e10cSrcweir 	OSL_ENSURE(m_xInStream.is() && m_xMarkStream.is(), "OStreamSection::OStreamSection : invalid argument !");
40cdf0e10cSrcweir 	if (m_xInStream.is() && m_xMarkStream.is())
41cdf0e10cSrcweir 	{
42cdf0e10cSrcweir 		m_nBlockLen = _rxInput->readLong();
43cdf0e10cSrcweir 		m_nBlockStart = m_xMarkStream->createMark();
44cdf0e10cSrcweir 	}
45cdf0e10cSrcweir }
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //-------------------------------------------------------------------------
OStreamSection(const staruno::Reference<stario::XDataOutputStream> & _rxOutput,sal_Int32 _nPresumedLength)48cdf0e10cSrcweir OStreamSection::OStreamSection(const staruno::Reference< stario::XDataOutputStream >& _rxOutput, sal_Int32 _nPresumedLength)
49cdf0e10cSrcweir 	:m_xMarkStream(_rxOutput, ::com::sun::star::uno::UNO_QUERY)
50cdf0e10cSrcweir 	,m_xOutStream(_rxOutput)
51cdf0e10cSrcweir 	,m_nBlockStart(-1)
52cdf0e10cSrcweir 	,m_nBlockLen(-1)
53cdf0e10cSrcweir {
54cdf0e10cSrcweir 	OSL_ENSURE(m_xOutStream.is() && m_xMarkStream.is(), "OStreamSection::OStreamSection : invalid argument !");
55cdf0e10cSrcweir 	if (m_xOutStream.is() && m_xMarkStream.is())
56cdf0e10cSrcweir 	{
57cdf0e10cSrcweir 		m_nBlockStart = m_xMarkStream->createMark();
58cdf0e10cSrcweir 		// a placeholder where we will write the overall length (within the destructor)
59cdf0e10cSrcweir 		if (_nPresumedLength > 0)
60cdf0e10cSrcweir 			m_nBlockLen = _nPresumedLength + sizeof(m_nBlockLen);
61cdf0e10cSrcweir 			// as the caller did not consider - of course - the placeholder we are going to write
62cdf0e10cSrcweir 		else
63cdf0e10cSrcweir 			m_nBlockLen = 0;
64cdf0e10cSrcweir 		m_xOutStream->writeLong(m_nBlockLen);
65cdf0e10cSrcweir 	}
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir //-------------------------------------------------------------------------
~OStreamSection()69cdf0e10cSrcweir OStreamSection::~OStreamSection()
70cdf0e10cSrcweir {
71cdf0e10cSrcweir 	try
72cdf0e10cSrcweir 	{	// don't allow any exceptions to leave this block, this may be called during the stack unwinding of an exception
73cdf0e10cSrcweir 		// handling routing
74cdf0e10cSrcweir 		if (m_xInStream.is() &&  m_xMarkStream.is())
75cdf0e10cSrcweir 		{	// we're working on an input stream
76cdf0e10cSrcweir 			m_xMarkStream->jumpToMark(m_nBlockStart);
77cdf0e10cSrcweir 			m_xInStream->skipBytes(m_nBlockLen);
78cdf0e10cSrcweir 			m_xMarkStream->deleteMark(m_nBlockStart);
79cdf0e10cSrcweir 		}
80cdf0e10cSrcweir 		else if (m_xOutStream.is() && m_xMarkStream.is())
81cdf0e10cSrcweir 		{
82cdf0e10cSrcweir 			sal_Int32 nRealBlockLength = m_xMarkStream->offsetToMark(m_nBlockStart) - sizeof(m_nBlockLen);
83cdf0e10cSrcweir 			if (m_nBlockLen && (m_nBlockLen == nRealBlockLength))
84cdf0e10cSrcweir 				// nothing to do : the estimation the caller gave us (in the ctor) was correct
85cdf0e10cSrcweir 				m_xMarkStream->deleteMark(m_nBlockStart);
86cdf0e10cSrcweir 			else
87cdf0e10cSrcweir 			{	// the estimation was wrong (or we didn't get one)
88cdf0e10cSrcweir 				m_nBlockLen = nRealBlockLength;
89cdf0e10cSrcweir 				m_xMarkStream->jumpToMark(m_nBlockStart);
90cdf0e10cSrcweir 				m_xOutStream->writeLong(m_nBlockLen);
91cdf0e10cSrcweir 				m_xMarkStream->jumpToFurthest();
92cdf0e10cSrcweir 				m_xMarkStream->deleteMark(m_nBlockStart);
93cdf0e10cSrcweir 			}
94cdf0e10cSrcweir 		}
95cdf0e10cSrcweir 	}
96cdf0e10cSrcweir 	catch(const staruno::Exception&)
97cdf0e10cSrcweir 	{
98cdf0e10cSrcweir 	}
99cdf0e10cSrcweir }
100cdf0e10cSrcweir // -----------------------------------------------------------------------------
available()101cdf0e10cSrcweir sal_Int32 OStreamSection::available()
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 	sal_Int32 nBytes = 0;
104cdf0e10cSrcweir 	try
105cdf0e10cSrcweir 	{	// don't allow any exceptions to leave this block, this may be called during the stack unwinding of an exception
106cdf0e10cSrcweir 		if (m_xInStream.is() &&  m_xMarkStream.is())
107cdf0e10cSrcweir 			nBytes = m_xMarkStream->offsetToMark(m_nBlockStart) - sizeof(m_nBlockLen);
108cdf0e10cSrcweir 	}
109cdf0e10cSrcweir 	catch(const staruno::Exception&)
110cdf0e10cSrcweir 	{
111cdf0e10cSrcweir 	}
112cdf0e10cSrcweir 	return nBytes;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir // -----------------------------------------------------------------------------
115cdf0e10cSrcweir 
116cdf0e10cSrcweir }	// namespace comphelper
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 
119