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
)
44 pSequence
= aSequence
.getArray();
47 ByteGrabber::~ByteGrabber()
51 void ByteGrabber::setInputStream (const uno::Reference
< io::XInputStream
>& xNewStream
)
53 std::scoped_lock
aGuard( m_aMutex
);
55 xSeek
.set(xNewStream
, uno::UNO_QUERY
);
58 // XInputStream chained
59 sal_Int32
ByteGrabber::readBytes( uno::Sequence
< sal_Int8
>& aData
,
60 sal_Int32 nBytesToRead
)
62 std::scoped_lock
aGuard( m_aMutex
);
63 return xStream
->readBytes(aData
, nBytesToRead
);
66 // XSeekable chained...
67 void ByteGrabber::seek( sal_Int64 location
)
69 std::scoped_lock
aGuard( m_aMutex
);
71 throw io::IOException(THROW_WHERE
);
73 xSeek
->seek( location
);
76 sal_Int64
ByteGrabber::getPosition( )
78 std::scoped_lock
aGuard( m_aMutex
);
80 throw io::IOException(THROW_WHERE
);
82 return xSeek
->getPosition();
85 sal_Int64
ByteGrabber::getLength( )
87 std::scoped_lock
aGuard( m_aMutex
);
89 throw io::IOException(THROW_WHERE
);
91 return xSeek
->getLength();
94 sal_uInt16
ByteGrabber::ReadUInt16()
96 std::scoped_lock
aGuard( m_aMutex
);
98 if (xStream
->readBytes(aSequence
, 2) != 2)
101 pSequence
= aSequence
.getConstArray();
102 return static_cast <sal_uInt16
>
103 ( (pSequence
[0] & 0xFF)
104 | (pSequence
[1] & 0xFF) << 8);
107 sal_uInt32
ByteGrabber::ReadUInt32()
109 std::scoped_lock
aGuard( m_aMutex
);
111 if (xStream
->readBytes(aSequence
, 4) != 4)
114 pSequence
= aSequence
.getConstArray();
115 return static_cast < sal_uInt32
>
116 ( (pSequence
[0] & 0xFF)
117 | ( pSequence
[1] & 0xFF ) << 8
118 | ( pSequence
[2] & 0xFF ) << 16
119 | ( pSequence
[3] & 0xFF ) << 24 );
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */