fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / xmlhelp / source / cxxhelp / provider / bufferedinputstream.cxx
blob7db8eccb6f2e060caf57ccf8bb90507709ebfef0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <string.h>
22 #include "bufferedinputstream.hxx"
25 using namespace cppu;
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() )
35 return xInputStream;
37 Reference<XSeekable> xSeekable(xInputStream,UNO_QUERY);
39 if( xSeekable.is() )
40 return xInputStream;
42 return new BufferedInputStream(xInputStream);
47 BufferedInputStream::BufferedInputStream(const Reference<XInputStream>& xInputStream)
48 : m_nBufferLocation(0),
49 m_nBufferSize(0),
50 m_pBuffer(new sal_Int8[1]) // Initialize with one to avoid gcc compiler warnings
52 try
54 sal_Int32 num;
55 sal_Int8 *tmp;
56 Sequence< sal_Int8 > aData(4096);
57 do{
58 num = xInputStream->readBytes(aData,4096);
59 if( num > 0 )
61 tmp = m_pBuffer;
62 m_pBuffer = new sal_Int8[m_nBufferSize+num];
63 memcpy((void *)(m_pBuffer),
64 (void *)(tmp),
65 sal_uInt32(m_nBufferSize));
66 memcpy((void *)(m_pBuffer+m_nBufferSize),
67 (void *)(aData.getArray()),
68 sal_uInt32(num));
69 m_nBufferSize += num;
70 delete[] tmp;
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()
92 delete[] m_pBuffer;
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,
122 IOException,
123 RuntimeException)
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),
138 nBytesToRead);
140 return nBytesToRead;
144 sal_Int32 SAL_CALL BufferedInputStream::readSomeBytes(
145 Sequence< sal_Int8 >& aData,sal_Int32 nMaxBytesToRead )
146 throw( NotConnectedException,
147 BufferSizeExceededException,
148 IOException,
149 RuntimeException)
151 return readBytes(aData,nMaxBytesToRead);
156 void SAL_CALL BufferedInputStream::skipBytes( sal_Int32 nBytesToSkip )
157 throw( NotConnectedException,
158 BufferSizeExceededException,
159 IOException,
160 RuntimeException )
164 seek(m_nBufferLocation+nBytesToSkip);
166 catch( const IllegalArgumentException& )
168 throw BufferSizeExceededException();
174 sal_Int32 SAL_CALL BufferedInputStream::available( void )
175 throw( NotConnectedException,
176 IOException,
177 RuntimeException )
179 osl::MutexGuard aGuard( m_aMutex );
180 return m_nBufferSize-m_nBufferLocation;
185 void SAL_CALL BufferedInputStream::closeInput( void )
186 throw( NotConnectedException,
187 IOException,
188 RuntimeException )
193 void SAL_CALL BufferedInputStream::seek( sal_Int64 location )
194 throw( IllegalArgumentException,
195 IOException,
196 RuntimeException )
198 if( 0 <= location && location < m_nBufferSize )
200 osl::MutexGuard aGuard( m_aMutex );
201 m_nBufferLocation = sal::static_int_cast<sal_Int32>( location );
203 else
204 throw IllegalArgumentException();
209 sal_Int64 SAL_CALL BufferedInputStream::getPosition( void )
210 throw( IOException,
211 RuntimeException )
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: */