LibreOffice Module oox (master) 1
binarystreambase.cxx
Go to the documentation of this file.
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
21#include <oox/helper/helper.hxx>
22
23#include <com/sun/star/io/XSeekable.hpp>
25
26namespace oox {
27
28using namespace ::com::sun::star::io;
29using namespace ::com::sun::star::uno;
30
32{
33}
34
36{
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;
41}
42
43void BinaryStreamBase::alignToBlock( sal_Int32 nBlockSize, sal_Int64 nAnchorPos )
44{
45 sal_Int64 nStrmPos = tell();
46 // nothing to do, if stream is at anchor position
47 if( mbSeekable && (0 <= nAnchorPos) && (nAnchorPos != nStrmPos) && (nBlockSize > 1) )
48 {
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 );
54 }
55}
56
57BinaryXSeekableStream::BinaryXSeekableStream( const Reference< XSeekable >& rxSeekable ) :
58 BinaryStreamBase( rxSeekable.is() ),
59 mxSeekable( rxSeekable )
60{
61}
62
64{
65}
66
68{
69 if( mxSeekable.is() ) try
70 {
71 return mxSeekable->getLength();
72 }
73 catch( Exception& )
74 {
75 TOOLS_WARN_EXCEPTION( "oox", "BinaryXSeekableStream::size" );
76 }
77 return -1;
78}
79
81{
82 if( mxSeekable.is() ) try
83 {
84 return mxSeekable->getPosition();
85 }
86 catch( Exception& )
87 {
88 TOOLS_WARN_EXCEPTION( "oox", "BinaryXSeekableStream::tell" );
89 }
90 return -1;
91}
92
93void BinaryXSeekableStream::seek( sal_Int64 nPos )
94{
95 if( mxSeekable.is() ) try
96 {
97 mbEof = false;
98 mxSeekable->seek( nPos );
99 }
100 catch( Exception& )
101 {
102 mbEof = true;
103 }
104}
105
107{
108 mxSeekable.clear();
109 mbEof = true;
110}
111
113 BinaryStreamBase( true ),
114 mpData( &rData ),
115 mnPos( 0 )
116{
117}
118
120{
121 return mpData ? mpData->getLength() : -1;
122}
123
125{
126 return mpData ? mnPos : -1;
127}
128
129void SequenceSeekableStream::seek( sal_Int64 nPos )
130{
131 if( mpData )
132 {
133 mnPos = getLimitedValue< sal_Int32, sal_Int64 >( nPos, 0, mpData->getLength() );
134 mbEof = mnPos != nPos;
135 }
136}
137
139{
140 mpData = nullptr;
141 mbEof = true;
142}
143
144} // namespace oox
145
146/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Base class for binary stream classes.
virtual sal_Int64 size() const =0
Implementations return the size of the stream, if possible.
void alignToBlock(sal_Int32 nBlockSize, sal_Int64 nAnchorPos)
Seeks the stream forward to a position that is a multiple of the passed block size,...
const bool mbSeekable
True = implementation supports seeking.
sal_Int64 getRemaining() const
Returns the size of the remaining data available in the stream, if stream supports size() and tell(),...
virtual sal_Int64 tell() const =0
Implementations return the current stream position, if possible.
virtual void seek(sal_Int64 nPos)=0
Implementations seek the stream to the passed position, if the stream is seekable.
bool mbEof
End of stream flag.
BinaryXSeekableStream(const css::uno::Reference< css::io::XSeekable > &rxSeekable)
virtual ~BinaryXSeekableStream() override
virtual void close() override
Releases the reference to the UNO XSeekable interface.
virtual sal_Int64 tell() const override
Returns the current stream position, if wrapped stream is seekable, otherwise -1.
css::uno::Reference< css::io::XSeekable > mxSeekable
Stream seeking interface.
virtual sal_Int64 size() const override
Returns the size of the stream, if wrapped stream is seekable, otherwise -1.
virtual void seek(sal_Int64 nPos) override
Seeks the stream to the passed position, if wrapped stream is seekable.
sal_Int32 mnPos
Current position in the sequence.
virtual void close() override
Releases the reference to the data sequence.
virtual void seek(sal_Int64 nPos) override
Seeks the stream to the passed position.
SequenceSeekableStream(const StreamDataSequence &rData)
virtual sal_Int64 size() const override
Returns the size of the wrapped data sequence.
virtual sal_Int64 tell() const override
Returns the current stream position.
const StreamDataSequence * mpData
Wrapped data sequence.
#define TOOLS_WARN_EXCEPTION(area, stream)
sal_uInt16 nPos
@ Exception
css::uno::Sequence< sal_Int8 > StreamDataSequence
Reference< XSeekable > mxSeekable
Definition: olestorage.cxx:79