LibreOffice Module ucb (master) 1
std_inputstream.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
10#include <sal/config.h>
11
12#include <com/sun/star/io/IOException.hpp>
13#include <com/sun/star/lang/IllegalArgumentException.hpp>
14#include <sal/log.hxx>
16#include <utility>
17
18#include "std_inputstream.hxx"
19
20using namespace com::sun::star;
21
22namespace cmis
23{
24 StdInputStream::StdInputStream( boost::shared_ptr< std::istream > pStream ) :
25 m_pStream(std::move( pStream )),
26 m_nLength( 0 )
27 {
28 if (m_pStream)
29 {
30 auto nInitPos = m_pStream->tellg( );
31 m_pStream->seekg( 0, std::ios_base::end );
32 auto nEndPos = m_pStream->tellg( );
33 m_pStream->seekg( nInitPos, std::ios_base::beg );
34
35 m_nLength = sal_Int64( nEndPos - nInitPos );
36 }
37 }
38
40 {
41 }
42
44 {
45 uno::Any aRet = ::cppu::queryInterface( rType,
46 static_cast< XInputStream* >( this ),
47 static_cast< XSeekable* >( this ) );
48
49 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
50 }
51
52 void SAL_CALL StdInputStream::acquire( ) noexcept
53 {
54 OWeakObject::acquire();
55 }
56
57 void SAL_CALL StdInputStream::release( ) noexcept
58 {
59 OWeakObject::release();
60 }
61
62 sal_Int32 SAL_CALL StdInputStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
63 {
64 std::scoped_lock aGuard( m_aMutex );
65
66 if ( 0 <= nBytesToRead && aData.getLength() < nBytesToRead )
67 aData.realloc( nBytesToRead );
68
69 if (!m_pStream)
70 throw io::IOException( );
71
72 sal_Int32 nRead = 0;
73 try
74 {
75 m_pStream->read( reinterpret_cast< char* >( aData.getArray( ) ), nBytesToRead );
76 nRead = m_pStream->gcount();
77 }
78 catch ( const std::ios_base::failure& e )
79 {
80 SAL_INFO( "ucb.ucp.cmis", "StdInputStream::readBytes() error: " << e.what() );
81 throw io::IOException( );
82 }
83
84 return nRead;
85 }
86
87 sal_Int32 SAL_CALL StdInputStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData,
88 sal_Int32 nMaxBytesToRead )
89 {
90 std::scoped_lock aGuard( m_aMutex );
91
92 if ( 0 <= nMaxBytesToRead && aData.getLength() < nMaxBytesToRead )
93 aData.realloc( nMaxBytesToRead );
94
95 if (!m_pStream)
96 throw io::IOException( );
97
98 sal_Int32 nRead = 0;
99 try
100 {
101 nRead = m_pStream->readsome( reinterpret_cast< char* >( aData.getArray( ) ), nMaxBytesToRead );
102 }
103 catch ( const std::ios_base::failure& e )
104 {
105 SAL_INFO( "ucb.ucp.cmis", "StdInputStream::readBytes() error: " << e.what() );
106 throw io::IOException( );
107 }
108 return nRead;
109 }
110
111 void SAL_CALL StdInputStream::skipBytes( sal_Int32 nBytesToSkip )
112 {
113 std::scoped_lock aGuard( m_aMutex );
114
115 if (!m_pStream)
116 throw io::IOException( );
117
118 try
119 {
120 m_pStream->seekg( nBytesToSkip, std::ios_base::cur );
121 }
122 catch ( const std::ios_base::failure& e )
123 {
124 SAL_INFO( "ucb.ucp.cmis", "StdInputStream::readBytes() error: " << e.what() );
125 throw io::IOException( );
126 }
127 }
128
129 sal_Int32 SAL_CALL StdInputStream::available( )
130 {
131 return std::min<sal_Int64>( SAL_MAX_INT32, m_nLength - getPosition() );
132 }
133
135 {
136 // No need to implement this for an istream
137 }
138
139 void SAL_CALL StdInputStream::seek( sal_Int64 location )
140 {
141 std::scoped_lock aGuard( m_aMutex );
142
143 if ( location < 0 || location > m_nLength )
144 throw lang::IllegalArgumentException(
145 "Location can't be negative or greater than the length",
146 getXWeak(), 0 );
147
148 if (!m_pStream)
149 throw io::IOException( );
150
151 try
152 {
153 m_pStream->clear( ); // may be needed to rewind the stream
154 m_pStream->seekg( location, std::ios_base::beg );
155 }
156 catch ( const std::ios_base::failure& e )
157 {
158 SAL_INFO( "ucb.ucp.cmis", "StdInputStream::readBytes() error: " << e.what() );
159 throw io::IOException( );
160 }
161 }
162
163 sal_Int64 SAL_CALL StdInputStream::getPosition( )
164 {
165 std::scoped_lock aGuard( m_aMutex );
166
167 if (!m_pStream)
168 throw io::IOException( );
169
170 sal_Int64 nPos = m_pStream->tellg( );
171 if ( -1 == nPos )
172 throw io::IOException( );
173
174 return nPos;
175 }
176
177 sal_Int64 SAL_CALL StdInputStream::getLength( )
178 {
179 return m_nLength;
180 }
181}
182
183/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const sal_Int32 m_nLength
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
virtual sal_Int64 SAL_CALL getPosition() override
virtual void SAL_CALL acquire() noexcept override
virtual void SAL_CALL release() noexcept override
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
boost::shared_ptr< std::istream > m_pStream
virtual sal_Int64 SAL_CALL getLength() override
virtual ~StdInputStream() override
virtual void SAL_CALL closeInput() override
StdInputStream(boost::shared_ptr< std::istream > pStream)
virtual sal_Int32 SAL_CALL available() override
virtual void SAL_CALL seek(sal_Int64 location) override
XSeekable.
GtkMediaStream * m_pStream
sal_uInt16 nPos
#define SAL_INFO(area, stream)
constexpr OUStringLiteral aData
bool hasValue()
#define SAL_MAX_INT32