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/streamsection.hxx> 31 #include <osl/diagnose.h> 32 33 namespace comphelper 34 { 35 36 //------------------------------------------------------------------------- 37 OStreamSection::OStreamSection(const staruno::Reference< stario::XDataInputStream >& _rxInput) 38 :m_xMarkStream(_rxInput, ::com::sun::star::uno::UNO_QUERY) 39 ,m_xInStream(_rxInput) 40 ,m_nBlockStart(-1) 41 ,m_nBlockLen(-1) 42 { 43 OSL_ENSURE(m_xInStream.is() && m_xMarkStream.is(), "OStreamSection::OStreamSection : invalid argument !"); 44 if (m_xInStream.is() && m_xMarkStream.is()) 45 { 46 m_nBlockLen = _rxInput->readLong(); 47 m_nBlockStart = m_xMarkStream->createMark(); 48 } 49 } 50 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) 55 ,m_nBlockStart(-1) 56 ,m_nBlockLen(-1) 57 { 58 OSL_ENSURE(m_xOutStream.is() && m_xMarkStream.is(), "OStreamSection::OStreamSection : invalid argument !"); 59 if (m_xOutStream.is() && m_xMarkStream.is()) 60 { 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 66 else 67 m_nBlockLen = 0; 68 m_xOutStream->writeLong(m_nBlockLen); 69 } 70 } 71 72 //------------------------------------------------------------------------- 73 OStreamSection::~OStreamSection() 74 { 75 try 76 { // don't allow any exceptions to leave this block, this may be called during the stack unwinding of an exception 77 // handling routing 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); 83 } 84 else if (m_xOutStream.is() && m_xMarkStream.is()) 85 { 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); 90 else 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); 97 } 98 } 99 } 100 catch(const staruno::Exception&) 101 { 102 } 103 } 104 // ----------------------------------------------------------------------------- 105 sal_Int32 OStreamSection::available() 106 { 107 sal_Int32 nBytes = 0; 108 try 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); 112 } 113 catch(const staruno::Exception&) 114 { 115 } 116 return nBytes; 117 } 118 // ----------------------------------------------------------------------------- 119 120 } // namespace comphelper 121 122 123