bump product version to 6.3.0.0.beta1
[LibreOffice.git] / oox / source / helper / binarystreambase.cxx
blob5da64a84eb1f533ac39e2769d539137029d41ba0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <oox/helper/binarystreambase.hxx>
21 #include <oox/helper/helper.hxx>
23 #include <com/sun/star/io/XSeekable.hpp>
24 #include <osl/diagnose.h>
26 namespace oox {
28 using namespace ::com::sun::star::io;
29 using namespace ::com::sun::star::uno;
31 BinaryStreamBase::~BinaryStreamBase()
35 sal_Int64 BinaryStreamBase::getRemaining() const
37 // do not use isSeekable(), implementations may provide stream position and size even if not seekable
38 sal_Int64 nPos = tell();
39 sal_Int64 nLen = size();
40 return ((nPos >= 0) && (nLen >= 0)) ? ::std::max< sal_Int64 >( nLen - nPos, 0 ) : -1;
43 void BinaryStreamBase::alignToBlock( sal_Int32 nBlockSize, sal_Int64 nAnchorPos )
45 sal_Int64 nStrmPos = tell();
46 // nothing to do, if stream is at anchor position
47 if( mbSeekable && (0 <= nAnchorPos) && (nAnchorPos != nStrmPos) && (nBlockSize > 1) )
49 // prevent modulo with negative arguments...
50 sal_Int64 nSkipSize = (nAnchorPos < nStrmPos) ?
51 (nBlockSize - ((nStrmPos - nAnchorPos - 1) % nBlockSize) - 1) :
52 ((nAnchorPos - nStrmPos) % nBlockSize);
53 seek( nStrmPos + nSkipSize );
57 BinaryXSeekableStream::BinaryXSeekableStream( const Reference< XSeekable >& rxSeekable ) :
58 BinaryStreamBase( rxSeekable.is() ),
59 mxSeekable( rxSeekable )
63 BinaryXSeekableStream::~BinaryXSeekableStream()
67 sal_Int64 BinaryXSeekableStream::size() const
69 if( mxSeekable.is() ) try
71 return mxSeekable->getLength();
73 catch( Exception& )
75 OSL_FAIL( "BinaryXSeekableStream::size - exception caught" );
77 return -1;
80 sal_Int64 BinaryXSeekableStream::tell() const
82 if( mxSeekable.is() ) try
84 return mxSeekable->getPosition();
86 catch( Exception& )
88 OSL_FAIL( "BinaryXSeekableStream::tell - exception caught" );
90 return -1;
93 void BinaryXSeekableStream::seek( sal_Int64 nPos )
95 if( mxSeekable.is() ) try
97 mbEof = false;
98 mxSeekable->seek( nPos );
100 catch( Exception& )
102 mbEof = true;
106 void BinaryXSeekableStream::close()
108 mxSeekable.clear();
109 mbEof = true;
112 SequenceSeekableStream::SequenceSeekableStream( const StreamDataSequence& rData ) :
113 BinaryStreamBase( true ),
114 mpData( &rData ),
115 mnPos( 0 )
119 sal_Int64 SequenceSeekableStream::size() const
121 return mpData ? mpData->getLength() : -1;
124 sal_Int64 SequenceSeekableStream::tell() const
126 return mpData ? mnPos : -1;
129 void SequenceSeekableStream::seek( sal_Int64 nPos )
131 if( mpData )
133 mnPos = getLimitedValue< sal_Int32, sal_Int64 >( nPos, 0, mpData->getLength() );
134 mbEof = mnPos != nPos;
138 void SequenceSeekableStream::close()
140 mpData = nullptr;
141 mbEof = true;
144 } // namespace oox
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */