Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / unotools / source / streaming / streamhelper.cxx
blob1b639fb04825025ec5997f5b29b9a24c5f78c7d2
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 <unotools/streamhelper.hxx>
27 namespace utl
30 sal_Int32 SAL_CALL OInputStreamHelper::readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
32 if (!m_xLockBytes.is())
33 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
35 if (nBytesToRead < 0)
36 throw css::io::BufferSizeExceededException(OUString(), static_cast<css::uno::XWeak*>(this));
38 ::osl::MutexGuard aGuard( m_aMutex );
39 if (aData.getLength() < nBytesToRead)
40 aData.realloc(nBytesToRead);
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;
46 if (nError != ERRCODE_NONE)
47 throw css::io::IOException(OUString(), static_cast<css::uno::XWeak*>(this));
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 );
53 return nRead;
56 void SAL_CALL OInputStreamHelper::seek( sal_Int64 location )
58 ::osl::MutexGuard aGuard( m_aMutex );
59 m_nActPos = location;
62 sal_Int64 SAL_CALL OInputStreamHelper::getPosition( )
64 return m_nActPos;
67 sal_Int64 SAL_CALL OInputStreamHelper::getLength( )
69 if (!m_xLockBytes.is())
70 return 0;
72 ::osl::MutexGuard aGuard( m_aMutex );
73 SvLockBytesStat aStat;
74 m_xLockBytes->Stat( &aStat );
75 return aStat.nSize;
78 sal_Int32 SAL_CALL OInputStreamHelper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData,
79 sal_Int32 nMaxBytesToRead)
81 // read all data desired
82 return readBytes(aData, nMaxBytesToRead);
85 void SAL_CALL OInputStreamHelper::skipBytes(sal_Int32 nBytesToSkip)
87 ::osl::MutexGuard aGuard( m_aMutex );
88 if (!m_xLockBytes.is())
89 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
91 if (nBytesToSkip < 0)
92 throw css::io::BufferSizeExceededException(OUString(), static_cast<css::uno::XWeak*>(this));
94 m_nActPos += nBytesToSkip;
97 sal_Int32 SAL_CALL OInputStreamHelper::available()
99 ::osl::MutexGuard aGuard( m_aMutex );
100 if (!m_xLockBytes.is())
101 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
103 return m_nAvailable;
106 void SAL_CALL OInputStreamHelper::closeInput()
108 ::osl::MutexGuard aGuard( m_aMutex );
109 if (!m_xLockBytes.is())
110 throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this));
112 m_xLockBytes = nullptr;
115 } // namespace utl
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */