merge the formfield patch from ooo-build
[ooovba.git] / package / source / zipapi / ByteGrabber.cxx
blob047064840ca7901830a2630de00d71f1d924cb95
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: ByteGrabber.cxx,v $
10 * $Revision: 1.16 $
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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_package.hxx"
33 #include <ByteGrabber.hxx>
34 #include <com/sun/star/io/XSeekable.hpp>
35 #include <com/sun/star/io/XInputStream.hpp>
37 using namespace ::com::sun::star;
39 /** ByteGrabber implements the >> operators on an XOutputStream. This is
40 * potentially quite slow and may need to be optimised
43 ByteGrabber::ByteGrabber(uno::Reference < io::XInputStream > xIstream)
44 : xStream(xIstream)
45 , xSeek (xIstream, uno::UNO_QUERY )
46 , aSequence ( 4 )
48 pSequence = aSequence.getArray();
51 ByteGrabber::~ByteGrabber()
54 void ByteGrabber::setInputStream (uno::Reference < io::XInputStream > xNewStream)
56 xStream = xNewStream;
57 xSeek = uno::Reference < io::XSeekable > (xNewStream, uno::UNO_QUERY);
60 // XInputStream chained
61 sal_Int32 SAL_CALL ByteGrabber::readBytes( uno::Sequence< sal_Int8 >& aData,
62 sal_Int32 nBytesToRead )
63 throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException)
65 return xStream->readBytes(aData, nBytesToRead );
68 // XSeekable chained...
69 sal_Int64 SAL_CALL ByteGrabber::seek( sal_Int64 location )
70 throw(lang::IllegalArgumentException, io::IOException, uno::RuntimeException)
72 if (xSeek.is() )
74 sal_Int64 nLen = xSeek->getLength();
75 if ( location < 0 || location > nLen )
76 throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 1 );
77 if (location > nLen )
78 location = nLen;
79 xSeek->seek( location );
80 return location;
82 else
83 throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
85 sal_Int64 SAL_CALL ByteGrabber::getPosition( )
86 throw(io::IOException, uno::RuntimeException)
88 if (xSeek.is() )
89 return xSeek->getPosition();
90 else
91 throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
93 sal_Int64 SAL_CALL ByteGrabber::getLength( )
94 throw(io::IOException, uno::RuntimeException)
96 if (xSeek.is() )
97 return xSeek->getLength();
98 else
99 throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
101 ByteGrabber& ByteGrabber::operator >> (sal_Int8& rInt8)
103 if (xStream->readBytes(aSequence,1) != 1)
104 rInt8 = 0;
105 else
106 rInt8 = aSequence[0] & 0xFF;
107 return *this;
109 ByteGrabber& ByteGrabber::operator >> (sal_Int16& rInt16)
111 if (xStream->readBytes ( aSequence, 2) != 2)
112 rInt16 = 0;
113 else
115 pSequence = aSequence.getConstArray();
116 rInt16 = static_cast <sal_Int16>
117 ( (pSequence[0] & 0xFF)
118 | (pSequence[1] & 0xFF) << 8);
120 return *this;
122 ByteGrabber& ByteGrabber::operator >> (sal_Int32& rInt32)
124 if (xStream->readBytes(aSequence, 4) != 4)
125 rInt32 = 0;
126 else
128 pSequence = aSequence.getConstArray();
129 rInt32 = static_cast < sal_Int32 >
130 ( (pSequence[0] & 0xFF)
131 | ( pSequence[1] & 0xFF ) << 8
132 | ( pSequence[2] & 0xFF ) << 16
133 | ( pSequence[3] & 0xFF ) << 24 );
135 return *this;
138 ByteGrabber& ByteGrabber::operator >> (sal_uInt8& rInt8)
140 if (xStream->readBytes(aSequence,1) != 1)
141 rInt8 = 0;
142 else
143 rInt8 = static_cast < sal_uInt8 > (aSequence[0] & 0xFF );
144 return *this;
146 ByteGrabber& ByteGrabber::operator >> (sal_uInt16& rInt16)
148 if (xStream->readBytes(aSequence, 2) != 2)
149 rInt16 = 0;
150 else
152 pSequence = aSequence.getConstArray();
153 rInt16 = static_cast <sal_uInt16>
154 ( (pSequence[0] & 0xFF)
155 | (pSequence[1] & 0xFF) << 8);
157 return *this;
159 ByteGrabber& ByteGrabber::operator >> (sal_uInt32& ruInt32)
161 if (xStream->readBytes(aSequence, 4) != 4)
162 ruInt32 = 0;
163 else
165 pSequence = aSequence.getConstArray();
166 ruInt32 = static_cast < sal_uInt32 >
167 ( (pSequence[0] & 0xFF)
168 | ( pSequence[1] & 0xFF ) << 8
169 | ( pSequence[2] & 0xFF ) << 16
170 | ( pSequence[3] & 0xFF ) << 24 );
172 return *this;