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 .
22 #include "bufferedinputstream.hxx"
26 using namespace com::sun::star::uno
;
27 using namespace com::sun::star::lang
;
28 using namespace com::sun::star::io
;
29 using namespace chelp
;
32 Reference
<XInputStream
> chelp::turnToSeekable(const Reference
<XInputStream
>& xInputStream
)
34 if( ! xInputStream
.is() )
37 Reference
<XSeekable
> xSeekable(xInputStream
,UNO_QUERY
);
42 return new BufferedInputStream(xInputStream
);
47 BufferedInputStream::BufferedInputStream(const Reference
<XInputStream
>& xInputStream
)
48 : m_nBufferLocation(0),
50 m_pBuffer(new sal_Int8
[1]) // Initialize with one to avoid gcc compiler warnings
56 Sequence
< sal_Int8
> aData(4096);
58 num
= xInputStream
->readBytes(aData
,4096);
62 m_pBuffer
= new sal_Int8
[m_nBufferSize
+num
];
63 memcpy((void *)(m_pBuffer
),
65 sal_uInt32(m_nBufferSize
));
66 memcpy((void *)(m_pBuffer
+m_nBufferSize
),
67 (void *)(aData
.getArray()),
72 } while( num
== 4096 );
74 catch( const NotConnectedException
&)
77 catch( const BufferSizeExceededException
&)
80 catch( const IOException
&)
83 catch( const RuntimeException
&)
86 xInputStream
->closeInput();
90 BufferedInputStream::~BufferedInputStream()
96 Any SAL_CALL
BufferedInputStream::queryInterface( const Type
& rType
) throw( RuntimeException
)
98 Any aRet
= ::cppu::queryInterface( rType
,
99 (static_cast< XInputStream
* >(this)),
100 (static_cast< XSeekable
* >(this)) );
102 return aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
);
106 void SAL_CALL
BufferedInputStream::acquire( void ) throw()
108 OWeakObject::acquire();
112 void SAL_CALL
BufferedInputStream::release( void ) throw()
114 OWeakObject::release();
119 sal_Int32 SAL_CALL
BufferedInputStream::readBytes( Sequence
< sal_Int8
>& aData
,sal_Int32 nBytesToRead
)
120 throw( NotConnectedException
,
121 BufferSizeExceededException
,
125 osl::MutexGuard
aGuard( m_aMutex
);
127 if( 0 > nBytesToRead
)
128 throw BufferSizeExceededException();
130 if( m_nBufferLocation
+ nBytesToRead
> m_nBufferSize
)
131 nBytesToRead
= m_nBufferSize
- m_nBufferLocation
;
133 if( aData
.getLength() < nBytesToRead
)
134 aData
.realloc(nBytesToRead
);
136 memcpy((void*)(aData
.getArray()),
137 (void*)(m_pBuffer
+m_nBufferLocation
),
144 sal_Int32 SAL_CALL
BufferedInputStream::readSomeBytes(
145 Sequence
< sal_Int8
>& aData
,sal_Int32 nMaxBytesToRead
)
146 throw( NotConnectedException
,
147 BufferSizeExceededException
,
151 return readBytes(aData
,nMaxBytesToRead
);
156 void SAL_CALL
BufferedInputStream::skipBytes( sal_Int32 nBytesToSkip
)
157 throw( NotConnectedException
,
158 BufferSizeExceededException
,
164 seek(m_nBufferLocation
+nBytesToSkip
);
166 catch( const IllegalArgumentException
& )
168 throw BufferSizeExceededException();
174 sal_Int32 SAL_CALL
BufferedInputStream::available( void )
175 throw( NotConnectedException
,
179 osl::MutexGuard
aGuard( m_aMutex
);
180 return m_nBufferSize
-m_nBufferLocation
;
185 void SAL_CALL
BufferedInputStream::closeInput( void )
186 throw( NotConnectedException
,
193 void SAL_CALL
BufferedInputStream::seek( sal_Int64 location
)
194 throw( IllegalArgumentException
,
198 if( 0 <= location
&& location
< m_nBufferSize
)
200 osl::MutexGuard
aGuard( m_aMutex
);
201 m_nBufferLocation
= sal::static_int_cast
<sal_Int32
>( location
);
204 throw IllegalArgumentException();
209 sal_Int64 SAL_CALL
BufferedInputStream::getPosition( void )
213 osl::MutexGuard
aGuard( m_aMutex
);
214 return m_nBufferLocation
;
219 sal_Int64 SAL_CALL
BufferedInputStream::getLength( void ) throw( IOException
,RuntimeException
)
221 osl::MutexGuard
aGuard( m_aMutex
);
222 return m_nBufferSize
;
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */