Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / unotools / source / streaming / streamhelper.cxx
blob12f6ad7afd5acdafee15ea2457762c0944cb8bfc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <sal/config.h>
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>
26 #include <unotools/streamhelper.hxx>
28 namespace utl
31 sal_Int32 SAL_CALL OInputStreamHelper::readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
33 if (!m_xLockBytes.is())
34 throw css::io::NotConnectedException(OUString(), getXWeak());
36 if (nBytesToRead < 0)
37 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
39 std::scoped_lock aGuard( m_aMutex );
40 if (aData.getLength() < nBytesToRead)
41 aData.realloc(nBytesToRead);
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;
47 if (nError != ERRCODE_NONE)
48 throw css::io::IOException(OUString(), getXWeak());
50 // adjust sequence if data read is lower than the desired data
51 if (nRead < o3tl::make_unsigned(aData.getLength()))
52 aData.realloc( nRead );
54 return nRead;
57 void SAL_CALL OInputStreamHelper::seek( sal_Int64 location )
59 std::scoped_lock aGuard( m_aMutex );
60 m_nActPos = location;
63 sal_Int64 SAL_CALL OInputStreamHelper::getPosition( )
65 return m_nActPos;
68 sal_Int64 SAL_CALL OInputStreamHelper::getLength( )
70 if (!m_xLockBytes.is())
71 return 0;
73 std::scoped_lock aGuard( m_aMutex );
74 SvLockBytesStat aStat;
75 m_xLockBytes->Stat( &aStat );
76 return aStat.nSize;
79 sal_Int32 SAL_CALL OInputStreamHelper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData,
80 sal_Int32 nMaxBytesToRead)
82 // read all data desired
83 return readBytes(aData, nMaxBytesToRead);
86 void SAL_CALL OInputStreamHelper::skipBytes(sal_Int32 nBytesToSkip)
88 std::scoped_lock aGuard( m_aMutex );
89 if (!m_xLockBytes.is())
90 throw css::io::NotConnectedException(OUString(), getXWeak());
92 if (nBytesToSkip < 0)
93 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
95 m_nActPos += nBytesToSkip;
98 sal_Int32 SAL_CALL OInputStreamHelper::available()
100 std::scoped_lock aGuard( m_aMutex );
101 if (!m_xLockBytes.is())
102 throw css::io::NotConnectedException(OUString(), getXWeak());
104 return m_nAvailable;
107 void SAL_CALL OInputStreamHelper::closeInput()
109 std::scoped_lock aGuard( m_aMutex );
110 if (!m_xLockBytes.is())
111 throw css::io::NotConnectedException(OUString(), getXWeak());
113 m_xLockBytes = nullptr;
116 void SAL_CALL OInputStreamHelper::acquire() SAL_NOEXCEPT
118 cppu::WeakImplHelper<css::io::XInputStream, css::io::XSeekable>::acquire();
121 } // namespace utl
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */