LibreOffice Module package (master) 1
ByteGrabber.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
26
27using namespace ::com::sun::star;
28
29#if OSL_DEBUG_LEVEL > 0
30#define THROW_WHERE SAL_WHERE
31#else
32#define THROW_WHERE ""
33#endif
34
39ByteGrabber::ByteGrabber(uno::Reference < io::XInputStream > const & xIstream)
40: xStream(xIstream)
41, xSeek (xIstream, uno::UNO_QUERY )
42, aSequence ( 4 )
43{
44 pSequence = aSequence.getArray();
45}
46
48{
49}
50
51void ByteGrabber::setInputStream (const uno::Reference < io::XInputStream >& xNewStream)
52{
53 std::scoped_lock aGuard( m_aMutex );
54 xStream = xNewStream;
55 xSeek.set(xNewStream, uno::UNO_QUERY);
56}
57
58// XInputStream chained
59sal_Int32 ByteGrabber::readBytes( uno::Sequence< sal_Int8 >& aData,
60 sal_Int32 nBytesToRead )
61{
62 std::scoped_lock aGuard( m_aMutex );
63 return xStream->readBytes(aData, nBytesToRead );
64}
65
66// XSeekable chained...
67void ByteGrabber::seek( sal_Int64 location )
68{
69 std::scoped_lock aGuard( m_aMutex );
70 if (!xSeek.is() )
71 throw io::IOException(THROW_WHERE );
72
73 xSeek->seek( location );
74}
75
77{
78 std::scoped_lock aGuard( m_aMutex );
79 if (!xSeek.is() )
80 throw io::IOException(THROW_WHERE );
81
82 return xSeek->getPosition();
83}
84
86{
87 std::scoped_lock aGuard( m_aMutex );
88 if (!xSeek.is() )
89 throw io::IOException(THROW_WHERE );
90
91 return xSeek->getLength();
92}
93
95{
96 std::scoped_lock aGuard( m_aMutex );
97
98 if (xStream->readBytes(aSequence, 2) != 2)
99 return 0;
100
101 pSequence = aSequence.getConstArray();
102 return static_cast <sal_uInt16>
103 ( (pSequence[0] & 0xFF)
104 | (pSequence[1] & 0xFF) << 8);
105}
106
108{
109 std::scoped_lock aGuard( m_aMutex );
110
111 if (xStream->readBytes(aSequence, 4) != 4)
112 return 0;
113
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 );
120}
121
122/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define THROW_WHERE
Definition: ByteGrabber.cxx:30
Reference< XInputStream > xStream
sal_Int64 getLength()
Definition: ByteGrabber.cxx:85
const sal_Int8 * pSequence
Definition: ByteGrabber.hxx:37
std::mutex m_aMutex
Definition: ByteGrabber.hxx:32
void seek(sal_Int64 location)
Definition: ByteGrabber.cxx:67
void setInputStream(const css::uno::Reference< css::io::XInputStream > &xNewStream)
Definition: ByteGrabber.cxx:51
css::uno::Reference< css::io::XInputStream > xStream
Definition: ByteGrabber.hxx:34
ByteGrabber(css::uno::Reference< css::io::XInputStream > const &xIstream)
ByteGrabber implements the >> operators on an XOutputStream.
Definition: ByteGrabber.cxx:39
sal_Int64 getPosition()
Definition: ByteGrabber.cxx:76
sal_Int32 readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead)
Definition: ByteGrabber.cxx:59
css::uno::Reference< css::io::XSeekable > xSeek
Definition: ByteGrabber.hxx:35
css::uno::Sequence< sal_Int8 > aSequence
Definition: ByteGrabber.hxx:36
sal_uInt32 ReadUInt32()
sal_uInt16 ReadUInt16()
Definition: ByteGrabber.cxx:94
constexpr OUStringLiteral aData