merge the formfield patch from ooo-build
[ooovba.git] / xmlhelp / source / cxxhelp / provider / bufferedinputstream.cxx
blob53489d279cfa333e73de999fec4bbe83dc3c2dfd
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: bufferedinputstream.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_xmlhelp.hxx"
34 #include <rtl/memory.h>
35 #include "bufferedinputstream.hxx"
38 using namespace cppu;
39 using namespace com::sun::star::uno;
40 using namespace com::sun::star::lang;
41 using namespace com::sun::star::io;
42 using namespace chelp;
45 Reference<XInputStream> chelp::turnToSeekable(const Reference<XInputStream>& xInputStream)
47 if( ! xInputStream.is() )
48 return xInputStream;
50 Reference<XSeekable> xSeekable(xInputStream,UNO_QUERY);
52 if( xSeekable.is() )
53 return xInputStream;
55 return new BufferedInputStream(xInputStream);
60 BufferedInputStream::BufferedInputStream(const Reference<XInputStream>& xInputStream)
61 : m_nBufferLocation(0),
62 m_nBufferSize(0),
63 m_pBuffer(new sal_Int8[1]) // Initialize with one to avoid gcc compiler warnings
65 try
67 sal_Int32 num;
68 sal_Int8 *tmp;
69 Sequence< sal_Int8 > aData(4096);
70 do{
71 num = xInputStream->readBytes(aData,4096);
72 if( num > 0 )
74 tmp = m_pBuffer;
75 m_pBuffer = new sal_Int8[m_nBufferSize+num];
76 rtl_copyMemory((void *)(m_pBuffer),
77 (void *)(tmp),
78 sal_uInt32(m_nBufferSize));
79 rtl_copyMemory((void *)(m_pBuffer+m_nBufferSize),
80 (void *)(aData.getArray()),
81 sal_uInt32(num));
82 m_nBufferSize += num;
83 delete[] tmp;
85 } while( num == 4096 );
87 catch( const NotConnectedException&)
90 catch( const BufferSizeExceededException&)
93 catch( const IOException&)
96 catch( const RuntimeException&)
99 xInputStream->closeInput();
103 BufferedInputStream::~BufferedInputStream()
105 delete[] m_pBuffer;
109 Any SAL_CALL BufferedInputStream::queryInterface( const Type& rType ) throw( RuntimeException )
111 Any aRet = ::cppu::queryInterface( rType,
112 SAL_STATIC_CAST( XInputStream*,this ),
113 SAL_STATIC_CAST( XSeekable*,this ) );
115 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
119 void SAL_CALL BufferedInputStream::acquire( void ) throw()
121 OWeakObject::acquire();
125 void SAL_CALL BufferedInputStream::release( void ) throw()
127 OWeakObject::release();
132 sal_Int32 SAL_CALL BufferedInputStream::readBytes( Sequence< sal_Int8 >& aData,sal_Int32 nBytesToRead )
133 throw( NotConnectedException,
134 BufferSizeExceededException,
135 IOException,
136 RuntimeException)
138 osl::MutexGuard aGuard( m_aMutex );
140 if( 0 > nBytesToRead )
141 throw BufferSizeExceededException();
143 if( m_nBufferLocation + nBytesToRead > m_nBufferSize )
144 nBytesToRead = m_nBufferSize - m_nBufferLocation;
146 if( aData.getLength() < nBytesToRead )
147 aData.realloc(nBytesToRead);
149 rtl_copyMemory((void*)(aData.getArray()),
150 (void*)(m_pBuffer+m_nBufferLocation),
151 nBytesToRead);
153 return nBytesToRead;
157 sal_Int32 SAL_CALL BufferedInputStream::readSomeBytes(
158 Sequence< sal_Int8 >& aData,sal_Int32 nMaxBytesToRead )
159 throw( NotConnectedException,
160 BufferSizeExceededException,
161 IOException,
162 RuntimeException)
164 return readBytes(aData,nMaxBytesToRead);
169 void SAL_CALL BufferedInputStream::skipBytes( sal_Int32 nBytesToSkip )
170 throw( NotConnectedException,
171 BufferSizeExceededException,
172 IOException,
173 RuntimeException )
177 seek(m_nBufferLocation+nBytesToSkip);
179 catch( const IllegalArgumentException& )
181 throw BufferSizeExceededException();
187 sal_Int32 SAL_CALL BufferedInputStream::available( void )
188 throw( NotConnectedException,
189 IOException,
190 RuntimeException )
192 osl::MutexGuard aGuard( m_aMutex );
193 return m_nBufferSize-m_nBufferLocation;
198 void SAL_CALL BufferedInputStream::closeInput( void )
199 throw( NotConnectedException,
200 IOException,
201 RuntimeException )
206 void SAL_CALL BufferedInputStream::seek( sal_Int64 location )
207 throw( IllegalArgumentException,
208 IOException,
209 RuntimeException )
211 if( 0 <= location && location < m_nBufferSize )
213 osl::MutexGuard aGuard( m_aMutex );
214 m_nBufferLocation = sal::static_int_cast<sal_Int32>( location );
216 else
217 throw IllegalArgumentException();
222 sal_Int64 SAL_CALL BufferedInputStream::getPosition( void )
223 throw( IOException,
224 RuntimeException )
226 osl::MutexGuard aGuard( m_aMutex );
227 return m_nBufferLocation;
232 sal_Int64 SAL_CALL BufferedInputStream::getLength( void ) throw( IOException,RuntimeException )
234 osl::MutexGuard aGuard( m_aMutex );
235 return m_nBufferSize;