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 <algorithm>
21 :
22 : #include "comphelper_module.hxx"
23 : #include "comphelper_services.hxx"
24 :
25 : #include <com/sun/star/io/XStream.hpp>
26 : #include <com/sun/star/io/XSeekableInputStream.hpp>
27 : #include <com/sun/star/io/XTruncate.hpp>
28 : #include <com/sun/star/uno/XComponentContext.hpp>
29 : #include <cppuhelper/implbase4.hxx>
30 : #include <osl/diagnose.h>
31 :
32 : #include <string.h>
33 : #include <vector>
34 :
35 : using ::cppu::OWeakObject;
36 : using ::cppu::WeakImplHelper4;
37 : using namespace ::com::sun::star::io;
38 : using namespace ::com::sun::star::uno;
39 : using namespace ::com::sun::star::lang;
40 : using namespace ::osl;
41 :
42 : namespace comphelper
43 : {
44 :
45 : class UNOMemoryStream : public WeakImplHelper4 < XStream, XSeekableInputStream, XOutputStream, XTruncate >
46 : {
47 : public:
48 : UNOMemoryStream();
49 : virtual ~UNOMemoryStream();
50 :
51 : // XStream
52 : virtual Reference< XInputStream > SAL_CALL getInputStream( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
53 : virtual Reference< XOutputStream > SAL_CALL getOutputStream( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
54 :
55 : // XInputStream
56 : virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
57 : virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
58 : virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
59 : virtual sal_Int32 SAL_CALL available() throw (NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
60 : virtual void SAL_CALL closeInput() throw (NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
61 :
62 : // XSeekable
63 : virtual void SAL_CALL seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
64 : virtual sal_Int64 SAL_CALL getPosition() throw (IOException, RuntimeException, std::exception) SAL_OVERRIDE;
65 : virtual sal_Int64 SAL_CALL getLength() throw (IOException, RuntimeException, std::exception) SAL_OVERRIDE;
66 :
67 : // XOutputStream
68 : virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
69 : virtual void SAL_CALL flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
70 : virtual void SAL_CALL closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
71 :
72 : // XTruncate
73 : virtual void SAL_CALL truncate() throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
74 :
75 : // XServiceInfo - static versions (used for component registration)
76 : static OUString SAL_CALL getImplementationName_static();
77 : static Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
78 : static Reference< XInterface > SAL_CALL Create( const Reference< ::com::sun::star::uno::XComponentContext >& );
79 :
80 : private:
81 : std::vector< sal_Int8 > maData;
82 : sal_Int32 mnCursor;
83 : };
84 :
85 27781 : UNOMemoryStream::UNOMemoryStream()
86 27781 : : mnCursor(0)
87 : {
88 27781 : }
89 :
90 55560 : UNOMemoryStream::~UNOMemoryStream()
91 : {
92 55560 : }
93 :
94 : // XStream
95 60288 : Reference< XInputStream > SAL_CALL UNOMemoryStream::getInputStream( ) throw (RuntimeException, std::exception)
96 : {
97 60288 : return this;
98 : }
99 :
100 42442 : Reference< XOutputStream > SAL_CALL UNOMemoryStream::getOutputStream( ) throw (RuntimeException, std::exception)
101 : {
102 42442 : return this;
103 : }
104 :
105 : // XInputStream
106 47012 : sal_Int32 SAL_CALL UNOMemoryStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
107 : {
108 47012 : if( nBytesToRead < 0 )
109 0 : throw IOException();
110 :
111 47012 : nBytesToRead = std::min( nBytesToRead, available() );
112 47012 : aData.realloc( nBytesToRead );
113 :
114 47012 : if( nBytesToRead )
115 : {
116 30959 : sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin()));
117 30959 : sal_Int8* pCursor = &((pData)[mnCursor]);
118 30959 : memcpy( static_cast<void*>(aData.getArray()), static_cast<void*>(pCursor), nBytesToRead );
119 :
120 30959 : mnCursor += nBytesToRead;
121 : }
122 :
123 47012 : return nBytesToRead;
124 : }
125 :
126 22808 : sal_Int32 SAL_CALL UNOMemoryStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
127 : {
128 22808 : return readBytes( aData, nMaxBytesToRead );
129 : }
130 :
131 0 : void SAL_CALL UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
132 : {
133 0 : if( nBytesToSkip < 0 )
134 0 : throw IOException();
135 :
136 0 : mnCursor += std::min( nBytesToSkip, available() );
137 0 : }
138 :
139 57535 : sal_Int32 SAL_CALL UNOMemoryStream::available() throw (NotConnectedException, IOException, RuntimeException, std::exception)
140 : {
141 57535 : return static_cast< sal_Int32 >( maData.size() ) - mnCursor;
142 : }
143 :
144 53468 : void SAL_CALL UNOMemoryStream::closeInput() throw (NotConnectedException, IOException, RuntimeException, std::exception)
145 : {
146 53468 : mnCursor = 0;
147 53468 : }
148 :
149 : // XSeekable
150 41953 : void SAL_CALL UNOMemoryStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
151 : {
152 41953 : if( (location < 0) || (location > SAL_MAX_INT32) )
153 0 : throw IllegalArgumentException("this implementation does not support more than 2GB!", static_cast<OWeakObject*>(this), 0 );
154 :
155 : // seek operation should be able to resize the stream
156 41953 : if ( location > static_cast< sal_Int64 >( maData.size() ) )
157 13 : maData.resize( static_cast< sal_Int32 >( location ) );
158 :
159 41953 : if ( location > static_cast< sal_Int64 >( maData.size() ) )
160 0 : maData.resize( static_cast< sal_Int32 >( location ) );
161 :
162 41953 : mnCursor = static_cast< sal_Int32 >( location );
163 41953 : }
164 :
165 65846 : sal_Int64 SAL_CALL UNOMemoryStream::getPosition() throw (IOException, RuntimeException, std::exception)
166 : {
167 65846 : return static_cast< sal_Int64 >( mnCursor );
168 : }
169 :
170 24308 : sal_Int64 SAL_CALL UNOMemoryStream::getLength() throw (IOException, RuntimeException, std::exception)
171 : {
172 24308 : return static_cast< sal_Int64 >( maData.size() );
173 : }
174 :
175 : // XOutputStream
176 62884 : void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
177 : {
178 62884 : const sal_Int32 nBytesToWrite( aData.getLength() );
179 62884 : if( nBytesToWrite )
180 : {
181 62802 : sal_Int64 nNewSize = static_cast< sal_Int64 >( mnCursor + nBytesToWrite );
182 62802 : if( nNewSize > SAL_MAX_INT32 )
183 : {
184 : OSL_ASSERT(false);
185 0 : throw IOException("this implementation does not support more than 2GB!", static_cast<OWeakObject*>(this) );
186 : }
187 :
188 62802 : if( static_cast< sal_Int32 >( nNewSize ) > static_cast< sal_Int32 >( maData.size() ) )
189 62570 : maData.resize( static_cast< sal_Int32 >( nNewSize ) );
190 :
191 62802 : sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin()));
192 62802 : sal_Int8* pCursor = &(pData[mnCursor]);
193 62802 : memcpy( pCursor, aData.getConstArray(), nBytesToWrite );
194 :
195 62802 : mnCursor += nBytesToWrite;
196 : }
197 62884 : }
198 :
199 2180 : void SAL_CALL UNOMemoryStream::flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
200 : {
201 2180 : }
202 :
203 26261 : void SAL_CALL UNOMemoryStream::closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
204 : {
205 26261 : mnCursor = 0;
206 26261 : }
207 :
208 : //XTruncate
209 74 : void SAL_CALL UNOMemoryStream::truncate() throw (IOException, RuntimeException, std::exception)
210 : {
211 74 : maData.resize( 0 );
212 74 : mnCursor = 0;
213 74 : }
214 :
215 382 : OUString SAL_CALL UNOMemoryStream::getImplementationName_static()
216 : {
217 382 : return OUString("com.sun.star.comp.MemoryStream");
218 : }
219 :
220 191 : Sequence< OUString > SAL_CALL UNOMemoryStream::getSupportedServiceNames_static()
221 : {
222 191 : Sequence< OUString > aSeq(1);
223 191 : aSeq[0] = getImplementationName_static();
224 191 : return aSeq;
225 : }
226 :
227 27781 : Reference< XInterface > SAL_CALL UNOMemoryStream::Create(
228 : SAL_UNUSED_PARAMETER const Reference< XComponentContext >& )
229 : {
230 27781 : return static_cast<OWeakObject*>(new UNOMemoryStream());
231 : }
232 :
233 : } // namespace comphelper
234 :
235 191 : void createRegistryInfo_UNOMemoryStream()
236 : {
237 191 : static ::comphelper::module::OAutoRegistration< ::comphelper::UNOMemoryStream > aAutoRegistration;
238 191 : }
239 :
240 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|