merge the formfield patch from ooo-build
[ooovba.git] / package / source / zipapi / MemoryByteGrabber.hxx
blobdb7087fcbc2f9b4b31cf487d3083d6d0610e4d60
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MemoryByteGrabber.hxx,v $
10 * $Revision: 1.7 $
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>
35 #include <string.h>
37 class MemoryByteGrabber
39 protected:
40 const com::sun::star::uno::Sequence < sal_Int8 > maBuffer;
41 const sal_Int8 *mpBuffer;
42 sal_Int32 mnCurrent, mnEnd;
43 public:
44 MemoryByteGrabber ( const com::sun::star::uno::Sequence < sal_Int8 > & rBuffer )
45 : maBuffer ( rBuffer )
46 , mpBuffer ( rBuffer.getConstArray() )
47 , mnCurrent ( 0 )
48 , mnEnd ( rBuffer.getLength() )
51 MemoryByteGrabber()
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;
70 return 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 );
101 return mnCurrent;
103 sal_Int64 SAL_CALL getPosition( )
104 throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
106 return mnCurrent;
108 sal_Int64 SAL_CALL getLength( )
109 throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
111 return mnEnd;
113 MemoryByteGrabber& operator >> (sal_Int8& rInt8)
115 if (mnCurrent + 1 > mnEnd )
116 rInt8 = 0;
117 else
118 rInt8 = mpBuffer [mnCurrent++] & 0xFF;
119 return *this;
121 MemoryByteGrabber& operator >> (sal_Int16& rInt16)
123 if (mnCurrent + 2 > mnEnd )
124 rInt16 = 0;
125 else
127 rInt16 = mpBuffer[mnCurrent++] & 0xFF;
128 rInt16 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
130 return *this;
132 MemoryByteGrabber& operator >> (sal_Int32& rInt32)
134 if (mnCurrent + 4 > mnEnd )
135 rInt32 = 0;
136 else
138 rInt32 = mpBuffer[mnCurrent++] & 0xFF;
139 rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
140 rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 16;
141 rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 24;
143 return *this;
146 MemoryByteGrabber& operator >> (sal_uInt8& rInt8)
148 if (mnCurrent + 1 > mnEnd )
149 rInt8 = 0;
150 else
151 rInt8 = mpBuffer [mnCurrent++] & 0xFF;
152 return *this;
154 MemoryByteGrabber& operator >> (sal_uInt16& rInt16)
156 if (mnCurrent + 2 > mnEnd )
157 rInt16 = 0;
158 else
160 rInt16 = mpBuffer [mnCurrent++] & 0xFF;
161 rInt16 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
163 return *this;
165 MemoryByteGrabber& operator >> (sal_uInt32& rInt32)
167 if (mnCurrent + 4 > mnEnd )
168 rInt32 = 0;
169 else
171 rInt32 = mpBuffer [mnCurrent++] & 0xFF;
172 rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
173 rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 16;
174 rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 24;
176 return *this;
180 #endif