1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MemoryByteGrabber.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef _MEMORY_BYTE_GRABBER_HXX_
31 #define _MEMORY_BYTE_GRABBER_HXX_
33 #include <com/sun/star/io/XInputStream.hpp>
34 #include <com/sun/star/io/XSeekable.hpp>
37 class MemoryByteGrabber
40 const com::sun::star::uno::Sequence
< sal_Int8
> maBuffer
;
41 const sal_Int8
*mpBuffer
;
42 sal_Int32 mnCurrent
, mnEnd
;
44 MemoryByteGrabber ( const com::sun::star::uno::Sequence
< sal_Int8
> & rBuffer
)
45 : maBuffer ( rBuffer
)
46 , mpBuffer ( rBuffer
.getConstArray() )
48 , mnEnd ( rBuffer
.getLength() )
54 const sal_Int8
* getCurrentPos () { return mpBuffer
+ mnCurrent
; }
56 // XInputStream chained
57 sal_Int32 SAL_CALL
readBytes( com::sun::star::uno::Sequence
< sal_Int8
>& aData
,
58 sal_Int32 nBytesToRead
)
59 throw(com::sun::star::io::NotConnectedException
, com::sun::star::io::BufferSizeExceededException
, com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
61 if ( nBytesToRead
< 0)
62 throw com::sun::star::io::BufferSizeExceededException();
64 if (nBytesToRead
+ mnCurrent
> mnEnd
)
65 nBytesToRead
= mnEnd
- mnCurrent
;
67 aData
.realloc ( nBytesToRead
);
68 memcpy ( aData
.getArray(), mpBuffer
+ mnCurrent
, nBytesToRead
);
69 mnCurrent
+= nBytesToRead
;
73 sal_Int32 SAL_CALL
readSomeBytes( com::sun::star::uno::Sequence
< sal_Int8
>& aData
,
74 sal_Int32 nMaxBytesToRead
)
75 throw(com::sun::star::io::NotConnectedException
, com::sun::star::io::BufferSizeExceededException
, com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
77 return readBytes( aData
, nMaxBytesToRead
);
79 void SAL_CALL
skipBytes( sal_Int32 nBytesToSkip
)
80 throw(com::sun::star::io::NotConnectedException
, com::sun::star::io::BufferSizeExceededException
, com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
82 mnCurrent
+= nBytesToSkip
;
84 sal_Int32 SAL_CALL
available( )
85 throw(com::sun::star::io::NotConnectedException
, com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
87 return mnEnd
- mnCurrent
;
89 void SAL_CALL
closeInput( )
90 throw(com::sun::star::io::NotConnectedException
, com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
94 // XSeekable chained...
95 sal_Int64 SAL_CALL
seek( sal_Int64 location
)
96 throw(com::sun::star::lang::IllegalArgumentException
, com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
98 if ( location
< 0 || location
> mnEnd
)
99 throw com::sun::star::lang::IllegalArgumentException ();
100 mnCurrent
= static_cast < sal_Int32
> ( location
);
103 sal_Int64 SAL_CALL
getPosition( )
104 throw(com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
108 sal_Int64 SAL_CALL
getLength( )
109 throw(com::sun::star::io::IOException
, com::sun::star::uno::RuntimeException
)
113 MemoryByteGrabber
& operator >> (sal_Int8
& rInt8
)
115 if (mnCurrent
+ 1 > mnEnd
)
118 rInt8
= mpBuffer
[mnCurrent
++] & 0xFF;
121 MemoryByteGrabber
& operator >> (sal_Int16
& rInt16
)
123 if (mnCurrent
+ 2 > mnEnd
)
127 rInt16
= mpBuffer
[mnCurrent
++] & 0xFF;
128 rInt16
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 8;
132 MemoryByteGrabber
& operator >> (sal_Int32
& rInt32
)
134 if (mnCurrent
+ 4 > mnEnd
)
138 rInt32
= mpBuffer
[mnCurrent
++] & 0xFF;
139 rInt32
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 8;
140 rInt32
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 16;
141 rInt32
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 24;
146 MemoryByteGrabber
& operator >> (sal_uInt8
& rInt8
)
148 if (mnCurrent
+ 1 > mnEnd
)
151 rInt8
= mpBuffer
[mnCurrent
++] & 0xFF;
154 MemoryByteGrabber
& operator >> (sal_uInt16
& rInt16
)
156 if (mnCurrent
+ 2 > mnEnd
)
160 rInt16
= mpBuffer
[mnCurrent
++] & 0xFF;
161 rInt16
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 8;
165 MemoryByteGrabber
& operator >> (sal_uInt32
& rInt32
)
167 if (mnCurrent
+ 4 > mnEnd
)
171 rInt32
= mpBuffer
[mnCurrent
++] & 0xFF;
172 rInt32
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 8;
173 rInt32
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 16;
174 rInt32
|= ( mpBuffer
[mnCurrent
++] & 0xFF ) << 24;