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