Branch libreoffice-5-0-4
[LibreOffice.git] / oox / source / helper / binarystreambase.cxx
blob929fb6069b52850c28966909232e573420bd21a8
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"
22 #include <com/sun/star/io/XSeekable.hpp>
23 #include <osl/diagnose.h>
25 namespace oox {
27 using namespace ::com::sun::star::io;
28 using namespace ::com::sun::star::uno;
30 BinaryStreamBase::~BinaryStreamBase()
34 sal_Int64 BinaryStreamBase::getRemaining() const
36 // do not use isSeekable(), implementations may provide stream position and size even if not seekable
37 sal_Int64 nPos = tell();
38 sal_Int64 nLen = size();
39 return ((nPos >= 0) && (nLen >= 0)) ? ::std::max< sal_Int64 >( nLen - nPos, 0 ) : -1;
42 void BinaryStreamBase::alignToBlock( sal_Int32 nBlockSize, sal_Int64 nAnchorPos )
44 sal_Int64 nStrmPos = tell();
45 // nothing to do, if stream is at anchor position
46 if( mbSeekable && (0 <= nAnchorPos) && (nAnchorPos != nStrmPos) && (nBlockSize > 1) )
48 // prevent modulo with negative arguments...
49 sal_Int64 nSkipSize = (nAnchorPos < nStrmPos) ?
50 (nBlockSize - ((nStrmPos - nAnchorPos - 1) % nBlockSize) - 1) :
51 ((nAnchorPos - nStrmPos) % nBlockSize);
52 seek( nStrmPos + nSkipSize );
56 BinaryXSeekableStream::BinaryXSeekableStream( const Reference< XSeekable >& rxSeekable ) :
57 BinaryStreamBase( rxSeekable.is() ),
58 mxSeekable( rxSeekable )
62 BinaryXSeekableStream::~BinaryXSeekableStream()
66 sal_Int64 BinaryXSeekableStream::size() const
68 if( mxSeekable.is() ) try
70 return mxSeekable->getLength();
72 catch( Exception& )
74 OSL_FAIL( "BinaryXSeekableStream::size - exception caught" );
76 return -1;
79 sal_Int64 BinaryXSeekableStream::tell() const
81 if( mxSeekable.is() ) try
83 return mxSeekable->getPosition();
85 catch( Exception& )
87 OSL_FAIL( "BinaryXSeekableStream::tell - exception caught" );
89 return -1;
92 void BinaryXSeekableStream::seek( sal_Int64 nPos )
94 if( mxSeekable.is() ) try
96 mbEof = false;
97 mxSeekable->seek( nPos );
99 catch( Exception& )
101 mbEof = true;
105 void BinaryXSeekableStream::close()
107 mxSeekable.clear();
108 mbEof = true;
111 SequenceSeekableStream::SequenceSeekableStream( const StreamDataSequence& rData ) :
112 BinaryStreamBase( true ),
113 mpData( &rData ),
114 mnPos( 0 )
118 sal_Int64 SequenceSeekableStream::size() const
120 return mpData ? mpData->getLength() : -1;
123 sal_Int64 SequenceSeekableStream::tell() const
125 return mpData ? mnPos : -1;
128 void SequenceSeekableStream::seek( sal_Int64 nPos )
130 if( mpData )
132 mnPos = getLimitedValue< sal_Int32, sal_Int64 >( nPos, 0, mpData->getLength() );
133 mbEof = mnPos != nPos;
137 void SequenceSeekableStream::close()
139 mpData = 0;
140 mbEof = true;
143 } // namespace oox
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */