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