Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <comphelper/streamsection.hxx>
21 : #include <osl/diagnose.h>
22 :
23 : namespace comphelper
24 : {
25 :
26 :
27 0 : OStreamSection::OStreamSection(const staruno::Reference< stario::XDataInputStream >& _rxInput)
28 : :m_xMarkStream(_rxInput, ::com::sun::star::uno::UNO_QUERY)
29 : ,m_xInStream(_rxInput)
30 : ,m_nBlockStart(-1)
31 0 : ,m_nBlockLen(-1)
32 : {
33 : OSL_ENSURE(m_xInStream.is() && m_xMarkStream.is(), "OStreamSection::OStreamSection : invalid argument !");
34 0 : if (m_xInStream.is() && m_xMarkStream.is())
35 : {
36 0 : m_nBlockLen = _rxInput->readLong();
37 0 : m_nBlockStart = m_xMarkStream->createMark();
38 : }
39 0 : }
40 :
41 :
42 0 : OStreamSection::OStreamSection(const staruno::Reference< stario::XDataOutputStream >& _rxOutput, sal_Int32 _nPresumedLength)
43 : :m_xMarkStream(_rxOutput, ::com::sun::star::uno::UNO_QUERY)
44 : ,m_xOutStream(_rxOutput)
45 : ,m_nBlockStart(-1)
46 0 : ,m_nBlockLen(-1)
47 : {
48 : OSL_ENSURE(m_xOutStream.is() && m_xMarkStream.is(), "OStreamSection::OStreamSection : invalid argument !");
49 0 : if (m_xOutStream.is() && m_xMarkStream.is())
50 : {
51 0 : m_nBlockStart = m_xMarkStream->createMark();
52 : // a placeholder where we will write the overall length (within the destructor)
53 0 : if (_nPresumedLength > 0)
54 0 : m_nBlockLen = _nPresumedLength + sizeof(m_nBlockLen);
55 : // as the caller did not consider - of course - the placeholder we are going to write
56 : else
57 0 : m_nBlockLen = 0;
58 0 : m_xOutStream->writeLong(m_nBlockLen);
59 : }
60 0 : }
61 :
62 :
63 0 : OStreamSection::~OStreamSection()
64 : {
65 : try
66 : { // don't allow any exceptions to leave this block, this may be called during the stack unwinding of an exception
67 : // handling routing
68 0 : if (m_xInStream.is() && m_xMarkStream.is())
69 : { // we're working on an input stream
70 0 : m_xMarkStream->jumpToMark(m_nBlockStart);
71 0 : m_xInStream->skipBytes(m_nBlockLen);
72 0 : m_xMarkStream->deleteMark(m_nBlockStart);
73 : }
74 0 : else if (m_xOutStream.is() && m_xMarkStream.is())
75 : {
76 0 : sal_Int32 nRealBlockLength = m_xMarkStream->offsetToMark(m_nBlockStart) - sizeof(m_nBlockLen);
77 0 : if (m_nBlockLen && (m_nBlockLen == nRealBlockLength))
78 : // nothing to do : the estimation the caller gave us (in the ctor) was correct
79 0 : m_xMarkStream->deleteMark(m_nBlockStart);
80 : else
81 : { // the estimation was wrong (or we didn't get one)
82 0 : m_nBlockLen = nRealBlockLength;
83 0 : m_xMarkStream->jumpToMark(m_nBlockStart);
84 0 : m_xOutStream->writeLong(m_nBlockLen);
85 0 : m_xMarkStream->jumpToFurthest();
86 0 : m_xMarkStream->deleteMark(m_nBlockStart);
87 : }
88 : }
89 : }
90 0 : catch(const staruno::Exception&)
91 : {
92 : }
93 0 : }
94 :
95 :
96 : } // namespace comphelper
97 :
98 :
99 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|