bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / streaming / memorystream.cxx
blob37016836d223bc66086333ee02fcae77bb30539b
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>
22 #include "comphelper_module.hxx"
23 #include "comphelper_services.hxx"
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/implbase4.hxx>
30 #include <osl/diagnose.h>
32 #include <string.h>
33 #include <vector>
35 using ::cppu::OWeakObject;
36 using ::cppu::WeakImplHelper4;
37 using namespace ::com::sun::star::io;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::osl;
42 namespace comphelper
45 class UNOMemoryStream : public WeakImplHelper4 < XStream, XSeekableInputStream, XOutputStream, XTruncate >
47 public:
48 UNOMemoryStream();
49 virtual ~UNOMemoryStream();
51 // XStream
52 virtual Reference< XInputStream > SAL_CALL getInputStream( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
53 virtual Reference< XOutputStream > SAL_CALL getOutputStream( ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
55 // XInputStream
56 virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
57 virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
58 virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
59 virtual sal_Int32 SAL_CALL available() throw (NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
60 virtual void SAL_CALL closeInput() throw (NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
62 // XSeekable
63 virtual void SAL_CALL seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
64 virtual sal_Int64 SAL_CALL getPosition() throw (IOException, RuntimeException, std::exception) SAL_OVERRIDE;
65 virtual sal_Int64 SAL_CALL getLength() throw (IOException, RuntimeException, std::exception) SAL_OVERRIDE;
67 // XOutputStream
68 virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
69 virtual void SAL_CALL flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
70 virtual void SAL_CALL closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
72 // XTruncate
73 virtual void SAL_CALL truncate() throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
75 // XServiceInfo - static versions (used for component registration)
76 static OUString SAL_CALL getImplementationName_static();
77 static Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
78 static Reference< XInterface > SAL_CALL Create( const Reference< ::com::sun::star::uno::XComponentContext >& );
80 private:
81 std::vector< sal_Int8 > maData;
82 sal_Int32 mnCursor;
85 UNOMemoryStream::UNOMemoryStream()
86 : mnCursor(0)
90 UNOMemoryStream::~UNOMemoryStream()
94 // XStream
95 Reference< XInputStream > SAL_CALL UNOMemoryStream::getInputStream( ) throw (RuntimeException, std::exception)
97 return this;
100 Reference< XOutputStream > SAL_CALL UNOMemoryStream::getOutputStream( ) throw (RuntimeException, std::exception)
102 return this;
105 // XInputStream
106 sal_Int32 SAL_CALL UNOMemoryStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
108 if( nBytesToRead < 0 )
109 throw IOException();
111 nBytesToRead = std::min( nBytesToRead, available() );
112 aData.realloc( nBytesToRead );
114 if( nBytesToRead )
116 sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin()));
117 sal_Int8* pCursor = &((pData)[mnCursor]);
118 memcpy( (void*)aData.getArray(), (void*)pCursor, nBytesToRead );
120 mnCursor += nBytesToRead;
123 return nBytesToRead;
126 sal_Int32 SAL_CALL UNOMemoryStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
128 return readBytes( aData, nMaxBytesToRead );
131 void SAL_CALL UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
133 if( nBytesToSkip < 0 )
134 throw IOException();
136 mnCursor += std::min( nBytesToSkip, available() );
139 sal_Int32 SAL_CALL UNOMemoryStream::available() throw (NotConnectedException, IOException, RuntimeException, std::exception)
141 return static_cast< sal_Int32 >( maData.size() ) - mnCursor;
144 void SAL_CALL UNOMemoryStream::closeInput() throw (NotConnectedException, IOException, RuntimeException, std::exception)
146 mnCursor = 0;
149 // XSeekable
150 void SAL_CALL UNOMemoryStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
152 if( (location < 0) || (location > SAL_MAX_INT32) )
153 throw IllegalArgumentException("this implementation does not support more than 2GB!", static_cast<OWeakObject*>(this), 0 );
155 // seek operation should be able to resize the stream
156 if ( location > static_cast< sal_Int64 >( maData.size() ) )
157 maData.resize( static_cast< sal_Int32 >( location ) );
159 if ( location > static_cast< sal_Int64 >( maData.size() ) )
160 maData.resize( static_cast< sal_Int32 >( location ) );
162 mnCursor = static_cast< sal_Int32 >( location );
165 sal_Int64 SAL_CALL UNOMemoryStream::getPosition() throw (IOException, RuntimeException, std::exception)
167 return static_cast< sal_Int64 >( mnCursor );
170 sal_Int64 SAL_CALL UNOMemoryStream::getLength() throw (IOException, RuntimeException, std::exception)
172 return static_cast< sal_Int64 >( maData.size() );
175 // XOutputStream
176 void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
178 const sal_Int32 nBytesToWrite( aData.getLength() );
179 if( nBytesToWrite )
181 sal_Int64 nNewSize = static_cast< sal_Int64 >( mnCursor + nBytesToWrite );
182 if( nNewSize > SAL_MAX_INT32 )
184 OSL_ASSERT(false);
185 throw IOException("this implementation does not support more than 2GB!", static_cast<OWeakObject*>(this) );
188 if( static_cast< sal_Int32 >( nNewSize ) > static_cast< sal_Int32 >( maData.size() ) )
189 maData.resize( static_cast< sal_Int32 >( nNewSize ) );
191 sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin()));
192 sal_Int8* pCursor = &(pData[mnCursor]);
193 memcpy( (void*)pCursor, (void*)aData.getConstArray(), nBytesToWrite );
195 mnCursor += nBytesToWrite;
199 void SAL_CALL UNOMemoryStream::flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
203 void SAL_CALL UNOMemoryStream::closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
205 mnCursor = 0;
208 //XTruncate
209 void SAL_CALL UNOMemoryStream::truncate() throw (IOException, RuntimeException, std::exception)
211 maData.resize( 0 );
212 mnCursor = 0;
215 OUString SAL_CALL UNOMemoryStream::getImplementationName_static()
217 return OUString("com.sun.star.comp.MemoryStream");
220 Sequence< OUString > SAL_CALL UNOMemoryStream::getSupportedServiceNames_static()
222 Sequence< OUString > aSeq(1);
223 aSeq[0] = getImplementationName_static();
224 return aSeq;
227 Reference< XInterface > SAL_CALL UNOMemoryStream::Create(
228 SAL_UNUSED_PARAMETER const Reference< XComponentContext >& )
230 return static_cast<OWeakObject*>(new UNOMemoryStream());
233 } // namespace comphelper
235 void createRegistryInfo_UNOMemoryStream()
237 static ::comphelper::module::OAutoRegistration< ::comphelper::UNOMemoryStream > aAutoRegistration;
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */