1*ca5ec200SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ca5ec200SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ca5ec200SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ca5ec200SAndrew Rist * distributed with this work for additional information 6*ca5ec200SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ca5ec200SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ca5ec200SAndrew Rist * "License"); you may not use this file except in compliance 9*ca5ec200SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*ca5ec200SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*ca5ec200SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ca5ec200SAndrew Rist * software distributed under the License is distributed on an 15*ca5ec200SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ca5ec200SAndrew Rist * KIND, either express or implied. See the License for the 17*ca5ec200SAndrew Rist * specific language governing permissions and limitations 18*ca5ec200SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*ca5ec200SAndrew Rist *************************************************************/ 21*ca5ec200SAndrew Rist 22*ca5ec200SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "oox/helper/binarystreambase.hxx" 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp> 27cdf0e10cSrcweir #include <osl/diagnose.h> 28cdf0e10cSrcweir 29cdf0e10cSrcweir namespace oox { 30cdf0e10cSrcweir 31cdf0e10cSrcweir // ============================================================================ 32cdf0e10cSrcweir 33cdf0e10cSrcweir using namespace ::com::sun::star::io; 34cdf0e10cSrcweir using namespace ::com::sun::star::uno; 35cdf0e10cSrcweir 36cdf0e10cSrcweir // ============================================================================ 37cdf0e10cSrcweir 38cdf0e10cSrcweir BinaryStreamBase::~BinaryStreamBase() 39cdf0e10cSrcweir { 40cdf0e10cSrcweir } 41cdf0e10cSrcweir 42cdf0e10cSrcweir sal_Int64 BinaryStreamBase::getRemaining() const 43cdf0e10cSrcweir { 44cdf0e10cSrcweir // do not use isSeekable(), implementations may provide stream position and size even if not seekable 45cdf0e10cSrcweir sal_Int64 nPos = tell(); 46cdf0e10cSrcweir sal_Int64 nLen = size(); 47cdf0e10cSrcweir return ((nPos >= 0) && (nLen >= 0)) ? ::std::max< sal_Int64 >( nLen - nPos, 0 ) : -1; 48cdf0e10cSrcweir } 49cdf0e10cSrcweir 50cdf0e10cSrcweir void BinaryStreamBase::alignToBlock( sal_Int32 nBlockSize, sal_Int64 nAnchorPos ) 51cdf0e10cSrcweir { 52cdf0e10cSrcweir sal_Int64 nStrmPos = tell(); 53cdf0e10cSrcweir // nothing to do, if stream is at anchor position 54cdf0e10cSrcweir if( mbSeekable && (0 <= nAnchorPos) && (nAnchorPos != nStrmPos) && (nBlockSize > 1) ) 55cdf0e10cSrcweir { 56cdf0e10cSrcweir // prevent modulo with negative arguments... 57cdf0e10cSrcweir sal_Int64 nSkipSize = (nAnchorPos < nStrmPos) ? 58cdf0e10cSrcweir (nBlockSize - ((nStrmPos - nAnchorPos - 1) % nBlockSize) - 1) : 59cdf0e10cSrcweir ((nAnchorPos - nStrmPos) % nBlockSize); 60cdf0e10cSrcweir seek( nStrmPos + nSkipSize ); 61cdf0e10cSrcweir } 62cdf0e10cSrcweir } 63cdf0e10cSrcweir 64cdf0e10cSrcweir // ============================================================================ 65cdf0e10cSrcweir 66cdf0e10cSrcweir BinaryXSeekableStream::BinaryXSeekableStream( const Reference< XSeekable >& rxSeekable ) : 67cdf0e10cSrcweir BinaryStreamBase( mxSeekable.is() ), 68cdf0e10cSrcweir mxSeekable( rxSeekable ) 69cdf0e10cSrcweir { 70cdf0e10cSrcweir } 71cdf0e10cSrcweir 72cdf0e10cSrcweir BinaryXSeekableStream::~BinaryXSeekableStream() 73cdf0e10cSrcweir { 74cdf0e10cSrcweir } 75cdf0e10cSrcweir 76cdf0e10cSrcweir sal_Int64 BinaryXSeekableStream::size() const 77cdf0e10cSrcweir { 78cdf0e10cSrcweir if( mxSeekable.is() ) try 79cdf0e10cSrcweir { 80cdf0e10cSrcweir return mxSeekable->getLength(); 81cdf0e10cSrcweir } 82cdf0e10cSrcweir catch( Exception& ) 83cdf0e10cSrcweir { 84cdf0e10cSrcweir OSL_ENSURE( false, "BinaryXSeekableStream::size - exception caught" ); 85cdf0e10cSrcweir } 86cdf0e10cSrcweir return -1; 87cdf0e10cSrcweir } 88cdf0e10cSrcweir 89cdf0e10cSrcweir sal_Int64 BinaryXSeekableStream::tell() const 90cdf0e10cSrcweir { 91cdf0e10cSrcweir if( mxSeekable.is() ) try 92cdf0e10cSrcweir { 93cdf0e10cSrcweir return mxSeekable->getPosition(); 94cdf0e10cSrcweir } 95cdf0e10cSrcweir catch( Exception& ) 96cdf0e10cSrcweir { 97cdf0e10cSrcweir OSL_ENSURE( false, "BinaryXSeekableStream::tell - exception caught" ); 98cdf0e10cSrcweir } 99cdf0e10cSrcweir return -1; 100cdf0e10cSrcweir } 101cdf0e10cSrcweir 102cdf0e10cSrcweir void BinaryXSeekableStream::seek( sal_Int64 nPos ) 103cdf0e10cSrcweir { 104cdf0e10cSrcweir if( mxSeekable.is() ) try 105cdf0e10cSrcweir { 106cdf0e10cSrcweir mbEof = false; 107cdf0e10cSrcweir mxSeekable->seek( nPos ); 108cdf0e10cSrcweir } 109cdf0e10cSrcweir catch( Exception& ) 110cdf0e10cSrcweir { 111cdf0e10cSrcweir mbEof = true; 112cdf0e10cSrcweir } 113cdf0e10cSrcweir } 114cdf0e10cSrcweir 115cdf0e10cSrcweir void BinaryXSeekableStream::close() 116cdf0e10cSrcweir { 117cdf0e10cSrcweir mxSeekable.clear(); 118cdf0e10cSrcweir mbEof = true; 119cdf0e10cSrcweir } 120cdf0e10cSrcweir 121cdf0e10cSrcweir // ============================================================================ 122cdf0e10cSrcweir 123cdf0e10cSrcweir SequenceSeekableStream::SequenceSeekableStream( const StreamDataSequence& rData ) : 124cdf0e10cSrcweir BinaryStreamBase( true ), 125cdf0e10cSrcweir mpData( &rData ), 126cdf0e10cSrcweir mnPos( 0 ) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir } 129cdf0e10cSrcweir 130cdf0e10cSrcweir sal_Int64 SequenceSeekableStream::size() const 131cdf0e10cSrcweir { 132cdf0e10cSrcweir return mpData ? mpData->getLength() : -1; 133cdf0e10cSrcweir } 134cdf0e10cSrcweir 135cdf0e10cSrcweir sal_Int64 SequenceSeekableStream::tell() const 136cdf0e10cSrcweir { 137cdf0e10cSrcweir return mpData ? mnPos : -1; 138cdf0e10cSrcweir } 139cdf0e10cSrcweir 140cdf0e10cSrcweir void SequenceSeekableStream::seek( sal_Int64 nPos ) 141cdf0e10cSrcweir { 142cdf0e10cSrcweir if( mpData ) 143cdf0e10cSrcweir { 144cdf0e10cSrcweir mnPos = getLimitedValue< sal_Int32, sal_Int64 >( nPos, 0, mpData->getLength() ); 145cdf0e10cSrcweir mbEof = mnPos != nPos; 146cdf0e10cSrcweir } 147cdf0e10cSrcweir } 148cdf0e10cSrcweir 149cdf0e10cSrcweir void SequenceSeekableStream::close() 150cdf0e10cSrcweir { 151cdf0e10cSrcweir mpData = 0; 152cdf0e10cSrcweir mbEof = true; 153cdf0e10cSrcweir } 154cdf0e10cSrcweir 155cdf0e10cSrcweir // ============================================================================ 156cdf0e10cSrcweir 157cdf0e10cSrcweir } // namespace oox 158