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