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