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
)
42 , xSeek (xIstream
, uno::UNO_QUERY
)
45 pSequence
= aSequence
.getArray();
48 ByteGrabber::~ByteGrabber()
52 void ByteGrabber::setInputStream (uno::Reference
< io::XInputStream
> xNewStream
)
54 ::osl::MutexGuard
aGuard( m_aMutex
);
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
);
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 );
80 xSeek
->seek( location
);
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
);
92 return xSeek
->getPosition();
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
);
102 return xSeek
->getLength();
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)
113 rInt8
= aSequence
[0] & 0xFF;
117 ByteGrabber
& ByteGrabber::operator >> (sal_Int16
& rInt16
)
119 ::osl::MutexGuard
aGuard( m_aMutex
);
120 if (xStream
->readBytes ( aSequence
, 2) != 2)
124 pSequence
= aSequence
.getConstArray();
125 rInt16
= static_cast <sal_Int16
>
126 ( (pSequence
[0] & 0xFF)
127 | (pSequence
[1] & 0xFF) << 8);
132 ByteGrabber
& ByteGrabber::operator >> (sal_Int32
& rInt32
)
134 ::osl::MutexGuard
aGuard( m_aMutex
);
136 if (xStream
->readBytes(aSequence
, 4) != 4)
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 );
150 ByteGrabber
& ByteGrabber::operator >> (sal_uInt8
& rInt8
)
152 ::osl::MutexGuard
aGuard( m_aMutex
);
154 if (xStream
->readBytes(aSequence
,1) != 1)
157 rInt8
= static_cast < sal_uInt8
> (aSequence
[0] & 0xFF );
160 ByteGrabber
& ByteGrabber::operator >> (sal_uInt16
& rInt16
)
162 ::osl::MutexGuard
aGuard( m_aMutex
);
164 if (xStream
->readBytes(aSequence
, 2) != 2)
168 pSequence
= aSequence
.getConstArray();
169 rInt16
= static_cast <sal_uInt16
>
170 ( (pSequence
[0] & 0xFF)
171 | (pSequence
[1] & 0xFF) << 8);
175 ByteGrabber
& ByteGrabber::operator >> (sal_uInt32
& ruInt32
)
177 ::osl::MutexGuard
aGuard( m_aMutex
);
179 if (xStream
->readBytes(aSequence
, 4) != 4)
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 );