Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <string.h>
30 : :
31 : : #include <rtl/memory.h>
32 : : #include "bufferedinputstream.hxx"
33 : :
34 : :
35 : : using namespace cppu;
36 : : using namespace com::sun::star::uno;
37 : : using namespace com::sun::star::lang;
38 : : using namespace com::sun::star::io;
39 : : using namespace chelp;
40 : :
41 : :
42 : 0 : Reference<XInputStream> chelp::turnToSeekable(const Reference<XInputStream>& xInputStream)
43 : : {
44 [ # # ]: 0 : if( ! xInputStream.is() )
45 : 0 : return xInputStream;
46 : :
47 [ # # ]: 0 : Reference<XSeekable> xSeekable(xInputStream,UNO_QUERY);
48 : :
49 [ # # ]: 0 : if( xSeekable.is() )
50 : 0 : return xInputStream;
51 : :
52 [ # # ][ # # ]: 0 : return new BufferedInputStream(xInputStream);
[ # # ]
53 : : }
54 : :
55 : :
56 : :
57 : 0 : BufferedInputStream::BufferedInputStream(const Reference<XInputStream>& xInputStream)
58 : : : m_nBufferLocation(0),
59 : : m_nBufferSize(0),
60 [ # # ][ # # ]: 0 : m_pBuffer(new sal_Int8[1]) // Initialize with one to avoid gcc compiler warnings
61 : : {
62 : : try
63 : : {
64 : : sal_Int32 num;
65 : : sal_Int8 *tmp;
66 [ # # ]: 0 : Sequence< sal_Int8 > aData(4096);
67 [ # # ]: 0 : do{
68 [ # # ][ # # ]: 0 : num = xInputStream->readBytes(aData,4096);
69 [ # # ]: 0 : if( num > 0 )
70 : : {
71 : 0 : tmp = m_pBuffer;
72 [ # # ]: 0 : m_pBuffer = new sal_Int8[m_nBufferSize+num];
73 : : memcpy((void *)(m_pBuffer),
74 : : (void *)(tmp),
75 : 0 : sal_uInt32(m_nBufferSize));
76 : 0 : memcpy((void *)(m_pBuffer+m_nBufferSize),
77 [ # # ]: 0 : (void *)(aData.getArray()),
78 : 0 : sal_uInt32(num));
79 : 0 : m_nBufferSize += num;
80 [ # # ]: 0 : delete[] tmp;
81 : : }
82 [ # # ][ # # : 0 : } while( num == 4096 );
# # # ]
83 : : }
84 [ # # ]: 0 : catch( const NotConnectedException&)
85 : : {
86 : : }
87 [ # # ]: 0 : catch( const BufferSizeExceededException&)
88 : : {
89 : : }
90 [ # # ]: 0 : catch( const IOException&)
91 : : {
92 : : }
93 [ # # ]: 0 : catch( const RuntimeException&)
94 : : {
95 : : }
96 [ # # ][ # # ]: 0 : xInputStream->closeInput();
97 : 0 : }
98 : :
99 : :
100 [ # # ]: 0 : BufferedInputStream::~BufferedInputStream()
101 : : {
102 [ # # ]: 0 : delete[] m_pBuffer;
103 [ # # ]: 0 : }
104 : :
105 : :
106 : 0 : Any SAL_CALL BufferedInputStream::queryInterface( const Type& rType ) throw( RuntimeException )
107 : : {
108 : : Any aRet = ::cppu::queryInterface( rType,
109 : : (static_cast< XInputStream* >(this)),
110 [ # # ]: 0 : (static_cast< XSeekable* >(this)) );
111 : :
112 [ # # ][ # # ]: 0 : return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
113 : : }
114 : :
115 : :
116 : 0 : void SAL_CALL BufferedInputStream::acquire( void ) throw()
117 : : {
118 : 0 : OWeakObject::acquire();
119 : 0 : }
120 : :
121 : :
122 : 0 : void SAL_CALL BufferedInputStream::release( void ) throw()
123 : : {
124 : 0 : OWeakObject::release();
125 : 0 : }
126 : :
127 : :
128 : :
129 : 0 : sal_Int32 SAL_CALL BufferedInputStream::readBytes( Sequence< sal_Int8 >& aData,sal_Int32 nBytesToRead )
130 : : throw( NotConnectedException,
131 : : BufferSizeExceededException,
132 : : IOException,
133 : : RuntimeException)
134 : : {
135 [ # # ]: 0 : osl::MutexGuard aGuard( m_aMutex );
136 : :
137 [ # # ]: 0 : if( 0 > nBytesToRead )
138 [ # # ]: 0 : throw BufferSizeExceededException();
139 : :
140 [ # # ]: 0 : if( m_nBufferLocation + nBytesToRead > m_nBufferSize )
141 : 0 : nBytesToRead = m_nBufferSize - m_nBufferLocation;
142 : :
143 [ # # ]: 0 : if( aData.getLength() < nBytesToRead )
144 [ # # ]: 0 : aData.realloc(nBytesToRead);
145 : :
146 [ # # ]: 0 : memcpy((void*)(aData.getArray()),
147 : : (void*)(m_pBuffer+m_nBufferLocation),
148 : 0 : nBytesToRead);
149 : :
150 [ # # ]: 0 : return nBytesToRead;
151 : : }
152 : :
153 : :
154 : 0 : sal_Int32 SAL_CALL BufferedInputStream::readSomeBytes(
155 : : Sequence< sal_Int8 >& aData,sal_Int32 nMaxBytesToRead )
156 : : throw( NotConnectedException,
157 : : BufferSizeExceededException,
158 : : IOException,
159 : : RuntimeException)
160 : : {
161 : 0 : return readBytes(aData,nMaxBytesToRead);
162 : : }
163 : :
164 : :
165 : :
166 : 0 : void SAL_CALL BufferedInputStream::skipBytes( sal_Int32 nBytesToSkip )
167 : : throw( NotConnectedException,
168 : : BufferSizeExceededException,
169 : : IOException,
170 : : RuntimeException )
171 : : {
172 : : try
173 : : {
174 [ # # ]: 0 : seek(m_nBufferLocation+nBytesToSkip);
175 : : }
176 [ # # ]: 0 : catch( const IllegalArgumentException& )
177 : : {
178 [ # # ]: 0 : throw BufferSizeExceededException();
179 : : }
180 : 0 : }
181 : :
182 : :
183 : :
184 : 0 : sal_Int32 SAL_CALL BufferedInputStream::available( void )
185 : : throw( NotConnectedException,
186 : : IOException,
187 : : RuntimeException )
188 : : {
189 [ # # ]: 0 : osl::MutexGuard aGuard( m_aMutex );
190 [ # # ]: 0 : return m_nBufferSize-m_nBufferLocation;
191 : : }
192 : :
193 : :
194 : :
195 : 0 : void SAL_CALL BufferedInputStream::closeInput( void )
196 : : throw( NotConnectedException,
197 : : IOException,
198 : : RuntimeException )
199 : : {
200 : 0 : }
201 : :
202 : :
203 : 0 : void SAL_CALL BufferedInputStream::seek( sal_Int64 location )
204 : : throw( IllegalArgumentException,
205 : : IOException,
206 : : RuntimeException )
207 : : {
208 [ # # ][ # # ]: 0 : if( 0 <= location && location < m_nBufferSize )
209 : : {
210 [ # # ]: 0 : osl::MutexGuard aGuard( m_aMutex );
211 [ # # ]: 0 : m_nBufferLocation = sal::static_int_cast<sal_Int32>( location );
212 : : }
213 : : else
214 [ # # ]: 0 : throw IllegalArgumentException();
215 : 0 : }
216 : :
217 : :
218 : :
219 : 0 : sal_Int64 SAL_CALL BufferedInputStream::getPosition( void )
220 : : throw( IOException,
221 : : RuntimeException )
222 : : {
223 [ # # ]: 0 : osl::MutexGuard aGuard( m_aMutex );
224 [ # # ]: 0 : return m_nBufferLocation;
225 : : }
226 : :
227 : :
228 : :
229 : 0 : sal_Int64 SAL_CALL BufferedInputStream::getLength( void ) throw( IOException,RuntimeException )
230 : : {
231 [ # # ]: 0 : osl::MutexGuard aGuard( m_aMutex );
232 [ # # ]: 0 : return m_nBufferSize;
233 : : }
234 : :
235 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|