1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/seqstream.hxx>
22 #include <osl/diagnose.h>
28 using namespace ::com::sun::star::lang
;
29 using namespace ::com::sun::star::io
;
30 using namespace ::com::sun::star::uno
;
31 using namespace ::osl
;
34 // class SequenceInputStream
38 SequenceInputStream::SequenceInputStream(
39 css::uno::Sequence
<sal_Int8
> const & rData
)
45 // checks if closed, returns available size, not mutex-protected
47 inline sal_Int32
SequenceInputStream::avail()
50 throw NotConnectedException(OUString(), *this);
52 return m_aData
.getLength() - m_nPos
;
55 // com::sun::star::io::XInputStream
57 sal_Int32 SAL_CALL
SequenceInputStream::readBytes( Sequence
<sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
58 throw(NotConnectedException
, BufferSizeExceededException
,
59 IOException
, RuntimeException
, std::exception
)
61 ::osl::MutexGuard
aGuard( m_aMutex
);
63 sal_Int32 nAvail
= avail();
66 throw BufferSizeExceededException(OUString(),*this);
68 if (nAvail
< nBytesToRead
)
69 nBytesToRead
= nAvail
;
71 aData
.realloc(nBytesToRead
);
72 memcpy(aData
.getArray(), m_aData
.getConstArray() + m_nPos
, nBytesToRead
);
73 m_nPos
+= nBytesToRead
;
79 sal_Int32 SAL_CALL
SequenceInputStream::readSomeBytes( Sequence
<sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
80 throw(NotConnectedException
, BufferSizeExceededException
,
81 IOException
, RuntimeException
, std::exception
)
83 // all data is available at once
84 return readBytes(aData
, nMaxBytesToRead
);
88 void SAL_CALL
SequenceInputStream::skipBytes( sal_Int32 nBytesToSkip
)
89 throw(NotConnectedException
, BufferSizeExceededException
,
90 IOException
, RuntimeException
, std::exception
)
92 ::osl::MutexGuard
aGuard( m_aMutex
);
94 sal_Int32 nAvail
= avail();
97 throw BufferSizeExceededException(OUString(),*this);
99 if (nAvail
< nBytesToSkip
)
100 nBytesToSkip
= nAvail
;
102 m_nPos
+= nBytesToSkip
;
106 sal_Int32 SAL_CALL
SequenceInputStream::available( )
107 throw(NotConnectedException
, IOException
, RuntimeException
, std::exception
)
109 ::osl::MutexGuard
aGuard( m_aMutex
);
115 void SAL_CALL
SequenceInputStream::closeInput( )
116 throw(NotConnectedException
, IOException
, RuntimeException
, std::exception
)
119 throw NotConnectedException(OUString(), *this);
124 void SAL_CALL
SequenceInputStream::seek( sal_Int64 location
) throw (IllegalArgumentException
, IOException
, RuntimeException
, std::exception
)
126 if ( location
> m_aData
.getLength() || location
< 0 || location
> SAL_MAX_INT32
)
127 throw IllegalArgumentException();
128 m_nPos
= (sal_Int32
) location
;
131 sal_Int64 SAL_CALL
SequenceInputStream::getPosition() throw (IOException
, RuntimeException
, std::exception
)
136 sal_Int64 SAL_CALL
SequenceInputStream::getLength( ) throw (IOException
, RuntimeException
, std::exception
)
138 return m_aData
.getLength();
142 OSequenceOutputStream::OSequenceOutputStream(Sequence
< sal_Int8
>& _rSeq
, double _nResizeFactor
, sal_Int32 _nMinimumResize
, sal_Int32 _nMaximumResize
)
144 ,m_nResizeFactor(_nResizeFactor
)
145 ,m_nMinimumResize(_nMinimumResize
)
146 ,m_nMaximumResize(_nMaximumResize
)
147 ,m_nSize(0) // starting at position 0
150 OSL_ENSURE(m_nResizeFactor
> 1, "OSequenceOutputStream::OSequenceOutputStream : invalid resize factor !");
151 OSL_ENSURE((m_nMaximumResize
< 0) || (m_nMaximumResize
> m_nMinimumResize
),
152 "OSequenceOutputStream::OSequenceOutputStream : these limits don't make any sense !");
154 if (m_nResizeFactor
<= 1)
155 m_nResizeFactor
= 1.3;
156 if ((m_nMaximumResize
>= 0) && (m_nMaximumResize
<= m_nMinimumResize
))
157 m_nMaximumResize
= m_nMinimumResize
* 2;
158 // this heuristic is as good as any other ... supply better parameters if you don't like it :)
162 void SAL_CALL
OSequenceOutputStream::writeBytes( const Sequence
< sal_Int8
>& _rData
) throw(NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
, std::exception
)
164 MutexGuard
aGuard(m_aMutex
);
166 throw NotConnectedException();
168 // ensure the sequence has enough space left
169 if (m_nSize
+ _rData
.getLength() > m_rSequence
.getLength())
171 sal_Int32 nCurrentLength
= m_rSequence
.getLength();
172 sal_Int32 nNewLength
= static_cast< sal_Int32
>(
173 nCurrentLength
* m_nResizeFactor
);
175 if (m_nMinimumResize
> nNewLength
- nCurrentLength
)
176 // we have a minimum so it's not too inefficient for small sequences and small write requests
177 nNewLength
= nCurrentLength
+ m_nMinimumResize
;
179 if ((m_nMaximumResize
> 0) && (nNewLength
- nCurrentLength
> m_nMaximumResize
))
180 // such a large step is not allowed
181 nNewLength
= nCurrentLength
+ m_nMaximumResize
;
183 if (nNewLength
< m_nSize
+ _rData
.getLength())
184 { // it's not enough .... the data would not fit
186 // let's take the double amount of the length of the data to be written, as the next write
187 // request could be as large as this one
188 sal_Int32 nNewGrowth
= _rData
.getLength() * 2;
189 if ((m_nMaximumResize
> 0) && (nNewGrowth
> m_nMaximumResize
))
190 { // we came to the limit, again ...
191 nNewGrowth
= m_nMaximumResize
;
192 if (nNewGrowth
+ nCurrentLength
< m_nSize
+ _rData
.getLength())
193 // but it would not fit if we respect the limit
194 nNewGrowth
= m_nSize
+ _rData
.getLength() - nCurrentLength
;
196 nNewLength
= nCurrentLength
+ nNewGrowth
;
199 // round it off to the next multiple of 4 ...
200 nNewLength
= (nNewLength
+ 3) / 4 * 4;
202 m_rSequence
.realloc(nNewLength
);
205 OSL_ENSURE(m_rSequence
.getLength() >= m_nSize
+ _rData
.getLength(),
206 "ooops ... the realloc algorithm seems to be wrong :( !");
208 memcpy(m_rSequence
.getArray() + m_nSize
, _rData
.getConstArray(), _rData
.getLength());
209 m_nSize
+= _rData
.getLength();
213 void SAL_CALL
OSequenceOutputStream::flush( ) throw(NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
, std::exception
)
215 MutexGuard
aGuard(m_aMutex
);
217 throw NotConnectedException();
219 // cut the sequence to the real size
220 m_rSequence
.realloc(m_nSize
);
224 void SAL_CALL
OSequenceOutputStream::closeOutput( ) throw(NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
, std::exception
)
226 MutexGuard
aGuard(m_aMutex
);
228 throw NotConnectedException();
230 // cut the sequence to the real size
231 m_rSequence
.realloc(m_nSize
);
232 // and don't allow any further accesses
233 m_bConnected
= false;
236 } // namespace comphelper
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */