1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <sal/config.h>
12 #include <com/sun/star/io/IOException.hpp>
13 #include <com/sun/star/lang/IllegalArgumentException.hpp>
14 #include <sal/log.hxx>
15 #include <cppuhelper/queryinterface.hxx>
18 #include "std_inputstream.hxx"
20 using namespace com::sun::star
;
24 StdInputStream::StdInputStream( boost::shared_ptr
< std::istream
> pStream
) :
25 m_pStream(std::move( pStream
)),
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
);
35 m_nLength
= sal_Int64( nEndPos
- nInitPos
);
39 StdInputStream::~StdInputStream()
43 uno::Any SAL_CALL
StdInputStream::queryInterface( const uno::Type
& rType
)
45 uno::Any aRet
= ::cppu::queryInterface( rType
,
46 static_cast< XInputStream
* >( this ),
47 static_cast< XSeekable
* >( this ) );
49 return aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
);
52 void SAL_CALL
StdInputStream::acquire( ) noexcept
54 OWeakObject::acquire();
57 void SAL_CALL
StdInputStream::release( ) noexcept
59 OWeakObject::release();
62 sal_Int32 SAL_CALL
StdInputStream::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
64 std::scoped_lock
aGuard( m_aMutex
);
66 if ( 0 <= nBytesToRead
&& aData
.getLength() < nBytesToRead
)
67 aData
.realloc( nBytesToRead
);
70 throw io::IOException( );
75 m_pStream
->read( reinterpret_cast< char* >( aData
.getArray( ) ), nBytesToRead
);
76 nRead
= m_pStream
->gcount();
78 catch ( const std::ios_base::failure
& e
)
80 SAL_INFO( "ucb.ucp.cmis", "StdInputStream::readBytes() error: " << e
.what() );
81 throw io::IOException( );
87 sal_Int32 SAL_CALL
StdInputStream::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
,
88 sal_Int32 nMaxBytesToRead
)
90 std::scoped_lock
aGuard( m_aMutex
);
92 if ( 0 <= nMaxBytesToRead
&& aData
.getLength() < nMaxBytesToRead
)
93 aData
.realloc( nMaxBytesToRead
);
96 throw io::IOException( );
101 nRead
= m_pStream
->readsome( reinterpret_cast< char* >( aData
.getArray( ) ), nMaxBytesToRead
);
103 catch ( const std::ios_base::failure
& e
)
105 SAL_INFO( "ucb.ucp.cmis", "StdInputStream::readBytes() error: " << e
.what() );
106 throw io::IOException( );
111 void SAL_CALL
StdInputStream::skipBytes( sal_Int32 nBytesToSkip
)
113 std::scoped_lock
aGuard( m_aMutex
);
116 throw io::IOException( );
120 m_pStream
->seekg( nBytesToSkip
, std::ios_base::cur
);
122 catch ( const std::ios_base::failure
& e
)
124 SAL_INFO( "ucb.ucp.cmis", "StdInputStream::readBytes() error: " << e
.what() );
125 throw io::IOException( );
129 sal_Int32 SAL_CALL
StdInputStream::available( )
131 return std::min
<sal_Int64
>( SAL_MAX_INT32
, m_nLength
- getPosition() );
134 void SAL_CALL
StdInputStream::closeInput( )
136 // No need to implement this for an istream
139 void SAL_CALL
StdInputStream::seek( sal_Int64 location
)
141 std::scoped_lock
aGuard( m_aMutex
);
143 if ( location
< 0 || location
> m_nLength
)
144 throw lang::IllegalArgumentException(
145 "Location can't be negative or greater than the length",
146 static_cast< cppu::OWeakObject
* >( this ), 0 );
149 throw io::IOException( );
153 m_pStream
->clear( ); // may be needed to rewind the stream
154 m_pStream
->seekg( location
, std::ios_base::beg
);
156 catch ( const std::ios_base::failure
& e
)
158 SAL_INFO( "ucb.ucp.cmis", "StdInputStream::readBytes() error: " << e
.what() );
159 throw io::IOException( );
163 sal_Int64 SAL_CALL
StdInputStream::getPosition( )
165 std::scoped_lock
aGuard( m_aMutex
);
168 throw io::IOException( );
170 sal_Int64 nPos
= m_pStream
->tellg( );
172 throw io::IOException( );
177 sal_Int64 SAL_CALL
StdInputStream::getLength( )
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */