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