lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / ucbhelper / source / provider / fd_inputstream.cxx
blobb34e39646a4f96f64859cc7ebd29170275ee574e
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 <ucbhelper/fd_inputstream.hxx>
22 #include <com/sun/star/io/IOException.hpp>
23 #include <rtl/alloc.h>
24 #include <osl/diagnose.h>
25 #include <sal/log.hxx>
26 #include <algorithm>
28 using namespace com::sun::star::uno;
29 using namespace com::sun::star::lang;
30 using namespace com::sun::star::io;
32 namespace ucbhelper
34 FdInputStream::FdInputStream( oslFileHandle tmpfl )
35 : m_tmpfl(tmpfl)
36 , m_nLength( 0 )
38 if ( !m_tmpfl )
39 osl_createTempFile( nullptr, &m_tmpfl, nullptr );
40 OSL_ENSURE( m_tmpfl, "input stream without tempfile!" );
42 if ( osl_setFilePos( m_tmpfl, osl_Pos_End, 0 ) == osl_File_E_None )
44 sal_uInt64 nFileSize = 0;
45 if ( osl_getFilePos( m_tmpfl, &nFileSize ) == osl_File_E_None )
46 m_nLength = nFileSize;
47 oslFileError rc = osl_setFilePos( m_tmpfl, osl_Pos_Absolut, 0 );
48 SAL_WARN_IF(rc != osl_File_E_None, "ucbhelper", "osl_setFilePos failed");
52 FdInputStream::~FdInputStream()
54 if ( nullptr != m_tmpfl)
55 osl_closeFile(m_tmpfl);
58 sal_Int32 SAL_CALL FdInputStream::readBytes(Sequence< sal_Int8 >& aData,
59 sal_Int32 nBytesToRead)
61 osl::MutexGuard aGuard(m_aMutex);
63 sal_uInt64 nBeforePos( 0 );
64 sal_uInt64 nBytesRequested( nBytesToRead );
65 sal_uInt64 nBytesRead( 0 );
67 osl_getFilePos( m_tmpfl, &nBeforePos );
69 if ( 0 == ( nBytesRequested = std::min< sal_uInt64 >( m_nLength - nBeforePos, nBytesRequested ) ) )
70 return 0;
72 if ( 0 <= nBytesToRead && aData.getLength() < nBytesToRead )
73 aData.realloc( nBytesToRead );
75 if ( osl_readFile( m_tmpfl, aData.getArray(), nBytesRequested, &nBytesRead ) != osl_File_E_None )
76 throw IOException();
78 return sal_Int32( nBytesRead );
82 sal_Int32 SAL_CALL FdInputStream::readSomeBytes( Sequence< sal_Int8 >& aData,
83 sal_Int32 nMaxBytesToRead )
85 return readBytes(aData,nMaxBytesToRead);
89 void SAL_CALL FdInputStream::skipBytes(sal_Int32 nBytesToSkip)
91 osl::MutexGuard aGuard(m_aMutex);
92 if(!m_tmpfl)
93 throw IOException();
95 oslFileError rc = osl_setFilePos( m_tmpfl, osl_Pos_Current, nBytesToSkip );
96 SAL_WARN_IF(rc != osl_File_E_None, "ucbhelper", "osl_setFilePos failed");
100 sal_Int32 SAL_CALL FdInputStream::available()
102 return std::min<sal_Int64>(SAL_MAX_INT32, m_nLength - getPosition());
106 void SAL_CALL FdInputStream::closeInput()
108 osl::MutexGuard aGuard(m_aMutex);
109 if(m_tmpfl)
111 osl_closeFile(m_tmpfl);
112 m_tmpfl = nullptr;
117 void SAL_CALL FdInputStream::seek(sal_Int64 location)
119 osl::MutexGuard aGuard(m_aMutex);
120 if(!m_tmpfl)
121 throw IOException();
123 oslFileError rc = osl_setFilePos( m_tmpfl, osl_Pos_Absolut, location );
124 SAL_WARN_IF(rc != osl_File_E_None, "ucbhelper", "osl_setFilePos failed");
128 sal_Int64 SAL_CALL
129 FdInputStream::getPosition()
131 osl::MutexGuard aGuard(m_aMutex);
132 if(!m_tmpfl)
133 throw IOException();
135 sal_uInt64 nFilePos = 0;
136 osl_getFilePos( m_tmpfl, &nFilePos );
137 return nFilePos;
141 sal_Int64 SAL_CALL FdInputStream::getLength()
143 return m_nLength;
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */