1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ByteGrabber.cxx,v $
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
)
45 , xSeek (xIstream
, uno::UNO_QUERY
)
48 pSequence
= aSequence
.getArray();
51 ByteGrabber::~ByteGrabber()
54 void ByteGrabber::setInputStream (uno::Reference
< io::XInputStream
> 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
)
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 );
79 xSeek
->seek( location
);
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
)
89 return xSeek
->getPosition();
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
)
97 return xSeek
->getLength();
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)
106 rInt8
= aSequence
[0] & 0xFF;
109 ByteGrabber
& ByteGrabber::operator >> (sal_Int16
& rInt16
)
111 if (xStream
->readBytes ( aSequence
, 2) != 2)
115 pSequence
= aSequence
.getConstArray();
116 rInt16
= static_cast <sal_Int16
>
117 ( (pSequence
[0] & 0xFF)
118 | (pSequence
[1] & 0xFF) << 8);
122 ByteGrabber
& ByteGrabber::operator >> (sal_Int32
& rInt32
)
124 if (xStream
->readBytes(aSequence
, 4) != 4)
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 );
138 ByteGrabber
& ByteGrabber::operator >> (sal_uInt8
& rInt8
)
140 if (xStream
->readBytes(aSequence
,1) != 1)
143 rInt8
= static_cast < sal_uInt8
> (aSequence
[0] & 0xFF );
146 ByteGrabber
& ByteGrabber::operator >> (sal_uInt16
& rInt16
)
148 if (xStream
->readBytes(aSequence
, 2) != 2)
152 pSequence
= aSequence
.getConstArray();
153 rInt16
= static_cast <sal_uInt16
>
154 ( (pSequence
[0] & 0xFF)
155 | (pSequence
[1] & 0xFF) << 8);
159 ByteGrabber
& ByteGrabber::operator >> (sal_uInt32
& ruInt32
)
161 if (xStream
->readBytes(aSequence
, 4) != 4)
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 );