1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/streamsection.hxx>
31 #include <osl/diagnose.h>
36 //-------------------------------------------------------------------------
37 OStreamSection::OStreamSection(const staruno::Reference
< stario::XDataInputStream
>& _rxInput
)
38 :m_xMarkStream(_rxInput
, ::com::sun::star::uno::UNO_QUERY
)
39 ,m_xInStream(_rxInput
)
43 OSL_ENSURE(m_xInStream
.is() && m_xMarkStream
.is(), "OStreamSection::OStreamSection : invalid argument !");
44 if (m_xInStream
.is() && m_xMarkStream
.is())
46 m_nBlockLen
= _rxInput
->readLong();
47 m_nBlockStart
= m_xMarkStream
->createMark();
51 //-------------------------------------------------------------------------
52 OStreamSection::OStreamSection(const staruno::Reference
< stario::XDataOutputStream
>& _rxOutput
, sal_Int32 _nPresumedLength
)
53 :m_xMarkStream(_rxOutput
, ::com::sun::star::uno::UNO_QUERY
)
54 ,m_xOutStream(_rxOutput
)
58 OSL_ENSURE(m_xOutStream
.is() && m_xMarkStream
.is(), "OStreamSection::OStreamSection : invalid argument !");
59 if (m_xOutStream
.is() && m_xMarkStream
.is())
61 m_nBlockStart
= m_xMarkStream
->createMark();
62 // a placeholder where we will write the overall length (within the destructor)
63 if (_nPresumedLength
> 0)
64 m_nBlockLen
= _nPresumedLength
+ sizeof(m_nBlockLen
);
65 // as the caller did not consider - of course - the placeholder we are going to write
68 m_xOutStream
->writeLong(m_nBlockLen
);
72 //-------------------------------------------------------------------------
73 OStreamSection::~OStreamSection()
76 { // don't allow any exceptions to leave this block, this may be called during the stack unwinding of an exception
78 if (m_xInStream
.is() && m_xMarkStream
.is())
79 { // we're working on an input stream
80 m_xMarkStream
->jumpToMark(m_nBlockStart
);
81 m_xInStream
->skipBytes(m_nBlockLen
);
82 m_xMarkStream
->deleteMark(m_nBlockStart
);
84 else if (m_xOutStream
.is() && m_xMarkStream
.is())
86 sal_Int32 nRealBlockLength
= m_xMarkStream
->offsetToMark(m_nBlockStart
) - sizeof(m_nBlockLen
);
87 if (m_nBlockLen
&& (m_nBlockLen
== nRealBlockLength
))
88 // nothing to do : the estimation the caller gave us (in the ctor) was correct
89 m_xMarkStream
->deleteMark(m_nBlockStart
);
91 { // the estimation was wrong (or we didn't get one)
92 m_nBlockLen
= nRealBlockLength
;
93 m_xMarkStream
->jumpToMark(m_nBlockStart
);
94 m_xOutStream
->writeLong(m_nBlockLen
);
95 m_xMarkStream
->jumpToFurthest();
96 m_xMarkStream
->deleteMark(m_nBlockStart
);
100 catch(const staruno::Exception
&)
104 // -----------------------------------------------------------------------------
105 sal_Int32
OStreamSection::available()
107 sal_Int32 nBytes
= 0;
109 { // don't allow any exceptions to leave this block, this may be called during the stack unwinding of an exception
110 if (m_xInStream
.is() && m_xMarkStream
.is())
111 nBytes
= m_xMarkStream
->offsetToMark(m_nBlockStart
) - sizeof(m_nBlockLen
);
113 catch(const staruno::Exception
&)
118 // -----------------------------------------------------------------------------
120 } // namespace comphelper