1*f319bb99SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f319bb99SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f319bb99SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f319bb99SAndrew Rist  * distributed with this work for additional information
6*f319bb99SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f319bb99SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f319bb99SAndrew Rist  * "License"); you may not use this file except in compliance
9*f319bb99SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f319bb99SAndrew Rist  *
11*f319bb99SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f319bb99SAndrew Rist  *
13*f319bb99SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f319bb99SAndrew Rist  * software distributed under the License is distributed on an
15*f319bb99SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f319bb99SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f319bb99SAndrew Rist  * specific language governing permissions and limitations
18*f319bb99SAndrew Rist  * under the License.
19*f319bb99SAndrew Rist  *
20*f319bb99SAndrew Rist  *************************************************************/
21*f319bb99SAndrew Rist 
22*f319bb99SAndrew Rist 
23cdf0e10cSrcweir #ifndef _MEMORY_BYTE_GRABBER_HXX_
24cdf0e10cSrcweir #define _MEMORY_BYTE_GRABBER_HXX_
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
27cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
28cdf0e10cSrcweir #include <string.h>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir class MemoryByteGrabber
31cdf0e10cSrcweir {
32cdf0e10cSrcweir protected:
33cdf0e10cSrcweir 	const com::sun::star::uno::Sequence < sal_Int8 > maBuffer;
34cdf0e10cSrcweir 	const sal_Int8 *mpBuffer;
35cdf0e10cSrcweir 	sal_Int32 mnCurrent, mnEnd;
36cdf0e10cSrcweir public:
MemoryByteGrabber(const com::sun::star::uno::Sequence<sal_Int8> & rBuffer)37cdf0e10cSrcweir 	MemoryByteGrabber ( const com::sun::star::uno::Sequence < sal_Int8 > & rBuffer )
38cdf0e10cSrcweir 	: maBuffer ( rBuffer )
39cdf0e10cSrcweir 	, mpBuffer ( rBuffer.getConstArray() )
40cdf0e10cSrcweir 	, mnCurrent ( 0 )
41cdf0e10cSrcweir 	, mnEnd ( rBuffer.getLength() )
42cdf0e10cSrcweir 	{
43cdf0e10cSrcweir 	}
MemoryByteGrabber()44cdf0e10cSrcweir 	MemoryByteGrabber()
45cdf0e10cSrcweir 	{
46cdf0e10cSrcweir 	}
getCurrentPos()47cdf0e10cSrcweir 	const sal_Int8 * getCurrentPos () { return mpBuffer + mnCurrent; }
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 	// XInputStream chained
readBytes(com::sun::star::uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)50cdf0e10cSrcweir 	sal_Int32 SAL_CALL readBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData,
51cdf0e10cSrcweir 											sal_Int32 nBytesToRead )
52cdf0e10cSrcweir 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
53cdf0e10cSrcweir 	{
54cdf0e10cSrcweir 		if ( nBytesToRead < 0)
55cdf0e10cSrcweir 			throw com::sun::star::io::BufferSizeExceededException();
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 		if (nBytesToRead + mnCurrent > mnEnd)
58cdf0e10cSrcweir 			nBytesToRead = mnEnd - mnCurrent;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 		aData.realloc ( nBytesToRead );
61cdf0e10cSrcweir 		rtl_copyMemory( aData.getArray(), mpBuffer + mnCurrent, nBytesToRead );
62cdf0e10cSrcweir 		mnCurrent += nBytesToRead;
63cdf0e10cSrcweir 		return nBytesToRead;
64cdf0e10cSrcweir 	}
65cdf0e10cSrcweir 
readSomeBytes(com::sun::star::uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)66cdf0e10cSrcweir 	sal_Int32 SAL_CALL readSomeBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData,
67cdf0e10cSrcweir 													sal_Int32 nMaxBytesToRead )
68cdf0e10cSrcweir 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
69cdf0e10cSrcweir 	{
70cdf0e10cSrcweir 		return readBytes( aData, nMaxBytesToRead );
71cdf0e10cSrcweir 	}
skipBytes(sal_Int32 nBytesToSkip)72cdf0e10cSrcweir 	void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
73cdf0e10cSrcweir 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
74cdf0e10cSrcweir 	{
75cdf0e10cSrcweir 		mnCurrent += nBytesToSkip;
76cdf0e10cSrcweir 	}
available()77cdf0e10cSrcweir 	sal_Int32 SAL_CALL available(  )
78cdf0e10cSrcweir 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
79cdf0e10cSrcweir 	{
80cdf0e10cSrcweir 		return mnEnd - mnCurrent;
81cdf0e10cSrcweir 	}
closeInput()82cdf0e10cSrcweir 	void SAL_CALL closeInput(  )
83cdf0e10cSrcweir 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
84cdf0e10cSrcweir 	{
85cdf0e10cSrcweir 	}
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 	// XSeekable chained...
seek(sal_Int64 location)88cdf0e10cSrcweir 	sal_Int64 SAL_CALL seek( sal_Int64 location )
89cdf0e10cSrcweir 		throw(com::sun::star::lang::IllegalArgumentException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
90cdf0e10cSrcweir 	{
91cdf0e10cSrcweir 		if ( location < 0 || location > mnEnd )
92cdf0e10cSrcweir 			throw com::sun::star::lang::IllegalArgumentException ();
93cdf0e10cSrcweir 		mnCurrent = static_cast < sal_Int32 > ( location );
94cdf0e10cSrcweir 		return mnCurrent;
95cdf0e10cSrcweir 	}
getPosition()96cdf0e10cSrcweir 	sal_Int64 SAL_CALL getPosition(  )
97cdf0e10cSrcweir 			throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
98cdf0e10cSrcweir 	{
99cdf0e10cSrcweir 		return mnCurrent;
100cdf0e10cSrcweir 	}
getLength()101cdf0e10cSrcweir 	sal_Int64 SAL_CALL getLength(  )
102cdf0e10cSrcweir 			throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
103cdf0e10cSrcweir 	{
104cdf0e10cSrcweir 		return mnEnd;
105cdf0e10cSrcweir 	}
operator >>(sal_Int8 & rInt8)106cdf0e10cSrcweir 	MemoryByteGrabber& operator >> (sal_Int8& rInt8)
107cdf0e10cSrcweir 	{
108cdf0e10cSrcweir 		if (mnCurrent + 1 > mnEnd )
109cdf0e10cSrcweir 			rInt8 = 0;
110cdf0e10cSrcweir 		else
111cdf0e10cSrcweir 			rInt8 = mpBuffer [mnCurrent++] & 0xFF;
112cdf0e10cSrcweir 		return *this;
113cdf0e10cSrcweir 	}
operator >>(sal_Int16 & rInt16)114cdf0e10cSrcweir 	MemoryByteGrabber& operator >> (sal_Int16& rInt16)
115cdf0e10cSrcweir 	{
116cdf0e10cSrcweir 		if (mnCurrent + 2 > mnEnd )
117cdf0e10cSrcweir 			rInt16 = 0;
118cdf0e10cSrcweir 		else
119cdf0e10cSrcweir 		{
120cdf0e10cSrcweir 			rInt16  =   mpBuffer[mnCurrent++] & 0xFF;
121cdf0e10cSrcweir 			rInt16 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
122cdf0e10cSrcweir 		}
123cdf0e10cSrcweir 		return *this;
124cdf0e10cSrcweir 	}
operator >>(sal_Int32 & rInt32)125cdf0e10cSrcweir 	MemoryByteGrabber& operator >> (sal_Int32& rInt32)
126cdf0e10cSrcweir 	{
127cdf0e10cSrcweir 		if (mnCurrent + 4 > mnEnd )
128cdf0e10cSrcweir 			rInt32 = 0;
129cdf0e10cSrcweir 		else
130cdf0e10cSrcweir 		{
131cdf0e10cSrcweir 			rInt32  =   mpBuffer[mnCurrent++] & 0xFF;
132cdf0e10cSrcweir 			rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
133cdf0e10cSrcweir 			rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 16;
134cdf0e10cSrcweir 			rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 24;
135cdf0e10cSrcweir 		}
136cdf0e10cSrcweir 		return *this;
137cdf0e10cSrcweir 	}
138cdf0e10cSrcweir 
operator >>(sal_uInt8 & rInt8)139cdf0e10cSrcweir 	MemoryByteGrabber& operator >> (sal_uInt8& rInt8)
140cdf0e10cSrcweir 	{
141cdf0e10cSrcweir 		if (mnCurrent + 1 > mnEnd )
142cdf0e10cSrcweir 			rInt8 = 0;
143cdf0e10cSrcweir 		else
144cdf0e10cSrcweir 			rInt8 = mpBuffer [mnCurrent++] & 0xFF;
145cdf0e10cSrcweir 		return *this;
146cdf0e10cSrcweir 	}
operator >>(sal_uInt16 & rInt16)147cdf0e10cSrcweir 	MemoryByteGrabber& operator >> (sal_uInt16& rInt16)
148cdf0e10cSrcweir 	{
149cdf0e10cSrcweir 		if (mnCurrent + 2 > mnEnd )
150cdf0e10cSrcweir 			rInt16 = 0;
151cdf0e10cSrcweir 		else
152cdf0e10cSrcweir 		{
153cdf0e10cSrcweir 			rInt16  =   mpBuffer [mnCurrent++] & 0xFF;
154cdf0e10cSrcweir 			rInt16 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
155cdf0e10cSrcweir 		}
156cdf0e10cSrcweir 		return *this;
157cdf0e10cSrcweir 	}
operator >>(sal_uInt32 & rInt32)158cdf0e10cSrcweir 	MemoryByteGrabber& operator >> (sal_uInt32& rInt32)
159cdf0e10cSrcweir 	{
160cdf0e10cSrcweir 		if (mnCurrent + 4 > mnEnd )
161cdf0e10cSrcweir 			rInt32 = 0;
162cdf0e10cSrcweir 		else
163cdf0e10cSrcweir 		{
164cdf0e10cSrcweir 			rInt32  =   mpBuffer [mnCurrent++] & 0xFF;
165cdf0e10cSrcweir 			rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
166cdf0e10cSrcweir 			rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 16;
167cdf0e10cSrcweir 			rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 24;
168cdf0e10cSrcweir 		}
169cdf0e10cSrcweir 		return *this;
170cdf0e10cSrcweir 	}
171cdf0e10cSrcweir };
172cdf0e10cSrcweir 
173cdf0e10cSrcweir #endif
174