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 .
19 #ifndef _MEMORY_BYTE_GRABBER_HXX_
20 #define _MEMORY_BYTE_GRABBER_HXX_
22 #include <com/sun/star/io/XInputStream.hpp>
23 #include <com/sun/star/io/XSeekable.hpp>
26 class MemoryByteGrabber
29 const com::sun::star::uno::Sequence
< sal_Int8
> maBuffer
;
30 const sal_Int8
*mpBuffer
;
31 sal_Int32 mnCurrent
, mnEnd
;
33 MemoryByteGrabber ( const com::sun::star::uno::Sequence
< sal_Int8
> & rBuffer
)
34 : maBuffer ( rBuffer
)
35 , mpBuffer ( rBuffer
.getConstArray() )
37 , mnEnd ( rBuffer
.getLength() )
43 const sal_Int8
* getCurrentPos () { return mpBuffer
+ mnCurrent
; }
45 // XInputStream chained
46 sal_Int32 SAL_CALL
readBytes( com::sun::star::uno::Sequence
< sal_Int8
>& aData
,
47 sal_Int32 nBytesToRead
)
48 throw(com::sun::star::io::NotConnectedException
, com::sun::star::io::BufferSizeExceededException
, com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
50 if ( nBytesToRead
< 0)
51 throw com::sun::star::io::BufferSizeExceededException();
53 if (nBytesToRead
+ mnCurrent
> mnEnd
)
54 nBytesToRead
= mnEnd
- mnCurrent
;
56 aData
.realloc ( nBytesToRead
);
57 memcpy( aData
.getArray(), mpBuffer
+ mnCurrent
, nBytesToRead
);
58 mnCurrent
+= nBytesToRead
;
62 sal_Int32 SAL_CALL
readSomeBytes( com::sun::star::uno::Sequence
< sal_Int8
>& aData
,
63 sal_Int32 nMaxBytesToRead
)
64 throw(com::sun::star::io::NotConnectedException
, com::sun::star::io::BufferSizeExceededException
, com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
66 return readBytes( aData
, nMaxBytesToRead
);
68 void SAL_CALL
skipBytes( sal_Int32 nBytesToSkip
)
69 throw(com::sun::star::io::NotConnectedException
, com::sun::star::io::BufferSizeExceededException
, com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
71 mnCurrent
+= nBytesToSkip
;
73 sal_Int32 SAL_CALL
available( )
74 throw(com::sun::star::io::NotConnectedException
, com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
76 return mnEnd
- mnCurrent
;
78 void SAL_CALL
closeInput( )
79 throw(com::sun::star::io::NotConnectedException
, com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
83 // XSeekable chained...
84 sal_Int64 SAL_CALL
seek( sal_Int64 location
)
85 throw(com::sun::star::lang::IllegalArgumentException
, com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
87 if ( location
< 0 || location
> mnEnd
)
88 throw com::sun::star::lang::IllegalArgumentException ();
89 mnCurrent
= static_cast < sal_Int32
> ( location
);
92 sal_Int64 SAL_CALL
getPosition( )
93 throw(com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
97 sal_Int64 SAL_CALL
getLength( )
98 throw(com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
102 MemoryByteGrabber
& operator >> (sal_Int8
& rInt8
)
104 if (mnCurrent
+ 1 > mnEnd
)
107 rInt8
= mpBuffer
[mnCurrent
++] & 0xFF;
110 MemoryByteGrabber
& operator >> (sal_Int16
& rInt16
)
112 if (mnCurrent
+ 2 > mnEnd
)
116 rInt16
= mpBuffer
[mnCurrent
++] & 0xFF;
117 rInt16
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 8;
121 MemoryByteGrabber
& operator >> (sal_Int32
& rInt32
)
123 if (mnCurrent
+ 4 > mnEnd
)
127 rInt32
= mpBuffer
[mnCurrent
++] & 0xFF;
128 rInt32
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 8;
129 rInt32
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 16;
130 rInt32
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 24;
135 MemoryByteGrabber
& operator >> (sal_uInt8
& rInt8
)
137 if (mnCurrent
+ 1 > mnEnd
)
140 rInt8
= mpBuffer
[mnCurrent
++] & 0xFF;
143 MemoryByteGrabber
& operator >> (sal_uInt16
& rInt16
)
145 if (mnCurrent
+ 2 > mnEnd
)
149 rInt16
= mpBuffer
[mnCurrent
++] & 0xFF;
150 rInt16
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 8;
154 MemoryByteGrabber
& operator >> (sal_uInt32
& rInt32
)
156 if (mnCurrent
+ 4 > mnEnd
)
160 rInt32
= mpBuffer
[mnCurrent
++] & 0xFF;
161 rInt32
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 8;
162 rInt32
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 16;
163 rInt32
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 24;
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */