android: Update app-specific/MIME type icons
[LibreOffice.git] / comphelper / source / streaming / memorystream.cxx
blobdc2a39d9e5b1ed2651f87521a1369f765b1ce3b5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <algorithm>
21 #include <cassert>
22 #include <memory>
24 #include <boost/core/noinit_adaptor.hpp>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/lang/XUnoTunnel.hpp>
29 #include <com/sun/star/io/IOException.hpp>
30 #include <com/sun/star/io/XStream.hpp>
31 #include <com/sun/star/io/XSeekableInputStream.hpp>
32 #include <com/sun/star/io/XTruncate.hpp>
33 //#include <com/sun/star/uno/XComponentContext.hpp>
34 #include <comphelper/bytereader.hxx>
35 #include <cppuhelper/implbase.hxx>
36 #include <cppuhelper/supportsservice.hxx>
37 #include <o3tl/safeint.hxx>
38 #include <osl/diagnose.h>
40 #include <string.h>
41 #include <vector>
43 namespace com::sun::star::uno { class XComponentContext; }
45 using ::cppu::OWeakObject;
46 using ::cppu::WeakImplHelper;
47 using namespace ::com::sun::star::io;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::lang;
50 using namespace ::osl;
52 namespace comphelper
55 namespace {
57 class UNOMemoryStream :
58 public WeakImplHelper<XServiceInfo, XStream, XSeekableInputStream, XOutputStream, XTruncate>,
59 public comphelper::ByteWriter
61 public:
62 UNOMemoryStream();
64 // XServiceInfo
65 virtual OUString SAL_CALL getImplementationName() override;
66 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
67 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
69 // XStream
70 virtual Reference< XInputStream > SAL_CALL getInputStream( ) override;
71 virtual Reference< XOutputStream > SAL_CALL getOutputStream( ) override;
73 // XInputStream
74 virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) override;
75 virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) override;
76 virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) override;
77 virtual sal_Int32 SAL_CALL available() override;
78 virtual void SAL_CALL closeInput() override;
80 // XSeekable
81 virtual void SAL_CALL seek( sal_Int64 location ) override;
82 virtual sal_Int64 SAL_CALL getPosition() override;
83 virtual sal_Int64 SAL_CALL getLength() override;
85 // XOutputStream
86 virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData ) override;
87 virtual void SAL_CALL flush() override;
88 virtual void SAL_CALL closeOutput() override;
90 // XTruncate
91 virtual void SAL_CALL truncate() override;
93 // comphelper::ByteWriter
94 virtual void writeBytes(const sal_Int8* aData, sal_Int32 nBytesToWrite) override;
96 private:
97 std::vector< sal_Int8, boost::noinit_adaptor<std::allocator<sal_Int8>> > maData;
98 sal_Int32 mnCursor;
103 UNOMemoryStream::UNOMemoryStream()
104 : mnCursor(0)
106 maData.reserve(1 * 1024 * 1024);
109 // XServiceInfo
110 OUString SAL_CALL UNOMemoryStream::getImplementationName()
112 return "com.sun.star.comp.MemoryStream";
115 sal_Bool SAL_CALL UNOMemoryStream::supportsService(const OUString& ServiceName)
117 return cppu::supportsService(this, ServiceName);
120 css::uno::Sequence<OUString> SAL_CALL UNOMemoryStream::getSupportedServiceNames()
122 return { "com.sun.star.comp.MemoryStream" };
125 // XStream
126 Reference< XInputStream > SAL_CALL UNOMemoryStream::getInputStream( )
128 return this;
131 Reference< XOutputStream > SAL_CALL UNOMemoryStream::getOutputStream( )
133 return this;
136 // XInputStream
137 sal_Int32 SAL_CALL UNOMemoryStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
139 if( nBytesToRead < 0 )
140 throw IOException("nBytesToRead < 0");
142 nBytesToRead = std::min( nBytesToRead, available() );
143 aData.realloc( nBytesToRead );
145 if( nBytesToRead )
147 sal_Int8* pData = &(*maData.begin());
148 sal_Int8* pCursor = &(pData[mnCursor]);
149 memcpy( aData.getArray(), pCursor, nBytesToRead );
151 mnCursor += nBytesToRead;
154 return nBytesToRead;
157 sal_Int32 SAL_CALL UNOMemoryStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
159 return readBytes( aData, nMaxBytesToRead );
162 void SAL_CALL UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip )
164 if( nBytesToSkip < 0 )
165 throw IOException("nBytesToSkip < 0");
167 mnCursor += std::min( nBytesToSkip, available() );
170 sal_Int32 SAL_CALL UNOMemoryStream::available()
172 return std::min<sal_Int64>( SAL_MAX_INT32, maData.size() - mnCursor);
175 void SAL_CALL UNOMemoryStream::closeInput()
177 mnCursor = 0;
180 // XSeekable
181 void SAL_CALL UNOMemoryStream::seek( sal_Int64 location )
183 if( (location < 0) || (location > SAL_MAX_INT32) )
184 throw IllegalArgumentException("this implementation does not support more than 2GB!", static_cast<OWeakObject*>(this), 0 );
186 // seek operation should be able to resize the stream
187 if ( o3tl::make_unsigned(location) > maData.size() )
188 maData.resize( static_cast< sal_Int32 >( location ) );
190 mnCursor = static_cast< sal_Int32 >( location );
193 sal_Int64 SAL_CALL UNOMemoryStream::getPosition()
195 return static_cast< sal_Int64 >( mnCursor );
198 sal_Int64 SAL_CALL UNOMemoryStream::getLength()
200 return static_cast< sal_Int64 >( maData.size() );
203 // XOutputStream
204 void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData )
206 writeBytes(aData.getConstArray(), aData.getLength());
209 void UNOMemoryStream::writeBytes( const sal_Int8* pInData, sal_Int32 nBytesToWrite )
211 assert(nBytesToWrite >= 0);
212 if( !nBytesToWrite )
213 return;
215 sal_Int64 nNewSize = static_cast<sal_Int64>(mnCursor) + nBytesToWrite;
216 if( nNewSize > SAL_MAX_INT32 )
218 OSL_ASSERT(false);
219 throw IOException("this implementation does not support more than 2GB!", static_cast<OWeakObject*>(this) );
222 if( o3tl::make_unsigned( nNewSize ) > maData.size() )
223 maData.resize( nNewSize );
225 sal_Int8* pData = &(*maData.begin());
226 sal_Int8* pCursor = &(pData[mnCursor]);
227 memcpy(pCursor, pInData, nBytesToWrite);
229 mnCursor += nBytesToWrite;
232 void SAL_CALL UNOMemoryStream::flush()
236 void SAL_CALL UNOMemoryStream::closeOutput()
238 mnCursor = 0;
241 //XTruncate
242 void SAL_CALL UNOMemoryStream::truncate()
244 maData.clear();
245 mnCursor = 0;
248 } // namespace comphelper
250 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
251 com_sun_star_comp_MemoryStream(
252 css::uno::XComponentContext *,
253 css::uno::Sequence<css::uno::Any> const &)
255 return cppu::acquire(new ::comphelper::UNOMemoryStream());
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */