Branch data 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 ::rtl::OUString;
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);
51 : : virtual Reference< XOutputStream > SAL_CALL getOutputStream( ) throw (RuntimeException);
52 : :
53 : : // XInputStream
54 : : virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
55 : : virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
56 : : virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
57 : : virtual sal_Int32 SAL_CALL available() throw (NotConnectedException, IOException, RuntimeException);
58 : : virtual void SAL_CALL closeInput() throw (NotConnectedException, IOException, RuntimeException);
59 : :
60 : : // XSeekable
61 : : virtual void SAL_CALL seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException);
62 : : virtual sal_Int64 SAL_CALL getPosition() throw (IOException, RuntimeException);
63 : : virtual sal_Int64 SAL_CALL getLength() throw (IOException, RuntimeException);
64 : :
65 : : // XOutputStream
66 : : virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
67 : : virtual void SAL_CALL flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
68 : : virtual void SAL_CALL closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
69 : :
70 : : // XTruncate
71 : : virtual void SAL_CALL truncate() throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
72 : :
73 : : // XServiceInfo - static versions (used for component registration)
74 : : static ::rtl::OUString SAL_CALL getImplementationName_static();
75 : : static Sequence< ::rtl::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 : 5473 : UNOMemoryStream::UNOMemoryStream()
84 [ + - ]: 5473 : : mnCursor(0)
85 : : {
86 : 5473 : }
87 : :
88 : 5035 : UNOMemoryStream::~UNOMemoryStream()
89 : : {
90 [ - + ]: 10070 : }
91 : :
92 : : // XStream
93 : 10954 : Reference< XInputStream > SAL_CALL UNOMemoryStream::getInputStream( ) throw (RuntimeException)
94 : : {
95 : 10954 : return this;
96 : : }
97 : :
98 : 6508 : Reference< XOutputStream > SAL_CALL UNOMemoryStream::getOutputStream( ) throw (RuntimeException)
99 : : {
100 : 6508 : return this;
101 : : }
102 : :
103 : : // XInputStream
104 : 4249 : sal_Int32 SAL_CALL UNOMemoryStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
105 : : {
106 [ - + ]: 4249 : if( nBytesToRead < 0 )
107 [ # # ]: 0 : throw IOException();
108 : :
109 [ + - ]: 4249 : nBytesToRead = std::min( nBytesToRead, available() );
110 : 4249 : aData.realloc( nBytesToRead );
111 : :
112 [ + + ]: 4249 : if( nBytesToRead )
113 : : {
114 [ + - ]: 2137 : sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin()));
115 : 2137 : sal_Int8* pCursor = &((pData)[mnCursor]);
116 : 2137 : memcpy( (void*)aData.getArray(), (void*)pCursor, nBytesToRead );
117 : :
118 : 2137 : mnCursor += nBytesToRead;
119 : : }
120 : :
121 : 4249 : return nBytesToRead;
122 : : }
123 : :
124 : 2877 : sal_Int32 SAL_CALL UNOMemoryStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
125 : : {
126 : 2877 : return readBytes( aData, nMaxBytesToRead );
127 : : }
128 : :
129 : 0 : void SAL_CALL UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
130 : : {
131 [ # # ]: 0 : if( nBytesToSkip < 0 )
132 [ # # ]: 0 : throw IOException();
133 : :
134 [ # # ]: 0 : mnCursor += std::min( nBytesToSkip, available() );
135 : 0 : }
136 : :
137 : 4249 : sal_Int32 SAL_CALL UNOMemoryStream::available() throw (NotConnectedException, IOException, RuntimeException)
138 : : {
139 : 4249 : return static_cast< sal_Int32 >( maData.size() ) - mnCursor;
140 : : }
141 : :
142 : 6830 : void SAL_CALL UNOMemoryStream::closeInput() throw (NotConnectedException, IOException, RuntimeException)
143 : : {
144 : 6830 : mnCursor = 0;
145 : 6830 : }
146 : :
147 : : // XSeekable
148 : 5654 : void SAL_CALL UNOMemoryStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException)
149 : : {
150 [ + - ][ - + ]: 5654 : if( (location < 0) || (location > SAL_MAX_INT32) )
151 [ # # ][ # # ]: 0 : throw IllegalArgumentException( OUString(RTL_CONSTASCII_USTRINGPARAM("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 [ - + ]: 5654 : if ( location > static_cast< sal_Int64 >( maData.size() ) )
155 : 0 : maData.resize( static_cast< sal_Int32 >( location ) );
156 : :
157 [ - + ]: 5654 : if ( location > static_cast< sal_Int64 >( maData.size() ) )
158 : 0 : maData.resize( static_cast< sal_Int32 >( location ) );
159 : :
160 : 5654 : mnCursor = static_cast< sal_Int32 >( location );
161 : 5654 : }
162 : :
163 : 76116 : sal_Int64 SAL_CALL UNOMemoryStream::getPosition() throw (IOException, RuntimeException)
164 : : {
165 : 76116 : return static_cast< sal_Int64 >( mnCursor );
166 : : }
167 : :
168 : 4386 : sal_Int64 SAL_CALL UNOMemoryStream::getLength() throw (IOException, RuntimeException)
169 : : {
170 : 4386 : return static_cast< sal_Int64 >( maData.size() );
171 : : }
172 : :
173 : : // XOutputStream
174 : 75045 : void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
175 : : {
176 : 75045 : const sal_Int32 nBytesToWrite( aData.getLength() );
177 [ + + ]: 75045 : if( nBytesToWrite )
178 : : {
179 : 74885 : sal_Int64 nNewSize = static_cast< sal_Int64 >( mnCursor + nBytesToWrite );
180 [ - + ]: 74885 : if( nNewSize > SAL_MAX_INT32 )
181 : : {
182 : : OSL_ASSERT(false);
183 [ # # ][ # # ]: 0 : throw IOException( OUString(RTL_CONSTASCII_USTRINGPARAM("this implementation does not support more than 2GB!")), Reference< XInterface >(static_cast<OWeakObject*>(this)) );
[ # # ]
184 : : }
185 : :
186 [ + + ]: 74885 : if( static_cast< sal_Int32 >( nNewSize ) > static_cast< sal_Int32 >( maData.size() ) )
187 : 74781 : maData.resize( static_cast< sal_Int32 >( nNewSize ) );
188 : :
189 [ + - ]: 74885 : sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin()));
190 : 74885 : sal_Int8* pCursor = &(pData[mnCursor]);
191 : 74885 : memcpy( (void*)pCursor, (void*)aData.getConstArray(), nBytesToWrite );
192 : :
193 : 74885 : mnCursor += nBytesToWrite;
194 : : }
195 : 75045 : }
196 : :
197 : 589 : void SAL_CALL UNOMemoryStream::flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
198 : : {
199 : 589 : }
200 : :
201 : 4921 : void SAL_CALL UNOMemoryStream::closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
202 : : {
203 : 4921 : mnCursor = 0;
204 : 4921 : }
205 : :
206 : : //XTruncate
207 : 31 : void SAL_CALL UNOMemoryStream::truncate() throw (IOException, RuntimeException)
208 : : {
209 : 31 : maData.resize( 0 );
210 : 31 : mnCursor = 0;
211 : 31 : }
212 : :
213 : 454 : ::rtl::OUString SAL_CALL UNOMemoryStream::getImplementationName_static()
214 : : {
215 : 454 : return ::rtl::OUString("com.sun.star.comp.MemoryStream");
216 : : }
217 : :
218 : 227 : Sequence< ::rtl::OUString > SAL_CALL UNOMemoryStream::getSupportedServiceNames_static()
219 : : {
220 : 227 : Sequence< OUString > aSeq(1);
221 [ + - ][ + - ]: 227 : aSeq[0] = getImplementationName_static();
222 : 227 : return aSeq;
223 : : }
224 : :
225 : 5473 : Reference< XInterface > SAL_CALL UNOMemoryStream::Create(
226 : : SAL_UNUSED_PARAMETER const Reference< XComponentContext >& )
227 : : {
228 [ + - ]: 5473 : return static_cast<OWeakObject*>(new UNOMemoryStream());
229 : : }
230 : :
231 : : } // namespace comphelper
232 : :
233 : 227 : void createRegistryInfo_UNOMemoryStream()
234 : : {
235 [ + - ][ + - ]: 227 : static ::comphelper::module::OAutoRegistration< ::comphelper::UNOMemoryStream > aAutoRegistration;
[ + - ][ # # ]
236 : 227 : }
237 : :
238 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|