LibreOffice Module unotools (master) 1
streamhelper.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 <sal/config.h>
21
22#include <com/sun/star/io/BufferSizeExceededException.hpp>
23#include <com/sun/star/io/IOException.hpp>
24#include <com/sun/star/io/NotConnectedException.hpp>
25#include <o3tl/safeint.hxx>
27
28namespace utl
29{
30
31sal_Int32 SAL_CALL OInputStreamHelper::readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
32{
33 if (!m_xLockBytes.is())
34 throw css::io::NotConnectedException(OUString(), getXWeak());
35
36 if (nBytesToRead < 0)
37 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
38
39 std::scoped_lock aGuard( m_aMutex );
40 if (aData.getLength() < nBytesToRead)
41 aData.realloc(nBytesToRead);
42
43 std::size_t nRead(0);
44 ErrCode nError = m_xLockBytes->ReadAt(m_nActPos, static_cast<void*>(aData.getArray()), nBytesToRead, &nRead);
45 m_nActPos += nRead;
46
47 if (nError != ERRCODE_NONE)
48 throw css::io::IOException(OUString(), getXWeak());
49
50 // adjust sequence if data read is lower than the desired data
51 if (nRead < o3tl::make_unsigned(aData.getLength()))
52 aData.realloc( nRead );
53
54 return nRead;
55}
56
57void SAL_CALL OInputStreamHelper::seek( sal_Int64 location )
58{
59 std::scoped_lock aGuard( m_aMutex );
60 m_nActPos = location;
61}
62
64{
65 return m_nActPos;
66}
67
68sal_Int64 SAL_CALL OInputStreamHelper::getLength( )
69{
70 if (!m_xLockBytes.is())
71 return 0;
72
73 std::scoped_lock aGuard( m_aMutex );
74 SvLockBytesStat aStat;
75 m_xLockBytes->Stat( &aStat );
76 return aStat.nSize;
77}
78
79sal_Int32 SAL_CALL OInputStreamHelper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData,
80 sal_Int32 nMaxBytesToRead)
81{
82 // read all data desired
83 return readBytes(aData, nMaxBytesToRead);
84}
85
86void SAL_CALL OInputStreamHelper::skipBytes(sal_Int32 nBytesToSkip)
87{
88 std::scoped_lock aGuard( m_aMutex );
89 if (!m_xLockBytes.is())
90 throw css::io::NotConnectedException(OUString(), getXWeak());
91
92 if (nBytesToSkip < 0)
93 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
94
95 m_nActPos += nBytesToSkip;
96}
97
99{
100 std::scoped_lock aGuard( m_aMutex );
101 if (!m_xLockBytes.is())
102 throw css::io::NotConnectedException(OUString(), getXWeak());
103
104 return m_nAvailable;
105}
106
108{
109 std::scoped_lock aGuard( m_aMutex );
110 if (!m_xLockBytes.is())
111 throw css::io::NotConnectedException(OUString(), getXWeak());
112
113 m_xLockBytes = nullptr;
114}
115
116void SAL_CALL OInputStreamHelper::acquire() SAL_NOEXCEPT
117{
118 cppu::WeakImplHelper<css::io::XInputStream, css::io::XSeekable>::acquire();
119}
120
121} // namespace utl
122
123/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool is() const
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
virtual void SAL_CALL acquire() SAL_NOEXCEPT override
virtual void SAL_CALL closeInput() override
virtual sal_Int64 SAL_CALL getLength() override
SvLockBytesRef m_xLockBytes
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
virtual sal_Int32 SAL_CALL available() override
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
virtual void SAL_CALL seek(sal_Int64 location) override
virtual sal_Int64 SAL_CALL getPosition() override
#define ERRCODE_NONE
constexpr OUStringLiteral aData
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
std::size_t nSize