1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski #include "oox/helper/binarystreambase.hxx"
25*b1cdbd2cSJim Jagielski
26*b1cdbd2cSJim Jagielski #include <com/sun/star/io/XSeekable.hpp>
27*b1cdbd2cSJim Jagielski #include <osl/diagnose.h>
28*b1cdbd2cSJim Jagielski
29*b1cdbd2cSJim Jagielski namespace oox {
30*b1cdbd2cSJim Jagielski
31*b1cdbd2cSJim Jagielski // ============================================================================
32*b1cdbd2cSJim Jagielski
33*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::io;
34*b1cdbd2cSJim Jagielski using namespace ::com::sun::star::uno;
35*b1cdbd2cSJim Jagielski
36*b1cdbd2cSJim Jagielski // ============================================================================
37*b1cdbd2cSJim Jagielski
~BinaryStreamBase()38*b1cdbd2cSJim Jagielski BinaryStreamBase::~BinaryStreamBase()
39*b1cdbd2cSJim Jagielski {
40*b1cdbd2cSJim Jagielski }
41*b1cdbd2cSJim Jagielski
getRemaining() const42*b1cdbd2cSJim Jagielski sal_Int64 BinaryStreamBase::getRemaining() const
43*b1cdbd2cSJim Jagielski {
44*b1cdbd2cSJim Jagielski // do not use isSeekable(), implementations may provide stream position and size even if not seekable
45*b1cdbd2cSJim Jagielski sal_Int64 nPos = tell();
46*b1cdbd2cSJim Jagielski sal_Int64 nLen = size();
47*b1cdbd2cSJim Jagielski return ((nPos >= 0) && (nLen >= 0)) ? ::std::max< sal_Int64 >( nLen - nPos, 0 ) : -1;
48*b1cdbd2cSJim Jagielski }
49*b1cdbd2cSJim Jagielski
alignToBlock(sal_Int32 nBlockSize,sal_Int64 nAnchorPos)50*b1cdbd2cSJim Jagielski void BinaryStreamBase::alignToBlock( sal_Int32 nBlockSize, sal_Int64 nAnchorPos )
51*b1cdbd2cSJim Jagielski {
52*b1cdbd2cSJim Jagielski sal_Int64 nStrmPos = tell();
53*b1cdbd2cSJim Jagielski // nothing to do, if stream is at anchor position
54*b1cdbd2cSJim Jagielski if( mbSeekable && (0 <= nAnchorPos) && (nAnchorPos != nStrmPos) && (nBlockSize > 1) )
55*b1cdbd2cSJim Jagielski {
56*b1cdbd2cSJim Jagielski // prevent modulo with negative arguments...
57*b1cdbd2cSJim Jagielski sal_Int64 nSkipSize = (nAnchorPos < nStrmPos) ?
58*b1cdbd2cSJim Jagielski (nBlockSize - ((nStrmPos - nAnchorPos - 1) % nBlockSize) - 1) :
59*b1cdbd2cSJim Jagielski ((nAnchorPos - nStrmPos) % nBlockSize);
60*b1cdbd2cSJim Jagielski seek( nStrmPos + nSkipSize );
61*b1cdbd2cSJim Jagielski }
62*b1cdbd2cSJim Jagielski }
63*b1cdbd2cSJim Jagielski
64*b1cdbd2cSJim Jagielski // ============================================================================
65*b1cdbd2cSJim Jagielski
BinaryXSeekableStream(const Reference<XSeekable> & rxSeekable)66*b1cdbd2cSJim Jagielski BinaryXSeekableStream::BinaryXSeekableStream( const Reference< XSeekable >& rxSeekable ) :
67*b1cdbd2cSJim Jagielski BinaryStreamBase( mxSeekable.is() ),
68*b1cdbd2cSJim Jagielski mxSeekable( rxSeekable )
69*b1cdbd2cSJim Jagielski {
70*b1cdbd2cSJim Jagielski }
71*b1cdbd2cSJim Jagielski
~BinaryXSeekableStream()72*b1cdbd2cSJim Jagielski BinaryXSeekableStream::~BinaryXSeekableStream()
73*b1cdbd2cSJim Jagielski {
74*b1cdbd2cSJim Jagielski }
75*b1cdbd2cSJim Jagielski
size() const76*b1cdbd2cSJim Jagielski sal_Int64 BinaryXSeekableStream::size() const
77*b1cdbd2cSJim Jagielski {
78*b1cdbd2cSJim Jagielski if( mxSeekable.is() ) try
79*b1cdbd2cSJim Jagielski {
80*b1cdbd2cSJim Jagielski return mxSeekable->getLength();
81*b1cdbd2cSJim Jagielski }
82*b1cdbd2cSJim Jagielski catch( Exception& )
83*b1cdbd2cSJim Jagielski {
84*b1cdbd2cSJim Jagielski OSL_ENSURE( false, "BinaryXSeekableStream::size - exception caught" );
85*b1cdbd2cSJim Jagielski }
86*b1cdbd2cSJim Jagielski return -1;
87*b1cdbd2cSJim Jagielski }
88*b1cdbd2cSJim Jagielski
tell() const89*b1cdbd2cSJim Jagielski sal_Int64 BinaryXSeekableStream::tell() const
90*b1cdbd2cSJim Jagielski {
91*b1cdbd2cSJim Jagielski if( mxSeekable.is() ) try
92*b1cdbd2cSJim Jagielski {
93*b1cdbd2cSJim Jagielski return mxSeekable->getPosition();
94*b1cdbd2cSJim Jagielski }
95*b1cdbd2cSJim Jagielski catch( Exception& )
96*b1cdbd2cSJim Jagielski {
97*b1cdbd2cSJim Jagielski OSL_ENSURE( false, "BinaryXSeekableStream::tell - exception caught" );
98*b1cdbd2cSJim Jagielski }
99*b1cdbd2cSJim Jagielski return -1;
100*b1cdbd2cSJim Jagielski }
101*b1cdbd2cSJim Jagielski
seek(sal_Int64 nPos)102*b1cdbd2cSJim Jagielski void BinaryXSeekableStream::seek( sal_Int64 nPos )
103*b1cdbd2cSJim Jagielski {
104*b1cdbd2cSJim Jagielski if( mxSeekable.is() ) try
105*b1cdbd2cSJim Jagielski {
106*b1cdbd2cSJim Jagielski mbEof = false;
107*b1cdbd2cSJim Jagielski mxSeekable->seek( nPos );
108*b1cdbd2cSJim Jagielski }
109*b1cdbd2cSJim Jagielski catch( Exception& )
110*b1cdbd2cSJim Jagielski {
111*b1cdbd2cSJim Jagielski mbEof = true;
112*b1cdbd2cSJim Jagielski }
113*b1cdbd2cSJim Jagielski }
114*b1cdbd2cSJim Jagielski
close()115*b1cdbd2cSJim Jagielski void BinaryXSeekableStream::close()
116*b1cdbd2cSJim Jagielski {
117*b1cdbd2cSJim Jagielski mxSeekable.clear();
118*b1cdbd2cSJim Jagielski mbEof = true;
119*b1cdbd2cSJim Jagielski }
120*b1cdbd2cSJim Jagielski
121*b1cdbd2cSJim Jagielski // ============================================================================
122*b1cdbd2cSJim Jagielski
SequenceSeekableStream(const StreamDataSequence & rData)123*b1cdbd2cSJim Jagielski SequenceSeekableStream::SequenceSeekableStream( const StreamDataSequence& rData ) :
124*b1cdbd2cSJim Jagielski BinaryStreamBase( true ),
125*b1cdbd2cSJim Jagielski mpData( &rData ),
126*b1cdbd2cSJim Jagielski mnPos( 0 )
127*b1cdbd2cSJim Jagielski {
128*b1cdbd2cSJim Jagielski }
129*b1cdbd2cSJim Jagielski
size() const130*b1cdbd2cSJim Jagielski sal_Int64 SequenceSeekableStream::size() const
131*b1cdbd2cSJim Jagielski {
132*b1cdbd2cSJim Jagielski return mpData ? mpData->getLength() : -1;
133*b1cdbd2cSJim Jagielski }
134*b1cdbd2cSJim Jagielski
tell() const135*b1cdbd2cSJim Jagielski sal_Int64 SequenceSeekableStream::tell() const
136*b1cdbd2cSJim Jagielski {
137*b1cdbd2cSJim Jagielski return mpData ? mnPos : -1;
138*b1cdbd2cSJim Jagielski }
139*b1cdbd2cSJim Jagielski
seek(sal_Int64 nPos)140*b1cdbd2cSJim Jagielski void SequenceSeekableStream::seek( sal_Int64 nPos )
141*b1cdbd2cSJim Jagielski {
142*b1cdbd2cSJim Jagielski if( mpData )
143*b1cdbd2cSJim Jagielski {
144*b1cdbd2cSJim Jagielski mnPos = getLimitedValue< sal_Int32, sal_Int64 >( nPos, 0, mpData->getLength() );
145*b1cdbd2cSJim Jagielski mbEof = mnPos != nPos;
146*b1cdbd2cSJim Jagielski }
147*b1cdbd2cSJim Jagielski }
148*b1cdbd2cSJim Jagielski
close()149*b1cdbd2cSJim Jagielski void SequenceSeekableStream::close()
150*b1cdbd2cSJim Jagielski {
151*b1cdbd2cSJim Jagielski mpData = 0;
152*b1cdbd2cSJim Jagielski mbEof = true;
153*b1cdbd2cSJim Jagielski }
154*b1cdbd2cSJim Jagielski
155*b1cdbd2cSJim Jagielski // ============================================================================
156*b1cdbd2cSJim Jagielski
157*b1cdbd2cSJim Jagielski } // namespace oox
158