1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "oox/helper/binarystreambase.hxx"
29 
30 #include <com/sun/star/io/XSeekable.hpp>
31 #include <osl/diagnose.h>
32 
33 namespace oox {
34 
35 // ============================================================================
36 
37 using namespace ::com::sun::star::io;
38 using namespace ::com::sun::star::uno;
39 
40 // ============================================================================
41 
42 BinaryStreamBase::~BinaryStreamBase()
43 {
44 }
45 
46 sal_Int64 BinaryStreamBase::getRemaining() const
47 {
48     // do not use isSeekable(), implementations may provide stream position and size even if not seekable
49     sal_Int64 nPos = tell();
50     sal_Int64 nLen = size();
51     return ((nPos >= 0) && (nLen >= 0)) ? ::std::max< sal_Int64 >( nLen - nPos, 0 ) : -1;
52 }
53 
54 void BinaryStreamBase::alignToBlock( sal_Int32 nBlockSize, sal_Int64 nAnchorPos )
55 {
56     sal_Int64 nStrmPos = tell();
57     // nothing to do, if stream is at anchor position
58     if( mbSeekable && (0 <= nAnchorPos) && (nAnchorPos != nStrmPos) && (nBlockSize > 1) )
59     {
60         // prevent modulo with negative arguments...
61         sal_Int64 nSkipSize = (nAnchorPos < nStrmPos) ?
62             (nBlockSize - ((nStrmPos - nAnchorPos - 1) % nBlockSize) - 1) :
63             ((nAnchorPos - nStrmPos) % nBlockSize);
64         seek( nStrmPos + nSkipSize );
65     }
66 }
67 
68 // ============================================================================
69 
70 BinaryXSeekableStream::BinaryXSeekableStream( const Reference< XSeekable >& rxSeekable ) :
71     BinaryStreamBase( mxSeekable.is() ),
72     mxSeekable( rxSeekable )
73 {
74 }
75 
76 BinaryXSeekableStream::~BinaryXSeekableStream()
77 {
78 }
79 
80 sal_Int64 BinaryXSeekableStream::size() const
81 {
82     if( mxSeekable.is() ) try
83     {
84         return mxSeekable->getLength();
85     }
86     catch( Exception& )
87     {
88         OSL_ENSURE( false, "BinaryXSeekableStream::size - exception caught" );
89     }
90     return -1;
91 }
92 
93 sal_Int64 BinaryXSeekableStream::tell() const
94 {
95     if( mxSeekable.is() ) try
96     {
97         return mxSeekable->getPosition();
98     }
99     catch( Exception& )
100     {
101         OSL_ENSURE( false, "BinaryXSeekableStream::tell - exception caught" );
102     }
103     return -1;
104 }
105 
106 void BinaryXSeekableStream::seek( sal_Int64 nPos )
107 {
108     if( mxSeekable.is() ) try
109     {
110         mbEof = false;
111         mxSeekable->seek( nPos );
112     }
113     catch( Exception& )
114     {
115         mbEof = true;
116     }
117 }
118 
119 void BinaryXSeekableStream::close()
120 {
121     mxSeekable.clear();
122     mbEof = true;
123 }
124 
125 // ============================================================================
126 
127 SequenceSeekableStream::SequenceSeekableStream( const StreamDataSequence& rData ) :
128     BinaryStreamBase( true ),
129     mpData( &rData ),
130     mnPos( 0 )
131 {
132 }
133 
134 sal_Int64 SequenceSeekableStream::size() const
135 {
136     return mpData ? mpData->getLength() : -1;
137 }
138 
139 sal_Int64 SequenceSeekableStream::tell() const
140 {
141     return mpData ? mnPos : -1;
142 }
143 
144 void SequenceSeekableStream::seek( sal_Int64 nPos )
145 {
146     if( mpData )
147     {
148         mnPos = getLimitedValue< sal_Int32, sal_Int64 >( nPos, 0, mpData->getLength() );
149         mbEof = mnPos != nPos;
150     }
151 }
152 
153 void SequenceSeekableStream::close()
154 {
155     mpData = 0;
156     mbEof = true;
157 }
158 
159 // ============================================================================
160 
161 } // namespace oox
162