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 <unotools/streamhelper.hxx>
26
27 namespace utl
28 {
29
readBytes(css::uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)30 sal_Int32 SAL_CALL OInputStreamHelper::readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
31 {
32 if (!m_xLockBytes.is())
33 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
34
35 if (nBytesToRead < 0)
36 throw css::io::BufferSizeExceededException(OUString(), static_cast<css::uno::XWeak*>(this));
37
38 ::osl::MutexGuard aGuard( m_aMutex );
39 if (aData.getLength() < nBytesToRead)
40 aData.realloc(nBytesToRead);
41
42 std::size_t nRead(0);
43 ErrCode nError = m_xLockBytes->ReadAt(m_nActPos, static_cast<void*>(aData.getArray()), nBytesToRead, &nRead);
44 m_nActPos += nRead;
45
46 if (nError != ERRCODE_NONE)
47 throw css::io::IOException(OUString(), static_cast<css::uno::XWeak*>(this));
48
49 // adjust sequence if data read is lower than the desired data
50 if (nRead < static_cast<std::size_t>(aData.getLength()))
51 aData.realloc( nRead );
52
53 return nRead;
54 }
55
seek(sal_Int64 location)56 void SAL_CALL OInputStreamHelper::seek( sal_Int64 location )
57 {
58 ::osl::MutexGuard aGuard( m_aMutex );
59 m_nActPos = location;
60 }
61
getPosition()62 sal_Int64 SAL_CALL OInputStreamHelper::getPosition( )
63 {
64 return m_nActPos;
65 }
66
getLength()67 sal_Int64 SAL_CALL OInputStreamHelper::getLength( )
68 {
69 if (!m_xLockBytes.is())
70 return 0;
71
72 ::osl::MutexGuard aGuard( m_aMutex );
73 SvLockBytesStat aStat;
74 m_xLockBytes->Stat( &aStat );
75 return aStat.nSize;
76 }
77
readSomeBytes(css::uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)78 sal_Int32 SAL_CALL OInputStreamHelper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData,
79 sal_Int32 nMaxBytesToRead)
80 {
81 // read all data desired
82 return readBytes(aData, nMaxBytesToRead);
83 }
84
skipBytes(sal_Int32 nBytesToSkip)85 void SAL_CALL OInputStreamHelper::skipBytes(sal_Int32 nBytesToSkip)
86 {
87 ::osl::MutexGuard aGuard( m_aMutex );
88 if (!m_xLockBytes.is())
89 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
90
91 if (nBytesToSkip < 0)
92 throw css::io::BufferSizeExceededException(OUString(), static_cast<css::uno::XWeak*>(this));
93
94 m_nActPos += nBytesToSkip;
95 }
96
available()97 sal_Int32 SAL_CALL OInputStreamHelper::available()
98 {
99 ::osl::MutexGuard aGuard( m_aMutex );
100 if (!m_xLockBytes.is())
101 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
102
103 return m_nAvailable;
104 }
105
closeInput()106 void SAL_CALL OInputStreamHelper::closeInput()
107 {
108 ::osl::MutexGuard aGuard( m_aMutex );
109 if (!m_xLockBytes.is())
110 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
111
112 m_xLockBytes = nullptr;
113 }
114
115 } // namespace utl
116
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
118