1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
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
;
45 class UNOMemoryStream
: public WeakImplHelper4
< XStream
, XSeekableInputStream
, XOutputStream
, XTruncate
>
49 virtual ~UNOMemoryStream();
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
;
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
;
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
;
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
;
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
>& );
81 std::vector
< sal_Int8
> maData
;
85 UNOMemoryStream::UNOMemoryStream()
90 UNOMemoryStream::~UNOMemoryStream()
95 Reference
< XInputStream
> SAL_CALL
UNOMemoryStream::getInputStream( ) throw (RuntimeException
, std::exception
)
100 Reference
< XOutputStream
> SAL_CALL
UNOMemoryStream::getOutputStream( ) throw (RuntimeException
, std::exception
)
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 )
111 nBytesToRead
= std::min( nBytesToRead
, available() );
112 aData
.realloc( 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
;
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 )
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
)
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() );
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() );
181 sal_Int64 nNewSize
= static_cast< sal_Int64
>( mnCursor
+ nBytesToWrite
);
182 if( nNewSize
> SAL_MAX_INT32
)
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
)
209 void SAL_CALL
UNOMemoryStream::truncate() throw (IOException
, RuntimeException
, std::exception
)
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();
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: */