1*a3872823SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a3872823SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a3872823SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a3872823SAndrew Rist  * distributed with this work for additional information
6*a3872823SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a3872823SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a3872823SAndrew Rist  * "License"); you may not use this file except in compliance
9*a3872823SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a3872823SAndrew Rist  *
11*a3872823SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a3872823SAndrew Rist  *
13*a3872823SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a3872823SAndrew Rist  * software distributed under the License is distributed on an
15*a3872823SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a3872823SAndrew Rist  * KIND, either express or implied.  See the License for the
17*a3872823SAndrew Rist  * specific language governing permissions and limitations
18*a3872823SAndrew Rist  * under the License.
19*a3872823SAndrew Rist  *
20*a3872823SAndrew Rist  *************************************************************/
21*a3872823SAndrew Rist 
22*a3872823SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_package.hxx"
26cdf0e10cSrcweir #include <ByteGrabber.hxx>
27cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
28cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir using namespace ::com::sun::star;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir /** ByteGrabber implements the >> operators on an XOutputStream. This is
33cdf0e10cSrcweir  *  potentially quite slow and may need to be optimised
34cdf0e10cSrcweir  */
35cdf0e10cSrcweir 
ByteGrabber(uno::Reference<io::XInputStream> xIstream)36cdf0e10cSrcweir ByteGrabber::ByteGrabber(uno::Reference  < io::XInputStream > xIstream)
37cdf0e10cSrcweir : xStream(xIstream)
38cdf0e10cSrcweir , xSeek (xIstream, uno::UNO_QUERY )
39cdf0e10cSrcweir , aSequence ( 4 )
40cdf0e10cSrcweir {
41cdf0e10cSrcweir 	pSequence = aSequence.getArray();
42cdf0e10cSrcweir }
43cdf0e10cSrcweir 
~ByteGrabber()44cdf0e10cSrcweir ByteGrabber::~ByteGrabber()
45cdf0e10cSrcweir {
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
setInputStream(uno::Reference<io::XInputStream> xNewStream)48cdf0e10cSrcweir void ByteGrabber::setInputStream (uno::Reference < io::XInputStream > xNewStream)
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
51cdf0e10cSrcweir 	xStream = xNewStream;
52cdf0e10cSrcweir 	xSeek = uno::Reference < io::XSeekable > (xNewStream, uno::UNO_QUERY);
53cdf0e10cSrcweir }
54cdf0e10cSrcweir 
55cdf0e10cSrcweir // XInputStream chained
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)56cdf0e10cSrcweir sal_Int32 SAL_CALL ByteGrabber::readBytes( uno::Sequence< sal_Int8 >& aData,
57cdf0e10cSrcweir 										sal_Int32 nBytesToRead )
58cdf0e10cSrcweir 	throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException)
59cdf0e10cSrcweir {
60cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
61cdf0e10cSrcweir 	return xStream->readBytes(aData, nBytesToRead );
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir // XSeekable chained...
seek(sal_Int64 location)65cdf0e10cSrcweir sal_Int64 SAL_CALL ByteGrabber::seek( sal_Int64 location )
66cdf0e10cSrcweir 	throw(lang::IllegalArgumentException, io::IOException, uno::RuntimeException)
67cdf0e10cSrcweir {
68cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
69cdf0e10cSrcweir 	if (xSeek.is() )
70cdf0e10cSrcweir 	{
71cdf0e10cSrcweir 		sal_Int64 nLen = xSeek->getLength();
72cdf0e10cSrcweir 		if ( location < 0 || location > nLen )
73cdf0e10cSrcweir 			throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 1 );
74cdf0e10cSrcweir 		if (location > nLen )
75cdf0e10cSrcweir 			location = nLen;
76cdf0e10cSrcweir 		xSeek->seek( location );
77cdf0e10cSrcweir 		return location;
78cdf0e10cSrcweir 	}
79cdf0e10cSrcweir 	else
80cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
getPosition()83cdf0e10cSrcweir sal_Int64 SAL_CALL ByteGrabber::getPosition(  )
84cdf0e10cSrcweir 		throw(io::IOException, uno::RuntimeException)
85cdf0e10cSrcweir {
86cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
87cdf0e10cSrcweir 	if (xSeek.is() )
88cdf0e10cSrcweir 		return xSeek->getPosition();
89cdf0e10cSrcweir 	else
90cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
getLength()93cdf0e10cSrcweir sal_Int64 SAL_CALL ByteGrabber::getLength(  )
94cdf0e10cSrcweir 		throw(io::IOException, uno::RuntimeException)
95cdf0e10cSrcweir {
96cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
97cdf0e10cSrcweir 	if (xSeek.is() )
98cdf0e10cSrcweir 		return xSeek->getLength();
99cdf0e10cSrcweir 	else
100cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
operator >>(sal_Int8 & rInt8)103cdf0e10cSrcweir ByteGrabber& ByteGrabber::operator >> (sal_Int8& rInt8)
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
106cdf0e10cSrcweir 	if (xStream->readBytes(aSequence,1) != 1)
107cdf0e10cSrcweir 		rInt8 = 0;
108cdf0e10cSrcweir 	else
109cdf0e10cSrcweir 		rInt8 = aSequence[0] & 0xFF;
110cdf0e10cSrcweir 	return *this;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
operator >>(sal_Int16 & rInt16)113cdf0e10cSrcweir ByteGrabber& ByteGrabber::operator >> (sal_Int16& rInt16)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
116cdf0e10cSrcweir 	if (xStream->readBytes ( aSequence, 2) != 2)
117cdf0e10cSrcweir 		rInt16 = 0;
118cdf0e10cSrcweir 	else
119cdf0e10cSrcweir 	{
120cdf0e10cSrcweir 		pSequence = aSequence.getConstArray();
121cdf0e10cSrcweir 		rInt16 = static_cast <sal_Int16>
122cdf0e10cSrcweir 			   ( (pSequence[0] & 0xFF)
123cdf0e10cSrcweir 		      | (pSequence[1] & 0xFF) << 8);
124cdf0e10cSrcweir 	}
125cdf0e10cSrcweir 	return *this;
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
operator >>(sal_Int32 & rInt32)128cdf0e10cSrcweir ByteGrabber& ByteGrabber::operator >> (sal_Int32& rInt32)
129cdf0e10cSrcweir {
130cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 	if (xStream->readBytes(aSequence, 4) != 4)
133cdf0e10cSrcweir 		rInt32 = 0;
134cdf0e10cSrcweir 	else
135cdf0e10cSrcweir 	{
136cdf0e10cSrcweir 		pSequence = aSequence.getConstArray();
137cdf0e10cSrcweir 		rInt32 = static_cast < sal_Int32 >
138cdf0e10cSrcweir 			    ( (pSequence[0] & 0xFF)
139cdf0e10cSrcweir 		      | ( pSequence[1] & 0xFF ) << 8
140cdf0e10cSrcweir 		      | ( pSequence[2] & 0xFF ) << 16
141cdf0e10cSrcweir 		      | ( pSequence[3] & 0xFF ) << 24 );
142cdf0e10cSrcweir 	}
143cdf0e10cSrcweir 	return *this;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir 
operator >>(sal_uInt8 & rInt8)146cdf0e10cSrcweir ByteGrabber& ByteGrabber::operator >> (sal_uInt8& rInt8)
147cdf0e10cSrcweir {
148cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 	if (xStream->readBytes(aSequence,1) != 1)
151cdf0e10cSrcweir 		rInt8 = 0;
152cdf0e10cSrcweir 	else
153cdf0e10cSrcweir 		rInt8 = static_cast < sal_uInt8 > (aSequence[0] & 0xFF );
154cdf0e10cSrcweir 	return *this;
155cdf0e10cSrcweir }
operator >>(sal_uInt16 & rInt16)156cdf0e10cSrcweir ByteGrabber& ByteGrabber::operator >> (sal_uInt16& rInt16)
157cdf0e10cSrcweir {
158cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 	if (xStream->readBytes(aSequence, 2) != 2)
161cdf0e10cSrcweir 		rInt16 = 0;
162cdf0e10cSrcweir 	else
163cdf0e10cSrcweir 	{
164cdf0e10cSrcweir 		pSequence = aSequence.getConstArray();
165cdf0e10cSrcweir 		rInt16 = static_cast <sal_uInt16>
166cdf0e10cSrcweir 			   ( (pSequence[0] & 0xFF)
167cdf0e10cSrcweir 		      | (pSequence[1] & 0xFF) << 8);
168cdf0e10cSrcweir 	}
169cdf0e10cSrcweir 	return *this;
170cdf0e10cSrcweir }
operator >>(sal_uInt32 & ruInt32)171cdf0e10cSrcweir ByteGrabber& ByteGrabber::operator >> (sal_uInt32& ruInt32)
172cdf0e10cSrcweir {
173cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 	if (xStream->readBytes(aSequence, 4) != 4)
176cdf0e10cSrcweir 		ruInt32 = 0;
177cdf0e10cSrcweir 	else
178cdf0e10cSrcweir 	{
179cdf0e10cSrcweir 		pSequence = aSequence.getConstArray();
180cdf0e10cSrcweir 		ruInt32 = static_cast < sal_uInt32 >
181cdf0e10cSrcweir 			    ( (pSequence[0] & 0xFF)
182cdf0e10cSrcweir 		      | ( pSequence[1] & 0xFF ) << 8
183cdf0e10cSrcweir 		      | ( pSequence[2] & 0xFF ) << 16
184cdf0e10cSrcweir 		      | ( pSequence[3] & 0xFF ) << 24 );
185cdf0e10cSrcweir 	}
186cdf0e10cSrcweir 	return *this;
187cdf0e10cSrcweir }
188