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