1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <ByteGrabber.hxx>
21 #include <sal/log.hxx>
22 #include <com/sun/star/io/IOException.hpp>
23 #include <com/sun/star/io/XSeekable.hpp>
24 #include <com/sun/star/io/XInputStream.hpp>
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 using namespace ::com::sun::star
;
29 #if OSL_DEBUG_LEVEL > 0
30 #define THROW_WHERE SAL_WHERE
32 #define THROW_WHERE ""
35 /** ByteGrabber implements the >> operators on an XOutputStream. This is
36 * potentially quite slow and may need to be optimised
39 ByteGrabber::ByteGrabber(uno::Reference
< io::XInputStream
> const & xIstream
)
41 , xSeek (xIstream
, uno::UNO_QUERY
)
43 mpByteReader
= dynamic_cast<comphelper::ByteReader
*>(xStream
.get());
47 ByteGrabber::~ByteGrabber()
51 void ByteGrabber::setInputStream (const uno::Reference
< io::XInputStream
>& xNewStream
)
54 xSeek
.set(xNewStream
, uno::UNO_QUERY
);
55 mpByteReader
= dynamic_cast<comphelper::ByteReader
*>(xStream
.get());
59 sal_Int32
ByteGrabber::readBytes( sal_Int8
* aData
,
60 sal_Int32 nBytesToRead
)
62 return mpByteReader
->readSomeBytes(aData
, nBytesToRead
);
65 // XSeekable chained...
66 void ByteGrabber::seek( sal_Int64 location
)
69 throw io::IOException(THROW_WHERE
);
71 xSeek
->seek( location
);
74 sal_Int64
ByteGrabber::getPosition( )
77 throw io::IOException(THROW_WHERE
);
79 return xSeek
->getPosition();
82 sal_Int64
ByteGrabber::getLength( )
85 throw io::IOException(THROW_WHERE
);
87 return xSeek
->getLength();
90 sal_uInt16
ByteGrabber::ReadUInt16()
92 if (mpByteReader
->readSomeBytes(maBuffer
.data(), 2) != 2)
95 return static_cast <sal_uInt16
>
96 ( (maBuffer
[0] & 0xFF)
97 | (maBuffer
[1] & 0xFF) << 8);
100 sal_uInt32
ByteGrabber::ReadUInt32()
102 if (mpByteReader
->readSomeBytes(maBuffer
.data(), 4) != 4)
105 return static_cast < sal_uInt32
>
106 ( (maBuffer
[0] & 0xFF)
107 | ( maBuffer
[1] & 0xFF ) << 8
108 | ( maBuffer
[2] & 0xFF ) << 16
109 | ( maBuffer
[3] & 0xFF ) << 24 );
112 sal_uInt64
ByteGrabber::ReadUInt64()
114 if (mpByteReader
->readSomeBytes(maBuffer
.data(), 8) != 8)
117 return static_cast<sal_uInt64
>(maBuffer
[0] & 0xFF)
118 | static_cast<sal_uInt64
>(maBuffer
[1] & 0xFF) << 8
119 | static_cast<sal_uInt64
>(maBuffer
[2] & 0xFF) << 16
120 | static_cast<sal_uInt64
>(maBuffer
[3] & 0xFF) << 24
121 | static_cast<sal_uInt64
>(maBuffer
[4] & 0xFF) << 32
122 | static_cast<sal_uInt64
>(maBuffer
[5] & 0xFF) << 40
123 | static_cast<sal_uInt64
>(maBuffer
[6] & 0xFF) << 48
124 | static_cast<sal_uInt64
>(maBuffer
[7] & 0xFF) << 56;
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */