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