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 "ucbhelper/std_inputstream.hxx"
13 using namespace com::sun::star
;
17 StdInputStream::StdInputStream( boost::shared_ptr
< istream
> pStream
) :
21 if ( m_pStream
.get() )
23 streampos nInitPos
= m_pStream
->tellg( );
24 m_pStream
->seekg( 0, ios_base::end
);
25 streampos nEndPos
= m_pStream
->tellg( );
26 m_pStream
->seekg( nInitPos
, ios_base::beg
);
28 m_nLength
= sal_Int64( nEndPos
- nInitPos
);
32 StdInputStream::~StdInputStream()
36 uno::Any SAL_CALL
StdInputStream::queryInterface( const uno::Type
& rType
) throw ( uno::RuntimeException
)
38 uno::Any aRet
= ::cppu::queryInterface( rType
,
39 ( static_cast< XInputStream
* >( this ) ),
40 ( static_cast< XSeekable
* >( this ) ) );
42 return aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
);
45 void SAL_CALL
StdInputStream::acquire( ) throw( )
47 OWeakObject::acquire();
50 void SAL_CALL
StdInputStream::release( ) throw( )
52 OWeakObject::release();
55 sal_Int32 SAL_CALL
StdInputStream::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
56 throw( io::NotConnectedException
, io::BufferSizeExceededException
,
57 io::IOException
, uno::RuntimeException
)
59 osl::MutexGuard
aGuard( m_aMutex
);
61 if ( 0 <= nBytesToRead
&& aData
.getLength() < nBytesToRead
)
62 aData
.realloc( nBytesToRead
);
64 if ( !m_pStream
.get() )
65 throw io::IOException( );
70 m_pStream
->read( reinterpret_cast< char* >( aData
.getArray( ) ), nBytesToRead
);
71 nRead
= m_pStream
->gcount();
73 catch ( const ios_base::failure
& e
)
75 SAL_INFO( "ucbhelper", "StdInputStream::readBytes() error: " << e
.what() );
76 throw io::IOException( );
82 sal_Int32 SAL_CALL
StdInputStream::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
,
83 sal_Int32 nMaxBytesToRead
)
84 throw( io::NotConnectedException
, io::BufferSizeExceededException
,
85 io::IOException
, uno::RuntimeException
)
87 osl::MutexGuard
aGuard( m_aMutex
);
89 if ( 0 <= nMaxBytesToRead
&& aData
.getLength() < nMaxBytesToRead
)
90 aData
.realloc( nMaxBytesToRead
);
92 if ( !m_pStream
.get() )
93 throw io::IOException( );
98 nRead
= m_pStream
->readsome( reinterpret_cast< char* >( aData
.getArray( ) ), nMaxBytesToRead
);
100 catch ( const ios_base::failure
& e
)
102 SAL_INFO( "ucbhelper", "StdInputStream::readBytes() error: " << e
.what() );
103 throw io::IOException( );
108 void SAL_CALL
StdInputStream::skipBytes( sal_Int32 nBytesToSkip
)
109 throw( io::NotConnectedException
, io::BufferSizeExceededException
,
110 io::IOException
, uno::RuntimeException
)
112 osl::MutexGuard
aGuard( m_aMutex
);
114 if ( !m_pStream
.get() )
115 throw io::IOException( );
119 m_pStream
->seekg( nBytesToSkip
, ios_base::cur
);
121 catch ( const ios_base::failure
& e
)
123 SAL_INFO( "ucbhelper", "StdInputStream::readBytes() error: " << e
.what() );
124 throw io::IOException( );
128 sal_Int32 SAL_CALL
StdInputStream::available( )
129 throw(io::NotConnectedException
, io::IOException
, uno::RuntimeException
)
131 return sal::static_int_cast
< sal_Int32
>( m_nLength
- getPosition() );
134 void SAL_CALL
StdInputStream::closeInput( )
135 throw( io::NotConnectedException
, io::IOException
, uno::RuntimeException
)
137 // No need to implement this for an istream
140 void SAL_CALL
StdInputStream::seek( sal_Int64 location
)
141 throw( lang::IllegalArgumentException
, io::IOException
, uno::RuntimeException
)
143 osl::MutexGuard
aGuard( m_aMutex
);
145 if ( location
< 0 || location
> m_nLength
)
146 throw lang::IllegalArgumentException(
147 "Location can't be negative or greater than the length",
148 static_cast< cppu::OWeakObject
* >( this ), 0 );
150 if ( !m_pStream
.get() )
151 throw io::IOException( );
155 m_pStream
->clear( ); // may be needed to rewind the stream
156 m_pStream
->seekg( location
, ios_base::beg
);
158 catch ( const ios_base::failure
& e
)
160 SAL_INFO( "ucbhelper", "StdInputStream::readBytes() error: " << e
.what() );
161 throw io::IOException( );
165 sal_Int64 SAL_CALL
StdInputStream::getPosition( )
166 throw( io::IOException
, uno::RuntimeException
)
168 osl::MutexGuard
aGuard( m_aMutex
);
170 if ( !m_pStream
.get() )
171 throw io::IOException( );
173 sal_Int64 nPos
= m_pStream
->tellg( );
175 throw io::IOException( );
180 sal_Int64 SAL_CALL
StdInputStream::getLength( )
181 throw ( io::IOException
, uno::RuntimeException
)
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */