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