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>
26 #include <unotools/streamhelper.hxx>
27
28 namespace utl
29 {
30
readBytes(css::uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)31 sal_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(), static_cast<css::uno::XWeak*>(this));
35
36 if (nBytesToRead < 0)
37 throw css::io::BufferSizeExceededException(OUString(), static_cast<css::uno::XWeak*>(this));
38
39 ::osl::MutexGuard 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(), static_cast<css::uno::XWeak*>(this));
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
seek(sal_Int64 location)57 void SAL_CALL OInputStreamHelper::seek( sal_Int64 location )
58 {
59 ::osl::MutexGuard aGuard( m_aMutex );
60 m_nActPos = location;
61 }
62
getPosition()63 sal_Int64 SAL_CALL OInputStreamHelper::getPosition( )
64 {
65 return m_nActPos;
66 }
67
getLength()68 sal_Int64 SAL_CALL OInputStreamHelper::getLength( )
69 {
70 if (!m_xLockBytes.is())
71 return 0;
72
73 ::osl::MutexGuard aGuard( m_aMutex );
74 SvLockBytesStat aStat;
75 m_xLockBytes->Stat( &aStat );
76 return aStat.nSize;
77 }
78
readSomeBytes(css::uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)79 sal_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
skipBytes(sal_Int32 nBytesToSkip)86 void SAL_CALL OInputStreamHelper::skipBytes(sal_Int32 nBytesToSkip)
87 {
88 ::osl::MutexGuard aGuard( m_aMutex );
89 if (!m_xLockBytes.is())
90 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
91
92 if (nBytesToSkip < 0)
93 throw css::io::BufferSizeExceededException(OUString(), static_cast<css::uno::XWeak*>(this));
94
95 m_nActPos += nBytesToSkip;
96 }
97
available()98 sal_Int32 SAL_CALL OInputStreamHelper::available()
99 {
100 ::osl::MutexGuard aGuard( m_aMutex );
101 if (!m_xLockBytes.is())
102 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
103
104 return m_nAvailable;
105 }
106
closeInput()107 void SAL_CALL OInputStreamHelper::closeInput()
108 {
109 ::osl::MutexGuard aGuard( m_aMutex );
110 if (!m_xLockBytes.is())
111 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
112
113 m_xLockBytes = nullptr;
114 }
115
116 } // namespace utl
117
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
119