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>
24 #include <osl/diagnose.h>
25 #include <tools/diagnose_ex.h>
26 
27 namespace oox {
28 
29 using namespace ::com::sun::star::io;
30 using namespace ::com::sun::star::uno;
31 
33 {
34 }
35 
37 {
38  // do not use isSeekable(), implementations may provide stream position and size even if not seekable
39  sal_Int64 nPos = tell();
40  sal_Int64 nLen = size();
41  return ((nPos >= 0) && (nLen >= 0)) ? ::std::max< sal_Int64 >( nLen - nPos, 0 ) : -1;
42 }
43 
44 void BinaryStreamBase::alignToBlock( sal_Int32 nBlockSize, sal_Int64 nAnchorPos )
45 {
46  sal_Int64 nStrmPos = tell();
47  // nothing to do, if stream is at anchor position
48  if( mbSeekable && (0 <= nAnchorPos) && (nAnchorPos != nStrmPos) && (nBlockSize > 1) )
49  {
50  // prevent modulo with negative arguments...
51  sal_Int64 nSkipSize = (nAnchorPos < nStrmPos) ?
52  (nBlockSize - ((nStrmPos - nAnchorPos - 1) % nBlockSize) - 1) :
53  ((nAnchorPos - nStrmPos) % nBlockSize);
54  seek( nStrmPos + nSkipSize );
55  }
56 }
57 
58 BinaryXSeekableStream::BinaryXSeekableStream( const Reference< XSeekable >& rxSeekable ) :
59  BinaryStreamBase( rxSeekable.is() ),
60  mxSeekable( rxSeekable )
61 {
62 }
63 
65 {
66 }
67 
68 sal_Int64 BinaryXSeekableStream::size() const
69 {
70  if( mxSeekable.is() ) try
71  {
72  return mxSeekable->getLength();
73  }
74  catch( Exception& )
75  {
76  TOOLS_WARN_EXCEPTION( "oox", "BinaryXSeekableStream::size" );
77  }
78  return -1;
79 }
80 
81 sal_Int64 BinaryXSeekableStream::tell() const
82 {
83  if( mxSeekable.is() ) try
84  {
85  return mxSeekable->getPosition();
86  }
87  catch( Exception& )
88  {
89  TOOLS_WARN_EXCEPTION( "oox", "BinaryXSeekableStream::tell" );
90  }
91  return -1;
92 }
93 
94 void BinaryXSeekableStream::seek( sal_Int64 nPos )
95 {
96  if( mxSeekable.is() ) try
97  {
98  mbEof = false;
99  mxSeekable->seek( nPos );
100  }
101  catch( Exception& )
102  {
103  mbEof = true;
104  }
105 }
106 
108 {
109  mxSeekable.clear();
110  mbEof = true;
111 }
112 
114  BinaryStreamBase( true ),
115  mpData( &rData ),
116  mnPos( 0 )
117 {
118 }
119 
121 {
122  return mpData ? mpData->getLength() : -1;
123 }
124 
126 {
127  return mpData ? mnPos : -1;
128 }
129 
130 void SequenceSeekableStream::seek( sal_Int64 nPos )
131 {
132  if( mpData )
133  {
134  mnPos = getLimitedValue< sal_Int32, sal_Int64 >( nPos, 0, mpData->getLength() );
135  mbEof = mnPos != nPos;
136  }
137 }
138 
140 {
141  mpData = nullptr;
142  mbEof = true;
143 }
144 
145 } // namespace oox
146 
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void close() override
Releases the reference to the UNO XSeekable interface.
Base class for binary stream classes.
virtual sal_Int64 tell() const override
Returns the current stream position, if wrapped stream is seekable, otherwise -1. ...
const bool mbSeekable
True = implementation supports seeking.
virtual sal_Int64 size() const =0
Implementations return the size of the stream, if possible.
virtual void seek(sal_Int64 nPos) override
Seeks the stream to the passed position, if wrapped stream is seekable.
virtual sal_Int64 tell() const override
Returns the current stream position.
css::uno::Sequence< sal_Int8 > StreamDataSequence
#define TOOLS_WARN_EXCEPTION(area, stream)
Reference< XSeekable > mxSeekable
Definition: olestorage.cxx:78
virtual ~BinaryXSeekableStream() override
css::uno::Reference< css::io::XSeekable > mxSeekable
Stream seeking interface.
SequenceSeekableStream(const StreamDataSequence &rData)
virtual sal_Int64 tell() const =0
Implementations return the current stream position, if possible.
virtual void close() override
Releases the reference to the data sequence.
bool mbEof
End of stream flag.
virtual sal_Int64 size() const override
Returns the size of the wrapped data sequence.
sal_Int64 getRemaining() const
Returns the size of the remaining data available in the stream, if stream supports size() and tell()...
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.
virtual void seek(sal_Int64 nPos)=0
Implementations seek the stream to the passed position, if the stream is seekable.
void alignToBlock(sal_Int32 nBlockSize, sal_Int64 nAnchorPos)
Seeks the stream forward to a position that is a multiple of the passed block size, if stream is seekable.
sal_Int32 mnPos
Current position in the sequence.
BinaryXSeekableStream(const css::uno::Reference< css::io::XSeekable > &rxSeekable)
sal_uInt16 nPos
const StreamDataSequence * mpData
Wrapped data sequence.