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