Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / ucb / source / ucp / file / filstr.cxx
blob85222141d5a31d5db553d9ad4678efffe407b664
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <com/sun/star/io/BufferSizeExceededException.hpp>
23 #include <com/sun/star/io/IOException.hpp>
24 #include <com/sun/star/lang/IllegalArgumentException.hpp>
25 #include <com/sun/star/uno/RuntimeException.hpp>
26 #include <osl/diagnose.h>
27 #include "filstr.hxx"
28 #include "filerror.hxx"
30 using namespace fileaccess;
31 using namespace com::sun::star;
33 #if OSL_DEBUG_LEVEL > 0
34 #define THROW_WHERE SAL_WHERE
35 #else
36 #define THROW_WHERE ""
37 #endif
39 /******************************************************************************/
40 /* */
41 /* XStream_impl implementation */
42 /* */
43 /******************************************************************************/
45 XStream_impl::XStream_impl( const OUString& aUncPath, bool bLock )
46 : m_bInputStreamCalled( false ),
47 m_bOutputStreamCalled( false ),
48 m_aFile( aUncPath ),
49 m_nErrorCode( TASKHANDLER_NO_ERROR ),
50 m_nMinorErrorCode( TASKHANDLER_NO_ERROR )
52 sal_uInt32 nFlags = ( osl_File_OpenFlag_Read | osl_File_OpenFlag_Write );
53 if ( !bLock )
54 nFlags |= osl_File_OpenFlag_NoLock;
56 osl::FileBase::RC err = m_aFile.open( nFlags );
57 if( err != osl::FileBase::E_None )
59 m_nIsOpen = false;
60 m_aFile.close();
62 m_nErrorCode = TASKHANDLING_OPEN_FOR_STREAM;
63 m_nMinorErrorCode = err;
65 else
66 m_nIsOpen = true;
70 XStream_impl::~XStream_impl()
72 try
74 closeStream();
76 catch (const io::IOException&)
78 OSL_FAIL("unexpected situation");
80 catch (const uno::RuntimeException&)
82 OSL_FAIL("unexpected situation");
87 uno::Reference< io::XInputStream > SAL_CALL
88 XStream_impl::getInputStream( )
91 std::scoped_lock aGuard( m_aMutex );
92 m_bInputStreamCalled = true;
94 return uno::Reference< io::XInputStream >( this );
98 uno::Reference< io::XOutputStream > SAL_CALL
99 XStream_impl::getOutputStream( )
102 std::scoped_lock aGuard( m_aMutex );
103 m_bOutputStreamCalled = true;
105 return uno::Reference< io::XOutputStream >( this );
109 void SAL_CALL XStream_impl::truncate()
111 if (osl::FileBase::E_None != m_aFile.setSize(0))
112 throw io::IOException( THROW_WHERE );
114 if (osl::FileBase::E_None != m_aFile.setPos(osl_Pos_Absolut,sal_uInt64(0)))
115 throw io::IOException( THROW_WHERE );
119 // XStream_impl private non interface methods
122 sal_Int32 SAL_CALL
123 XStream_impl::readBytes(
124 uno::Sequence< sal_Int8 >& aData,
125 sal_Int32 nBytesToRead )
127 if( ! m_nIsOpen )
128 throw io::IOException( THROW_WHERE );
132 aData.realloc(nBytesToRead);
134 catch (const std::bad_alloc&)
136 if( m_nIsOpen ) m_aFile.close();
137 throw io::BufferSizeExceededException( THROW_WHERE );
140 sal_uInt64 nrc(0);
141 if(m_aFile.read( aData.getArray(), sal_uInt64(nBytesToRead), nrc )
142 != osl::FileBase::E_None)
144 throw io::IOException( THROW_WHERE );
146 if (nrc != static_cast<sal_uInt64>(nBytesToRead))
147 aData.realloc(nrc);
148 return static_cast<sal_Int32>(nrc);
151 sal_Int32
152 XStream_impl::readSomeBytes(
153 sal_Int8* pData,
154 sal_Int32 nBytesToRead )
156 if( ! m_nIsOpen )
157 throw io::IOException( THROW_WHERE );
159 sal_uInt64 nrc(0);
160 if(m_aFile.read( pData, sal_uInt64(nBytesToRead), nrc )
161 != osl::FileBase::E_None)
163 throw io::IOException( THROW_WHERE );
165 return static_cast<sal_Int32>(nrc);
168 sal_Int32 SAL_CALL
169 XStream_impl::readSomeBytes(
170 uno::Sequence< sal_Int8 >& aData,
171 sal_Int32 nMaxBytesToRead )
173 return readBytes( aData,nMaxBytesToRead );
177 void SAL_CALL
178 XStream_impl::skipBytes( sal_Int32 nBytesToSkip )
180 m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) );
184 sal_Int32 SAL_CALL
185 XStream_impl::available()
187 sal_Int64 avail = getLength() - getPosition();
188 return std::min<sal_Int64>(avail, SAL_MAX_INT32);
192 void SAL_CALL
193 XStream_impl::writeBytes( const uno::Sequence< sal_Int8 >& aData )
195 sal_uInt32 length = aData.getLength();
196 if(length)
198 sal_uInt64 nWrittenBytes(0);
199 const sal_Int8* p = aData.getConstArray();
200 if(osl::FileBase::E_None != m_aFile.write(static_cast<void const *>(p),sal_uInt64(length),nWrittenBytes) ||
201 nWrittenBytes != length )
202 throw io::IOException( THROW_WHERE );
207 void
208 XStream_impl::closeStream()
210 if( m_nIsOpen )
212 osl::FileBase::RC err = m_aFile.close();
214 if( err != osl::FileBase::E_None ) {
215 io::IOException ex;
216 ex.Message = "could not close file";
217 throw ex;
220 m_nIsOpen = false;
224 void SAL_CALL
225 XStream_impl::closeInput()
227 std::scoped_lock aGuard( m_aMutex );
228 m_bInputStreamCalled = false;
230 if( ! m_bOutputStreamCalled )
231 closeStream();
235 void SAL_CALL
236 XStream_impl::closeOutput()
238 std::scoped_lock aGuard( m_aMutex );
239 m_bOutputStreamCalled = false;
241 if( ! m_bInputStreamCalled )
242 closeStream();
246 void SAL_CALL
247 XStream_impl::seek( sal_Int64 location )
249 if( location < 0 )
250 throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 0 );
251 if( osl::FileBase::E_None != m_aFile.setPos( osl_Pos_Absolut, sal_uInt64( location ) ) )
252 throw io::IOException( THROW_WHERE );
256 sal_Int64 SAL_CALL
257 XStream_impl::getPosition()
259 sal_uInt64 uPos;
260 if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
261 throw io::IOException( THROW_WHERE );
262 return sal_Int64( uPos );
265 sal_Int64 SAL_CALL
266 XStream_impl::getLength()
268 sal_uInt64 uEndPos;
269 if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None )
270 throw io::IOException( THROW_WHERE );
271 return sal_Int64( uEndPos );
274 void SAL_CALL
275 XStream_impl::flush()
278 void XStream_impl::waitForCompletion()
280 // At least on UNIX, to reliably learn about any errors encountered by
281 // asynchronous NFS write operations, without closing the file directly
282 // afterwards, there appears to be no cheaper way than to call fsync:
283 if (m_nIsOpen && m_aFile.sync() != osl::FileBase::E_None) {
284 throw io::IOException(
285 "could not synchronize file to disc",
286 static_cast< OWeakObject * >(this));
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */