1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_package.hxx"
30 #include <ByteGrabber.hxx>
31 #include <com/sun/star/io/XSeekable.hpp>
32 #include <com/sun/star/io/XInputStream.hpp>
33 
34 using namespace ::com::sun::star;
35 
36 /** ByteGrabber implements the >> operators on an XOutputStream. This is
37  *  potentially quite slow and may need to be optimised
38  */
39 
40 ByteGrabber::ByteGrabber(uno::Reference  < io::XInputStream > xIstream)
41 : xStream(xIstream)
42 , xSeek (xIstream, uno::UNO_QUERY )
43 , aSequence ( 4 )
44 {
45 	pSequence = aSequence.getArray();
46 }
47 
48 ByteGrabber::~ByteGrabber()
49 {
50 }
51 
52 void ByteGrabber::setInputStream (uno::Reference < io::XInputStream > xNewStream)
53 {
54     ::osl::MutexGuard aGuard( m_aMutex );
55 	xStream = xNewStream;
56 	xSeek = uno::Reference < io::XSeekable > (xNewStream, uno::UNO_QUERY);
57 }
58 
59 // XInputStream chained
60 sal_Int32 SAL_CALL ByteGrabber::readBytes( uno::Sequence< sal_Int8 >& aData,
61 										sal_Int32 nBytesToRead )
62 	throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException)
63 {
64     ::osl::MutexGuard aGuard( m_aMutex );
65 	return xStream->readBytes(aData, nBytesToRead );
66 }
67 
68 // XSeekable chained...
69 sal_Int64 SAL_CALL ByteGrabber::seek( sal_Int64 location )
70 	throw(lang::IllegalArgumentException, io::IOException, uno::RuntimeException)
71 {
72     ::osl::MutexGuard aGuard( m_aMutex );
73 	if (xSeek.is() )
74 	{
75 		sal_Int64 nLen = xSeek->getLength();
76 		if ( location < 0 || location > nLen )
77 			throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 1 );
78 		if (location > nLen )
79 			location = nLen;
80 		xSeek->seek( location );
81 		return location;
82 	}
83 	else
84 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
85 }
86 
87 sal_Int64 SAL_CALL ByteGrabber::getPosition(  )
88 		throw(io::IOException, uno::RuntimeException)
89 {
90     ::osl::MutexGuard aGuard( m_aMutex );
91 	if (xSeek.is() )
92 		return xSeek->getPosition();
93 	else
94 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
95 }
96 
97 sal_Int64 SAL_CALL ByteGrabber::getLength(  )
98 		throw(io::IOException, uno::RuntimeException)
99 {
100     ::osl::MutexGuard aGuard( m_aMutex );
101 	if (xSeek.is() )
102 		return xSeek->getLength();
103 	else
104 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
105 }
106 
107 ByteGrabber& ByteGrabber::operator >> (sal_Int8& rInt8)
108 {
109     ::osl::MutexGuard aGuard( m_aMutex );
110 	if (xStream->readBytes(aSequence,1) != 1)
111 		rInt8 = 0;
112 	else
113 		rInt8 = aSequence[0] & 0xFF;
114 	return *this;
115 }
116 
117 ByteGrabber& ByteGrabber::operator >> (sal_Int16& rInt16)
118 {
119     ::osl::MutexGuard aGuard( m_aMutex );
120 	if (xStream->readBytes ( aSequence, 2) != 2)
121 		rInt16 = 0;
122 	else
123 	{
124 		pSequence = aSequence.getConstArray();
125 		rInt16 = static_cast <sal_Int16>
126 			   ( (pSequence[0] & 0xFF)
127 		      | (pSequence[1] & 0xFF) << 8);
128 	}
129 	return *this;
130 }
131 
132 ByteGrabber& ByteGrabber::operator >> (sal_Int32& rInt32)
133 {
134     ::osl::MutexGuard aGuard( m_aMutex );
135 
136 	if (xStream->readBytes(aSequence, 4) != 4)
137 		rInt32 = 0;
138 	else
139 	{
140 		pSequence = aSequence.getConstArray();
141 		rInt32 = static_cast < sal_Int32 >
142 			    ( (pSequence[0] & 0xFF)
143 		      | ( pSequence[1] & 0xFF ) << 8
144 		      | ( pSequence[2] & 0xFF ) << 16
145 		      | ( pSequence[3] & 0xFF ) << 24 );
146 	}
147 	return *this;
148 }
149 
150 ByteGrabber& ByteGrabber::operator >> (sal_uInt8& rInt8)
151 {
152     ::osl::MutexGuard aGuard( m_aMutex );
153 
154 	if (xStream->readBytes(aSequence,1) != 1)
155 		rInt8 = 0;
156 	else
157 		rInt8 = static_cast < sal_uInt8 > (aSequence[0] & 0xFF );
158 	return *this;
159 }
160 ByteGrabber& ByteGrabber::operator >> (sal_uInt16& rInt16)
161 {
162     ::osl::MutexGuard aGuard( m_aMutex );
163 
164 	if (xStream->readBytes(aSequence, 2) != 2)
165 		rInt16 = 0;
166 	else
167 	{
168 		pSequence = aSequence.getConstArray();
169 		rInt16 = static_cast <sal_uInt16>
170 			   ( (pSequence[0] & 0xFF)
171 		      | (pSequence[1] & 0xFF) << 8);
172 	}
173 	return *this;
174 }
175 ByteGrabber& ByteGrabber::operator >> (sal_uInt32& ruInt32)
176 {
177     ::osl::MutexGuard aGuard( m_aMutex );
178 
179 	if (xStream->readBytes(aSequence, 4) != 4)
180 		ruInt32 = 0;
181 	else
182 	{
183 		pSequence = aSequence.getConstArray();
184 		ruInt32 = static_cast < sal_uInt32 >
185 			    ( (pSequence[0] & 0xFF)
186 		      | ( pSequence[1] & 0xFF ) << 8
187 		      | ( pSequence[2] & 0xFF ) << 16
188 		      | ( pSequence[3] & 0xFF ) << 24 );
189 	}
190 	return *this;
191 }
192