Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <string.h>
21 :
22 : #include "bufferedinputstream.hxx"
23 :
24 :
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;
30 :
31 :
32 0 : Reference<XInputStream> chelp::turnToSeekable(const Reference<XInputStream>& xInputStream)
33 : {
34 0 : if( ! xInputStream.is() )
35 0 : return xInputStream;
36 :
37 0 : Reference<XSeekable> xSeekable(xInputStream,UNO_QUERY);
38 :
39 0 : if( xSeekable.is() )
40 0 : return xInputStream;
41 :
42 0 : return new BufferedInputStream(xInputStream);
43 : }
44 :
45 :
46 :
47 0 : BufferedInputStream::BufferedInputStream(const Reference<XInputStream>& xInputStream)
48 : : m_nBufferLocation(0),
49 : m_nBufferSize(0),
50 0 : m_pBuffer(new sal_Int8[1]) // Initialize with one to avoid gcc compiler warnings
51 : {
52 : try
53 : {
54 : sal_Int32 num;
55 : sal_Int8 *tmp;
56 0 : Sequence< sal_Int8 > aData(4096);
57 0 : do{
58 0 : num = xInputStream->readBytes(aData,4096);
59 0 : if( num > 0 )
60 : {
61 0 : tmp = m_pBuffer;
62 0 : m_pBuffer = new sal_Int8[m_nBufferSize+num];
63 : memcpy((void *)(m_pBuffer),
64 : (void *)(tmp),
65 0 : sal_uInt32(m_nBufferSize));
66 0 : memcpy((void *)(m_pBuffer+m_nBufferSize),
67 0 : (void *)(aData.getArray()),
68 0 : sal_uInt32(num));
69 0 : m_nBufferSize += num;
70 0 : delete[] tmp;
71 : }
72 0 : } while( num == 4096 );
73 : }
74 0 : catch( const NotConnectedException&)
75 : {
76 : }
77 0 : catch( const BufferSizeExceededException&)
78 : {
79 : }
80 0 : catch( const IOException&)
81 : {
82 : }
83 0 : catch( const RuntimeException&)
84 : {
85 : }
86 0 : xInputStream->closeInput();
87 0 : }
88 :
89 :
90 0 : BufferedInputStream::~BufferedInputStream()
91 : {
92 0 : delete[] m_pBuffer;
93 0 : }
94 :
95 :
96 0 : Any SAL_CALL BufferedInputStream::queryInterface( const Type& rType ) throw( RuntimeException, std::exception )
97 : {
98 : Any aRet = ::cppu::queryInterface( rType,
99 : (static_cast< XInputStream* >(this)),
100 0 : (static_cast< XSeekable* >(this)) );
101 :
102 0 : return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
103 : }
104 :
105 :
106 0 : void SAL_CALL BufferedInputStream::acquire( void ) throw()
107 : {
108 0 : OWeakObject::acquire();
109 0 : }
110 :
111 :
112 0 : void SAL_CALL BufferedInputStream::release( void ) throw()
113 : {
114 0 : OWeakObject::release();
115 0 : }
116 :
117 :
118 :
119 0 : sal_Int32 SAL_CALL BufferedInputStream::readBytes( Sequence< sal_Int8 >& aData,sal_Int32 nBytesToRead )
120 : throw( NotConnectedException,
121 : BufferSizeExceededException,
122 : IOException,
123 : RuntimeException, std::exception)
124 : {
125 0 : osl::MutexGuard aGuard( m_aMutex );
126 :
127 0 : if( 0 > nBytesToRead )
128 0 : throw BufferSizeExceededException();
129 :
130 0 : if( m_nBufferLocation + nBytesToRead > m_nBufferSize )
131 0 : nBytesToRead = m_nBufferSize - m_nBufferLocation;
132 :
133 0 : if( aData.getLength() < nBytesToRead )
134 0 : aData.realloc(nBytesToRead);
135 :
136 0 : memcpy((void*)(aData.getArray()),
137 : (void*)(m_pBuffer+m_nBufferLocation),
138 0 : nBytesToRead);
139 :
140 0 : return nBytesToRead;
141 : }
142 :
143 :
144 0 : sal_Int32 SAL_CALL BufferedInputStream::readSomeBytes(
145 : Sequence< sal_Int8 >& aData,sal_Int32 nMaxBytesToRead )
146 : throw( NotConnectedException,
147 : BufferSizeExceededException,
148 : IOException,
149 : RuntimeException, std::exception)
150 : {
151 0 : return readBytes(aData,nMaxBytesToRead);
152 : }
153 :
154 :
155 :
156 0 : void SAL_CALL BufferedInputStream::skipBytes( sal_Int32 nBytesToSkip )
157 : throw( NotConnectedException,
158 : BufferSizeExceededException,
159 : IOException,
160 : RuntimeException, std::exception )
161 : {
162 : try
163 : {
164 0 : seek(m_nBufferLocation+nBytesToSkip);
165 : }
166 0 : catch( const IllegalArgumentException& )
167 : {
168 0 : throw BufferSizeExceededException();
169 : }
170 0 : }
171 :
172 :
173 :
174 0 : sal_Int32 SAL_CALL BufferedInputStream::available( void )
175 : throw( NotConnectedException,
176 : IOException,
177 : RuntimeException, std::exception )
178 : {
179 0 : osl::MutexGuard aGuard( m_aMutex );
180 0 : return m_nBufferSize-m_nBufferLocation;
181 : }
182 :
183 :
184 :
185 0 : void SAL_CALL BufferedInputStream::closeInput( void )
186 : throw( NotConnectedException,
187 : IOException,
188 : RuntimeException, std::exception )
189 : {
190 0 : }
191 :
192 :
193 0 : void SAL_CALL BufferedInputStream::seek( sal_Int64 location )
194 : throw( IllegalArgumentException,
195 : IOException,
196 : RuntimeException, std::exception )
197 : {
198 0 : if( 0 <= location && location < m_nBufferSize )
199 : {
200 0 : osl::MutexGuard aGuard( m_aMutex );
201 0 : m_nBufferLocation = sal::static_int_cast<sal_Int32>( location );
202 : }
203 : else
204 0 : throw IllegalArgumentException();
205 0 : }
206 :
207 :
208 :
209 0 : sal_Int64 SAL_CALL BufferedInputStream::getPosition( void )
210 : throw( IOException,
211 : RuntimeException, std::exception )
212 : {
213 0 : osl::MutexGuard aGuard( m_aMutex );
214 0 : return m_nBufferLocation;
215 : }
216 :
217 :
218 :
219 0 : sal_Int64 SAL_CALL BufferedInputStream::getLength( void ) throw( IOException,RuntimeException, std::exception )
220 : {
221 0 : osl::MutexGuard aGuard( m_aMutex );
222 0 : return m_nBufferSize;
223 : }
224 :
225 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|