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