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
20 #include <oox/helper/binarystreambase.hxx>
21 #include <oox/helper/helper.hxx>
22
23 #include <com/sun/star/io/XSeekable.hpp>
24 #include <osl/diagnose.h>
25
26 namespace oox {
27
28 using namespace ::com::sun::star::io;
29 using namespace ::com::sun::star::uno;
30
~BinaryStreamBase()31 BinaryStreamBase::~BinaryStreamBase()
32 {
33 }
34
getRemaining() const35 sal_Int64 BinaryStreamBase::getRemaining() const
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
alignToBlock(sal_Int32 nBlockSize,sal_Int64 nAnchorPos)43 void 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
BinaryXSeekableStream(const Reference<XSeekable> & rxSeekable)57 BinaryXSeekableStream::BinaryXSeekableStream( const Reference< XSeekable >& rxSeekable ) :
58 BinaryStreamBase( rxSeekable.is() ),
59 mxSeekable( rxSeekable )
60 {
61 }
62
~BinaryXSeekableStream()63 BinaryXSeekableStream::~BinaryXSeekableStream()
64 {
65 }
66
size() const67 sal_Int64 BinaryXSeekableStream::size() const
68 {
69 if( mxSeekable.is() ) try
70 {
71 return mxSeekable->getLength();
72 }
73 catch( Exception& )
74 {
75 OSL_FAIL( "BinaryXSeekableStream::size - exception caught" );
76 }
77 return -1;
78 }
79
tell() const80 sal_Int64 BinaryXSeekableStream::tell() const
81 {
82 if( mxSeekable.is() ) try
83 {
84 return mxSeekable->getPosition();
85 }
86 catch( Exception& )
87 {
88 OSL_FAIL( "BinaryXSeekableStream::tell - exception caught" );
89 }
90 return -1;
91 }
92
seek(sal_Int64 nPos)93 void 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
close()106 void BinaryXSeekableStream::close()
107 {
108 mxSeekable.clear();
109 mbEof = true;
110 }
111
SequenceSeekableStream(const StreamDataSequence & rData)112 SequenceSeekableStream::SequenceSeekableStream( const StreamDataSequence& rData ) :
113 BinaryStreamBase( true ),
114 mpData( &rData ),
115 mnPos( 0 )
116 {
117 }
118
size() const119 sal_Int64 SequenceSeekableStream::size() const
120 {
121 return mpData ? mpData->getLength() : -1;
122 }
123
tell() const124 sal_Int64 SequenceSeekableStream::tell() const
125 {
126 return mpData ? mnPos : -1;
127 }
128
seek(sal_Int64 nPos)129 void 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
close()138 void SequenceSeekableStream::close()
139 {
140 mpData = nullptr;
141 mbEof = true;
142 }
143
144 } // namespace oox
145
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
147