Branch libreoffice-5-0-4
[LibreOffice.git] / ucbhelper / source / provider / std_inputstream.cxx
blob513b9db9f53b7cb5ca1270181b487a35a1778f0c
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/.
8 */
10 #include <sal/config.h>
12 #include <sal/log.hxx>
14 #include "ucbhelper/std_inputstream.hxx"
16 using namespace std;
17 using namespace com::sun::star;
19 namespace ucbhelper
21 StdInputStream::StdInputStream( boost::shared_ptr< istream > pStream ) :
22 m_pStream( pStream ),
23 m_nLength( 0 )
25 if ( m_pStream.get() )
27 streampos nInitPos = m_pStream->tellg( );
28 m_pStream->seekg( 0, ios_base::end );
29 streampos nEndPos = m_pStream->tellg( );
30 m_pStream->seekg( nInitPos, ios_base::beg );
32 m_nLength = sal_Int64( nEndPos - nInitPos );
36 StdInputStream::~StdInputStream()
40 uno::Any SAL_CALL StdInputStream::queryInterface( const uno::Type& rType ) throw ( uno::RuntimeException, std::exception )
42 uno::Any aRet = ::cppu::queryInterface( rType,
43 ( static_cast< XInputStream* >( this ) ),
44 ( static_cast< XSeekable* >( this ) ) );
46 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
49 void SAL_CALL StdInputStream::acquire( ) throw( )
51 OWeakObject::acquire();
54 void SAL_CALL StdInputStream::release( ) throw( )
56 OWeakObject::release();
59 sal_Int32 SAL_CALL StdInputStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
60 throw( io::NotConnectedException, io::BufferSizeExceededException,
61 io::IOException, uno::RuntimeException, std::exception)
63 osl::MutexGuard aGuard( m_aMutex );
65 if ( 0 <= nBytesToRead && aData.getLength() < nBytesToRead )
66 aData.realloc( nBytesToRead );
68 if ( !m_pStream.get() )
69 throw io::IOException( );
71 sal_Int32 nRead = 0;
72 try
74 m_pStream->read( reinterpret_cast< char* >( aData.getArray( ) ), nBytesToRead );
75 nRead = m_pStream->gcount();
77 catch ( const ios_base::failure& e )
79 SAL_INFO( "ucbhelper", "StdInputStream::readBytes() error: " << e.what() );
80 throw io::IOException( );
83 return nRead;
86 sal_Int32 SAL_CALL StdInputStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData,
87 sal_Int32 nMaxBytesToRead )
88 throw( io::NotConnectedException, io::BufferSizeExceededException,
89 io::IOException, uno::RuntimeException, std::exception)
91 osl::MutexGuard aGuard( m_aMutex );
93 if ( 0 <= nMaxBytesToRead && aData.getLength() < nMaxBytesToRead )
94 aData.realloc( nMaxBytesToRead );
96 if ( !m_pStream.get() )
97 throw io::IOException( );
99 sal_Int32 nRead = 0;
102 nRead = m_pStream->readsome( reinterpret_cast< char* >( aData.getArray( ) ), nMaxBytesToRead );
104 catch ( const ios_base::failure& e )
106 SAL_INFO( "ucbhelper", "StdInputStream::readBytes() error: " << e.what() );
107 throw io::IOException( );
109 return nRead;
112 void SAL_CALL StdInputStream::skipBytes( sal_Int32 nBytesToSkip )
113 throw( io::NotConnectedException, io::BufferSizeExceededException,
114 io::IOException, uno::RuntimeException, std::exception )
116 osl::MutexGuard aGuard( m_aMutex );
118 if ( !m_pStream.get() )
119 throw io::IOException( );
123 m_pStream->seekg( nBytesToSkip, ios_base::cur );
125 catch ( const ios_base::failure& e )
127 SAL_INFO( "ucbhelper", "StdInputStream::readBytes() error: " << e.what() );
128 throw io::IOException( );
132 sal_Int32 SAL_CALL StdInputStream::available( )
133 throw(io::NotConnectedException, io::IOException, uno::RuntimeException, std::exception )
135 return sal::static_int_cast< sal_Int32 >( m_nLength - getPosition() );
138 void SAL_CALL StdInputStream::closeInput( )
139 throw( io::NotConnectedException, io::IOException, uno::RuntimeException, std::exception)
141 // No need to implement this for an istream
144 void SAL_CALL StdInputStream::seek( sal_Int64 location )
145 throw( lang::IllegalArgumentException, io::IOException, uno::RuntimeException, std::exception )
147 osl::MutexGuard aGuard( m_aMutex );
149 if ( location < 0 || location > m_nLength )
150 throw lang::IllegalArgumentException(
151 "Location can't be negative or greater than the length",
152 static_cast< cppu::OWeakObject* >( this ), 0 );
154 if ( !m_pStream.get() )
155 throw io::IOException( );
159 m_pStream->clear( ); // may be needed to rewind the stream
160 m_pStream->seekg( location, ios_base::beg );
162 catch ( const ios_base::failure& e )
164 SAL_INFO( "ucbhelper", "StdInputStream::readBytes() error: " << e.what() );
165 throw io::IOException( );
169 sal_Int64 SAL_CALL StdInputStream::getPosition( )
170 throw( io::IOException, uno::RuntimeException, std::exception )
172 osl::MutexGuard aGuard( m_aMutex );
174 if ( !m_pStream.get() )
175 throw io::IOException( );
177 sal_Int64 nPos = m_pStream->tellg( );
178 if ( -1 == nPos )
179 throw io::IOException( );
181 return nPos;
184 sal_Int64 SAL_CALL StdInputStream::getLength( )
185 throw ( io::IOException, uno::RuntimeException, std::exception )
187 return m_nLength;
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */