1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #ifndef _MEMORY_BYTE_GRABBER_HXX_
28*cdf0e10cSrcweir #define _MEMORY_BYTE_GRABBER_HXX_
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
32*cdf0e10cSrcweir #include <string.h>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir class MemoryByteGrabber
35*cdf0e10cSrcweir {
36*cdf0e10cSrcweir protected:
37*cdf0e10cSrcweir 	const com::sun::star::uno::Sequence < sal_Int8 > maBuffer;
38*cdf0e10cSrcweir 	const sal_Int8 *mpBuffer;
39*cdf0e10cSrcweir 	sal_Int32 mnCurrent, mnEnd;
40*cdf0e10cSrcweir public:
41*cdf0e10cSrcweir 	MemoryByteGrabber ( const com::sun::star::uno::Sequence < sal_Int8 > & rBuffer )
42*cdf0e10cSrcweir 	: maBuffer ( rBuffer )
43*cdf0e10cSrcweir 	, mpBuffer ( rBuffer.getConstArray() )
44*cdf0e10cSrcweir 	, mnCurrent ( 0 )
45*cdf0e10cSrcweir 	, mnEnd ( rBuffer.getLength() )
46*cdf0e10cSrcweir 	{
47*cdf0e10cSrcweir 	}
48*cdf0e10cSrcweir 	MemoryByteGrabber()
49*cdf0e10cSrcweir 	{
50*cdf0e10cSrcweir 	}
51*cdf0e10cSrcweir 	const sal_Int8 * getCurrentPos () { return mpBuffer + mnCurrent; }
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 	// XInputStream chained
54*cdf0e10cSrcweir 	sal_Int32 SAL_CALL readBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData,
55*cdf0e10cSrcweir 											sal_Int32 nBytesToRead )
56*cdf0e10cSrcweir 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
57*cdf0e10cSrcweir 	{
58*cdf0e10cSrcweir 		if ( nBytesToRead < 0)
59*cdf0e10cSrcweir 			throw com::sun::star::io::BufferSizeExceededException();
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 		if (nBytesToRead + mnCurrent > mnEnd)
62*cdf0e10cSrcweir 			nBytesToRead = mnEnd - mnCurrent;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 		aData.realloc ( nBytesToRead );
65*cdf0e10cSrcweir 		rtl_copyMemory( aData.getArray(), mpBuffer + mnCurrent, nBytesToRead );
66*cdf0e10cSrcweir 		mnCurrent += nBytesToRead;
67*cdf0e10cSrcweir 		return nBytesToRead;
68*cdf0e10cSrcweir 	}
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 	sal_Int32 SAL_CALL readSomeBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData,
71*cdf0e10cSrcweir 													sal_Int32 nMaxBytesToRead )
72*cdf0e10cSrcweir 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
73*cdf0e10cSrcweir 	{
74*cdf0e10cSrcweir 		return readBytes( aData, nMaxBytesToRead );
75*cdf0e10cSrcweir 	}
76*cdf0e10cSrcweir 	void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
77*cdf0e10cSrcweir 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
78*cdf0e10cSrcweir 	{
79*cdf0e10cSrcweir 		mnCurrent += nBytesToSkip;
80*cdf0e10cSrcweir 	}
81*cdf0e10cSrcweir 	sal_Int32 SAL_CALL available(  )
82*cdf0e10cSrcweir 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
83*cdf0e10cSrcweir 	{
84*cdf0e10cSrcweir 		return mnEnd - mnCurrent;
85*cdf0e10cSrcweir 	}
86*cdf0e10cSrcweir 	void SAL_CALL closeInput(  )
87*cdf0e10cSrcweir 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
88*cdf0e10cSrcweir 	{
89*cdf0e10cSrcweir 	}
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 	// XSeekable chained...
92*cdf0e10cSrcweir 	sal_Int64 SAL_CALL seek( sal_Int64 location )
93*cdf0e10cSrcweir 		throw(com::sun::star::lang::IllegalArgumentException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
94*cdf0e10cSrcweir 	{
95*cdf0e10cSrcweir 		if ( location < 0 || location > mnEnd )
96*cdf0e10cSrcweir 			throw com::sun::star::lang::IllegalArgumentException ();
97*cdf0e10cSrcweir 		mnCurrent = static_cast < sal_Int32 > ( location );
98*cdf0e10cSrcweir 		return mnCurrent;
99*cdf0e10cSrcweir 	}
100*cdf0e10cSrcweir 	sal_Int64 SAL_CALL getPosition(  )
101*cdf0e10cSrcweir 			throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
102*cdf0e10cSrcweir 	{
103*cdf0e10cSrcweir 		return mnCurrent;
104*cdf0e10cSrcweir 	}
105*cdf0e10cSrcweir 	sal_Int64 SAL_CALL getLength(  )
106*cdf0e10cSrcweir 			throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
107*cdf0e10cSrcweir 	{
108*cdf0e10cSrcweir 		return mnEnd;
109*cdf0e10cSrcweir 	}
110*cdf0e10cSrcweir 	MemoryByteGrabber& operator >> (sal_Int8& rInt8)
111*cdf0e10cSrcweir 	{
112*cdf0e10cSrcweir 		if (mnCurrent + 1 > mnEnd )
113*cdf0e10cSrcweir 			rInt8 = 0;
114*cdf0e10cSrcweir 		else
115*cdf0e10cSrcweir 			rInt8 = mpBuffer [mnCurrent++] & 0xFF;
116*cdf0e10cSrcweir 		return *this;
117*cdf0e10cSrcweir 	}
118*cdf0e10cSrcweir 	MemoryByteGrabber& operator >> (sal_Int16& rInt16)
119*cdf0e10cSrcweir 	{
120*cdf0e10cSrcweir 		if (mnCurrent + 2 > mnEnd )
121*cdf0e10cSrcweir 			rInt16 = 0;
122*cdf0e10cSrcweir 		else
123*cdf0e10cSrcweir 		{
124*cdf0e10cSrcweir 			rInt16  =   mpBuffer[mnCurrent++] & 0xFF;
125*cdf0e10cSrcweir 			rInt16 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
126*cdf0e10cSrcweir 		}
127*cdf0e10cSrcweir 		return *this;
128*cdf0e10cSrcweir 	}
129*cdf0e10cSrcweir 	MemoryByteGrabber& operator >> (sal_Int32& rInt32)
130*cdf0e10cSrcweir 	{
131*cdf0e10cSrcweir 		if (mnCurrent + 4 > mnEnd )
132*cdf0e10cSrcweir 			rInt32 = 0;
133*cdf0e10cSrcweir 		else
134*cdf0e10cSrcweir 		{
135*cdf0e10cSrcweir 			rInt32  =   mpBuffer[mnCurrent++] & 0xFF;
136*cdf0e10cSrcweir 			rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
137*cdf0e10cSrcweir 			rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 16;
138*cdf0e10cSrcweir 			rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 24;
139*cdf0e10cSrcweir 		}
140*cdf0e10cSrcweir 		return *this;
141*cdf0e10cSrcweir 	}
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	MemoryByteGrabber& operator >> (sal_uInt8& rInt8)
144*cdf0e10cSrcweir 	{
145*cdf0e10cSrcweir 		if (mnCurrent + 1 > mnEnd )
146*cdf0e10cSrcweir 			rInt8 = 0;
147*cdf0e10cSrcweir 		else
148*cdf0e10cSrcweir 			rInt8 = mpBuffer [mnCurrent++] & 0xFF;
149*cdf0e10cSrcweir 		return *this;
150*cdf0e10cSrcweir 	}
151*cdf0e10cSrcweir 	MemoryByteGrabber& operator >> (sal_uInt16& rInt16)
152*cdf0e10cSrcweir 	{
153*cdf0e10cSrcweir 		if (mnCurrent + 2 > mnEnd )
154*cdf0e10cSrcweir 			rInt16 = 0;
155*cdf0e10cSrcweir 		else
156*cdf0e10cSrcweir 		{
157*cdf0e10cSrcweir 			rInt16  =   mpBuffer [mnCurrent++] & 0xFF;
158*cdf0e10cSrcweir 			rInt16 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
159*cdf0e10cSrcweir 		}
160*cdf0e10cSrcweir 		return *this;
161*cdf0e10cSrcweir 	}
162*cdf0e10cSrcweir 	MemoryByteGrabber& operator >> (sal_uInt32& rInt32)
163*cdf0e10cSrcweir 	{
164*cdf0e10cSrcweir 		if (mnCurrent + 4 > mnEnd )
165*cdf0e10cSrcweir 			rInt32 = 0;
166*cdf0e10cSrcweir 		else
167*cdf0e10cSrcweir 		{
168*cdf0e10cSrcweir 			rInt32  =   mpBuffer [mnCurrent++] & 0xFF;
169*cdf0e10cSrcweir 			rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
170*cdf0e10cSrcweir 			rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 16;
171*cdf0e10cSrcweir 			rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 24;
172*cdf0e10cSrcweir 		}
173*cdf0e10cSrcweir 		return *this;
174*cdf0e10cSrcweir 	}
175*cdf0e10cSrcweir };
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir #endif
178