| 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 <osl/diagnose.h> |
| 26 |
|
| 27 |
#include "filinpstr.hxx" |
| 28 |
#include "filerror.hxx" |
| 29 |
|
| 30 |
using namespace fileaccess; |
| 31 |
using namespace com::sun::star; |
| 32 |
|
| 33 |
#if OSL_DEBUG_LEVEL > 0 |
| 34 |
#define THROW_WHERE SAL_WHERE |
| 35 |
#else |
| 36 |
#define THROW_WHERE "" |
| 37 |
#endif |
| 38 |
|
| 39 |
XInputStream_impl::XInputStream_impl( const OUString& aUncPath, bool bLock ) |
| 40 |
: m_aFile( aUncPath ), |
| 41 |
m_nErrorCode( TaskHandlerErr::NO_ERROR ), |
| 42 |
m_nMinorErrorCode( 0 ) |
| 43 |
{ |
| 44 |
sal_uInt32 nFlags = osl_File_OpenFlag_Read; |
| 45 |
if ( !bLock ) |
| 46 |
nFlags |= osl_File_OpenFlag_NoLock; |
| 47 |
|
| 48 |
osl::FileBase::RC err = m_aFile.open( nFlags ); |
| 49 |
if( err != osl::FileBase::E_None ) |
| 50 |
{ |
| 51 |
m_nIsOpen = false; |
| 52 |
m_aFile.close(); |
| 53 |
|
| 54 |
m_nErrorCode = TaskHandlerErr::OPEN_FOR_INPUTSTREAM; |
| 55 |
m_nMinorErrorCode = err; |
| 56 |
} |
| 57 |
else |
| 58 |
m_nIsOpen = true; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
XInputStream_impl::~XInputStream_impl() |
| 63 |
{ |
| 64 |
try |
| 65 |
{ |
| 66 |
closeInput(); |
| 67 |
} |
| 68 |
catch (io::IOException const &) |
| 69 |
{ |
| 70 |
OSL_FAIL("unexpected situation"); |
| 71 |
} |
| 72 |
catch (uno::RuntimeException const &) |
| 73 |
{ |
| 74 |
OSL_FAIL("unexpected situation"); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
sal_Int32 SAL_CALL |
| 79 |
XInputStream_impl::readBytes( |
| 80 |
uno::Sequence< sal_Int8 >& aData, |
| 81 |
sal_Int32 nBytesToRead ) |
| 82 |
{ |
| 83 |
if( ! m_nIsOpen ) throw io::IOException( THROW_WHERE ); |
| 84 |
|
| 85 |
aData.realloc(nBytesToRead); |
| 86 |
//TODO! translate memory exhaustion (if it were detectable...) into |
| 87 |
// io::BufferSizeExceededException |
| 88 |
|
| 89 |
sal_uInt64 nrc(0); |
| 90 |
if(m_aFile.read( aData.getArray(),sal_uInt64(nBytesToRead),nrc ) |
| 91 |
!= osl::FileBase::E_None) |
| 92 |
throw io::IOException( THROW_WHERE ); |
| 93 |
|
| 94 |
// Shrink aData in case we read less than nBytesToRead (XInputStream |
| 95 |
// documentation does not tell whether this is required, and I do not know |
| 96 |
// if any code relies on this, so be conservative---SB): |
| 97 |
if (sal::static_int_cast<sal_Int32>(nrc) != nBytesToRead) |
| 98 |
aData.realloc(sal_Int32(nrc)); |
| 99 |
return static_cast<sal_Int32>(nrc); |
| 100 |
} |
| 101 |
|
| 102 |
sal_Int32 |
| 103 |
XInputStream_impl::readSomeBytes( |
| 104 |
sal_Int8* aData, |
| 105 |
sal_Int32 nBytesToRead ) |
| 106 |
{ |
| 107 |
if( ! m_nIsOpen ) throw io::IOException( THROW_WHERE ); |
| 108 |
|
| 109 |
//TODO! translate memory exhaustion (if it were detectable...) into |
| 110 |
// io::BufferSizeExceededException |
| 111 |
|
| 112 |
sal_uInt64 nrc(0); |
| 113 |
if(m_aFile.read( aData, sal_uInt64(nBytesToRead),nrc ) |
| 114 |
!= osl::FileBase::E_None) |
| 115 |
throw io::IOException( THROW_WHERE ); |
| 116 |
|
| 117 |
return static_cast<sal_Int32>(nrc); |
| 118 |
} |
| 119 |
|
| 120 |
sal_Int32 SAL_CALL |
| 121 |
XInputStream_impl::readSomeBytes( |
| 122 |
uno::Sequence< sal_Int8 >& aData, |
| 123 |
sal_Int32 nMaxBytesToRead ) |
| 124 |
{ |
| 125 |
return readBytes( aData,nMaxBytesToRead ); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
void SAL_CALL |
| 130 |
XInputStream_impl::skipBytes( sal_Int32 nBytesToSkip ) |
| 131 |
{ |
| 132 |
m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) ); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
sal_Int32 SAL_CALL |
| 137 |
XInputStream_impl::available() |
| 138 |
{ |
| 139 |
sal_Int64 avail = getLength() - getPosition(); |
| 140 |
return std::min<sal_Int64>(avail, SAL_MAX_INT32); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
void SAL_CALL |
| 145 |
XInputStream_impl::closeInput() |
| 146 |
{ |
| 147 |
if( m_nIsOpen ) |
| 148 |
{ |
| 149 |
osl::FileBase::RC err = m_aFile.close(); |
| 150 |
if( err != osl::FileBase::E_None ) |
| 151 |
throw io::IOException( THROW_WHERE ); |
| 152 |
m_nIsOpen = false; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
void SAL_CALL |
| 158 |
XInputStream_impl::seek( sal_Int64 location ) |
| 159 |
{ |
| 160 |
if( location < 0 ) |
| 161 |
throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 0 ); |
| 162 |
if( osl::FileBase::E_None != m_aFile.setPos( osl_Pos_Absolut, sal_uInt64( location ) ) ) |
| 163 |
throw io::IOException( THROW_WHERE ); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
sal_Int64 SAL_CALL |
| 168 |
XInputStream_impl::getPosition() |
| 169 |
{ |
| 170 |
sal_uInt64 uPos; |
| 171 |
if( osl::FileBase::E_None != m_aFile.getPos( uPos ) ) |
| 172 |
throw io::IOException( THROW_WHERE ); |
| 173 |
return sal_Int64( uPos ); |
| 174 |
} |
| 175 |
|
| 176 |
sal_Int64 SAL_CALL |
| 177 |
XInputStream_impl::getLength() |
| 178 |
{ |
| 179 |
sal_uInt64 uEndPos; |
| 180 |
if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None ) |
| 181 |
throw io::IOException( THROW_WHERE ); |
| 182 |
return sal_Int64( uEndPos ); |
| 183 |
} |
| 184 |
|
| 185 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |