LibreOffice Module ucb (master)  1
filinpstr.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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <com/sun/star/io/IOException.hpp>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 
25 #include "filinpstr.hxx"
26 #include "filerror.hxx"
27 
28 using namespace fileaccess;
29 using namespace com::sun::star;
30 
31 #if OSL_DEBUG_LEVEL > 0
32 #define THROW_WHERE SAL_WHERE
33 #else
34 #define THROW_WHERE ""
35 #endif
36 
37 XInputStream_impl::XInputStream_impl( const OUString& aUncPath, bool bLock )
38  : m_aFile( aUncPath ),
39  m_nErrorCode( TASKHANDLER_NO_ERROR ),
40  m_nMinorErrorCode( TASKHANDLER_NO_ERROR )
41 {
42  sal_uInt32 nFlags = osl_File_OpenFlag_Read;
43  if ( !bLock )
44  nFlags |= osl_File_OpenFlag_NoLock;
45 
46  osl::FileBase::RC err = m_aFile.open( nFlags );
47  if( err != osl::FileBase::E_None )
48  {
49  m_nIsOpen = false;
50  m_aFile.close();
51 
53  m_nMinorErrorCode = err;
54  }
55  else
56  m_nIsOpen = true;
57 }
58 
59 
61 {
62  try
63  {
64  closeInput();
65  }
66  catch (io::IOException const &)
67  {
68  OSL_FAIL("unexpected situation");
69  }
70  catch (uno::RuntimeException const &)
71  {
72  OSL_FAIL("unexpected situation");
73  }
74 }
75 
76 sal_Int32 SAL_CALL
78  uno::Sequence< sal_Int8 >& aData,
79  sal_Int32 nBytesToRead )
80 {
81  if( ! m_nIsOpen ) throw io::IOException( THROW_WHERE );
82 
83  aData.realloc(nBytesToRead);
84  //TODO! translate memory exhaustion (if it were detectable...) into
85  // io::BufferSizeExceededException
86 
87  sal_uInt64 nrc(0);
88  if(m_aFile.read( aData.getArray(),sal_uInt64(nBytesToRead),nrc )
89  != osl::FileBase::E_None)
90  throw io::IOException( THROW_WHERE );
91 
92  // Shrink aData in case we read less than nBytesToRead (XInputStream
93  // documentation does not tell whether this is required, and I do not know
94  // if any code relies on this, so be conservative---SB):
95  if (sal::static_int_cast<sal_Int32>(nrc) != nBytesToRead)
96  aData.realloc(sal_Int32(nrc));
97  return static_cast<sal_Int32>(nrc);
98 }
99 
100 sal_Int32 SAL_CALL
102  uno::Sequence< sal_Int8 >& aData,
103  sal_Int32 nMaxBytesToRead )
104 {
105  return readBytes( aData,nMaxBytesToRead );
106 }
107 
108 
109 void SAL_CALL
110 XInputStream_impl::skipBytes( sal_Int32 nBytesToSkip )
111 {
112  m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) );
113 }
114 
115 
116 sal_Int32 SAL_CALL
118 {
119  sal_Int64 avail = getLength() - getPosition();
120  return std::min<sal_Int64>(avail, SAL_MAX_INT32);
121 }
122 
123 
124 void SAL_CALL
126 {
127  if( m_nIsOpen )
128  {
129  osl::FileBase::RC err = m_aFile.close();
130  if( err != osl::FileBase::E_None )
131  throw io::IOException( THROW_WHERE );
132  m_nIsOpen = false;
133  }
134 }
135 
136 
137 void SAL_CALL
138 XInputStream_impl::seek( sal_Int64 location )
139 {
140  if( location < 0 )
141  throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 0 );
142  if( osl::FileBase::E_None != m_aFile.setPos( osl_Pos_Absolut, sal_uInt64( location ) ) )
143  throw io::IOException( THROW_WHERE );
144 }
145 
146 
147 sal_Int64 SAL_CALL
149 {
150  sal_uInt64 uPos;
151  if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
152  throw io::IOException( THROW_WHERE );
153  return sal_Int64( uPos );
154 }
155 
156 sal_Int64 SAL_CALL
158 {
159  sal_uInt64 uEndPos;
160  if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None )
161  throw io::IOException( THROW_WHERE );
162  return sal_Int64( uEndPos );
163 }
164 
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::osl::FileBase::RC getSize(sal_uInt64 &rSize)
Definition: filrec.cxx:143
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
virtual sal_Int32 SAL_CALL available() override
Definition: filinpstr.cxx:117
::osl::FileBase::RC close()
Definition: filrec.cxx:64
virtual sal_Int64 SAL_CALL getPosition() override
Definition: filinpstr.cxx:148
::osl::FileBase::RC getPos(sal_uInt64 &uPos)
Definition: filrec.cxx:104
err
#define SAL_MAX_INT32
#define TASKHANDLING_OPEN_FOR_INPUTSTREAM
Definition: filerror.hxx:47
virtual void SAL_CALL closeInput() override
Definition: filinpstr.cxx:125
::osl::FileBase::RC setPos(sal_uInt32 uHow, sal_Int64 uPos)
Definition: filrec.cxx:73
virtual void SAL_CALL seek(sal_Int64 location) override
Definition: filinpstr.cxx:138
#define THROW_WHERE
Definition: filinpstr.cxx:32
virtual sal_Int64 SAL_CALL getLength() override
Definition: filinpstr.cxx:157
ReconnectingFile m_aFile
Definition: filinpstr.hxx:81
virtual ~XInputStream_impl() override
Definition: filinpstr.cxx:60
::osl::FileBase::RC open(sal_uInt32 uFlags)
Definition: filrec.cxx:48
XInputStream_impl(const OUString &aUncPath, bool bLock)
Definition: filinpstr.cxx:37
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
Definition: filinpstr.cxx:110
#define TASKHANDLER_NO_ERROR
Definition: filerror.hxx:27
::osl::FileBase::RC read(void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64 &rBytesRead)
Definition: filrec.cxx:166
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override