Line data Source code
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 :
22 : #include <com/sun/star/io/XSeekable.hpp>
23 : #include <osl/diagnose.h>
24 :
25 : namespace oox {
26 :
27 : using namespace ::com::sun::star::io;
28 : using namespace ::com::sun::star::uno;
29 :
30 7637 : BinaryStreamBase::~BinaryStreamBase()
31 : {
32 7637 : }
33 :
34 113 : sal_Int64 BinaryStreamBase::getRemaining() const
35 : {
36 : // do not use isSeekable(), implementations may provide stream position and size even if not seekable
37 113 : sal_Int64 nPos = tell();
38 113 : sal_Int64 nLen = size();
39 113 : return ((nPos >= 0) && (nLen >= 0)) ? ::std::max< sal_Int64 >( nLen - nPos, 0 ) : -1;
40 : }
41 :
42 2 : void BinaryStreamBase::alignToBlock( sal_Int32 nBlockSize, sal_Int64 nAnchorPos )
43 : {
44 2 : sal_Int64 nStrmPos = tell();
45 : // nothing to do, if stream is at anchor position
46 2 : if( mbSeekable && (0 <= nAnchorPos) && (nAnchorPos != nStrmPos) && (nBlockSize > 1) )
47 : {
48 : // prevent modulo with negative arguments...
49 : sal_Int64 nSkipSize = (nAnchorPos < nStrmPos) ?
50 2 : (nBlockSize - ((nStrmPos - nAnchorPos - 1) % nBlockSize) - 1) :
51 4 : ((nAnchorPos - nStrmPos) % nBlockSize);
52 2 : seek( nStrmPos + nSkipSize );
53 : }
54 2 : }
55 :
56 4431 : BinaryXSeekableStream::BinaryXSeekableStream( const Reference< XSeekable >& rxSeekable ) :
57 0 : BinaryStreamBase( rxSeekable.is() ),
58 4431 : mxSeekable( rxSeekable )
59 : {
60 4431 : }
61 :
62 4431 : BinaryXSeekableStream::~BinaryXSeekableStream()
63 : {
64 4431 : }
65 :
66 111 : sal_Int64 BinaryXSeekableStream::size() const
67 : {
68 111 : if( mxSeekable.is() ) try
69 : {
70 111 : return mxSeekable->getLength();
71 : }
72 0 : catch( Exception& )
73 : {
74 : OSL_FAIL( "BinaryXSeekableStream::size - exception caught" );
75 : }
76 0 : return -1;
77 : }
78 :
79 382 : sal_Int64 BinaryXSeekableStream::tell() const
80 : {
81 382 : if( mxSeekable.is() ) try
82 : {
83 382 : return mxSeekable->getPosition();
84 : }
85 0 : catch( Exception& )
86 : {
87 : OSL_FAIL( "BinaryXSeekableStream::tell - exception caught" );
88 : }
89 0 : return -1;
90 : }
91 :
92 434 : void BinaryXSeekableStream::seek( sal_Int64 nPos )
93 : {
94 434 : if( mxSeekable.is() ) try
95 : {
96 434 : mbEof = false;
97 434 : mxSeekable->seek( nPos );
98 : }
99 0 : catch( Exception& )
100 : {
101 0 : mbEof = true;
102 : }
103 434 : }
104 :
105 4431 : void BinaryXSeekableStream::close()
106 : {
107 4431 : mxSeekable.clear();
108 4431 : mbEof = true;
109 4431 : }
110 :
111 2924 : SequenceSeekableStream::SequenceSeekableStream( const StreamDataSequence& rData ) :
112 : BinaryStreamBase( true ),
113 : mpData( &rData ),
114 2924 : mnPos( 0 )
115 : {
116 2924 : }
117 :
118 23 : sal_Int64 SequenceSeekableStream::size() const
119 : {
120 23 : return mpData ? mpData->getLength() : -1;
121 : }
122 :
123 268 : sal_Int64 SequenceSeekableStream::tell() const
124 : {
125 268 : return mpData ? mnPos : -1;
126 : }
127 :
128 592 : void SequenceSeekableStream::seek( sal_Int64 nPos )
129 : {
130 592 : if( mpData )
131 : {
132 592 : mnPos = getLimitedValue< sal_Int32, sal_Int64 >( nPos, 0, mpData->getLength() );
133 592 : mbEof = mnPos != nPos;
134 : }
135 592 : }
136 :
137 0 : void SequenceSeekableStream::close()
138 : {
139 0 : mpData = 0;
140 0 : mbEof = true;
141 0 : }
142 :
143 : } // namespace oox
144 :
145 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|