masterfix DEV300: #i10000# build fix
[LibreOffice.git] / package / source / zipapi / ByteGrabber.cxx
blob40998c3de70d9306085d5e48ad2309210dee04e3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
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>
34 using namespace ::com::sun::star;
36 /** ByteGrabber implements the >> operators on an XOutputStream. This is
37 * potentially quite slow and may need to be optimised
40 ByteGrabber::ByteGrabber(uno::Reference < io::XInputStream > xIstream)
41 : xStream(xIstream)
42 , xSeek (xIstream, uno::UNO_QUERY )
43 , aSequence ( 4 )
45 pSequence = aSequence.getArray();
48 ByteGrabber::~ByteGrabber()
52 void ByteGrabber::setInputStream (uno::Reference < io::XInputStream > xNewStream)
54 ::osl::MutexGuard aGuard( m_aMutex );
55 xStream = xNewStream;
56 xSeek = uno::Reference < io::XSeekable > (xNewStream, uno::UNO_QUERY);
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)
64 ::osl::MutexGuard aGuard( m_aMutex );
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 ::osl::MutexGuard aGuard( m_aMutex );
73 if (xSeek.is() )
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;
83 else
84 throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
87 sal_Int64 SAL_CALL ByteGrabber::getPosition( )
88 throw(io::IOException, uno::RuntimeException)
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 >() );
97 sal_Int64 SAL_CALL ByteGrabber::getLength( )
98 throw(io::IOException, uno::RuntimeException)
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 >() );
107 ByteGrabber& ByteGrabber::operator >> (sal_Int8& rInt8)
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;
117 ByteGrabber& ByteGrabber::operator >> (sal_Int16& rInt16)
119 ::osl::MutexGuard aGuard( m_aMutex );
120 if (xStream->readBytes ( aSequence, 2) != 2)
121 rInt16 = 0;
122 else
124 pSequence = aSequence.getConstArray();
125 rInt16 = static_cast <sal_Int16>
126 ( (pSequence[0] & 0xFF)
127 | (pSequence[1] & 0xFF) << 8);
129 return *this;
132 ByteGrabber& ByteGrabber::operator >> (sal_Int32& rInt32)
134 ::osl::MutexGuard aGuard( m_aMutex );
136 if (xStream->readBytes(aSequence, 4) != 4)
137 rInt32 = 0;
138 else
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 );
147 return *this;
150 ByteGrabber& ByteGrabber::operator >> (sal_uInt8& rInt8)
152 ::osl::MutexGuard aGuard( m_aMutex );
154 if (xStream->readBytes(aSequence,1) != 1)
155 rInt8 = 0;
156 else
157 rInt8 = static_cast < sal_uInt8 > (aSequence[0] & 0xFF );
158 return *this;
160 ByteGrabber& ByteGrabber::operator >> (sal_uInt16& rInt16)
162 ::osl::MutexGuard aGuard( m_aMutex );
164 if (xStream->readBytes(aSequence, 2) != 2)
165 rInt16 = 0;
166 else
168 pSequence = aSequence.getConstArray();
169 rInt16 = static_cast <sal_uInt16>
170 ( (pSequence[0] & 0xFF)
171 | (pSequence[1] & 0xFF) << 8);
173 return *this;
175 ByteGrabber& ByteGrabber::operator >> (sal_uInt32& ruInt32)
177 ::osl::MutexGuard aGuard( m_aMutex );
179 if (xStream->readBytes(aSequence, 4) != 4)
180 ruInt32 = 0;
181 else
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 );
190 return *this;