Update ooo320-m1
[ooovba.git] / comphelper / source / streaming / streamsection.cxx
blobafcc4aa00baaf362ee83ccf9fc459172a8e59a2b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: streamsection.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <comphelper/streamsection.hxx>
34 #include <osl/diagnose.h>
36 namespace comphelper
39 //-------------------------------------------------------------------------
40 OStreamSection::OStreamSection(const staruno::Reference< stario::XDataInputStream >& _rxInput)
41 :m_xMarkStream(_rxInput, ::com::sun::star::uno::UNO_QUERY)
42 ,m_xInStream(_rxInput)
43 ,m_nBlockStart(-1)
44 ,m_nBlockLen(-1)
46 OSL_ENSURE(m_xInStream.is() && m_xMarkStream.is(), "OStreamSection::OStreamSection : invalid argument !");
47 if (m_xInStream.is() && m_xMarkStream.is())
49 m_nBlockLen = _rxInput->readLong();
50 m_nBlockStart = m_xMarkStream->createMark();
54 //-------------------------------------------------------------------------
55 OStreamSection::OStreamSection(const staruno::Reference< stario::XDataOutputStream >& _rxOutput, sal_Int32 _nPresumedLength)
56 :m_xMarkStream(_rxOutput, ::com::sun::star::uno::UNO_QUERY)
57 ,m_xOutStream(_rxOutput)
58 ,m_nBlockStart(-1)
59 ,m_nBlockLen(-1)
61 OSL_ENSURE(m_xOutStream.is() && m_xMarkStream.is(), "OStreamSection::OStreamSection : invalid argument !");
62 if (m_xOutStream.is() && m_xMarkStream.is())
64 m_nBlockStart = m_xMarkStream->createMark();
65 // a placeholder where we will write the overall length (within the destructor)
66 if (_nPresumedLength > 0)
67 m_nBlockLen = _nPresumedLength + sizeof(m_nBlockLen);
68 // as the caller did not consider - of course - the placeholder we are going to write
69 else
70 m_nBlockLen = 0;
71 m_xOutStream->writeLong(m_nBlockLen);
75 //-------------------------------------------------------------------------
76 OStreamSection::~OStreamSection()
78 try
79 { // don't allow any exceptions to leave this block, this may be called during the stack unwinding of an exception
80 // handling routing
81 if (m_xInStream.is() && m_xMarkStream.is())
82 { // we're working on an input stream
83 m_xMarkStream->jumpToMark(m_nBlockStart);
84 m_xInStream->skipBytes(m_nBlockLen);
85 m_xMarkStream->deleteMark(m_nBlockStart);
87 else if (m_xOutStream.is() && m_xMarkStream.is())
89 sal_Int32 nRealBlockLength = m_xMarkStream->offsetToMark(m_nBlockStart) - sizeof(m_nBlockLen);
90 if (m_nBlockLen && (m_nBlockLen == nRealBlockLength))
91 // nothing to do : the estimation the caller gave us (in the ctor) was correct
92 m_xMarkStream->deleteMark(m_nBlockStart);
93 else
94 { // the estimation was wrong (or we didn't get one)
95 m_nBlockLen = nRealBlockLength;
96 m_xMarkStream->jumpToMark(m_nBlockStart);
97 m_xOutStream->writeLong(m_nBlockLen);
98 m_xMarkStream->jumpToFurthest();
99 m_xMarkStream->deleteMark(m_nBlockStart);
103 catch(const staruno::Exception&)
107 // -----------------------------------------------------------------------------
108 sal_Int32 OStreamSection::available()
110 sal_Int32 nBytes = 0;
112 { // don't allow any exceptions to leave this block, this may be called during the stack unwinding of an exception
113 if (m_xInStream.is() && m_xMarkStream.is())
114 nBytes = m_xMarkStream->offsetToMark(m_nBlockStart) - sizeof(m_nBlockLen);
116 catch(const staruno::Exception&)
119 return nBytes;
121 // -----------------------------------------------------------------------------
123 } // namespace comphelper