LibreOffice Module ucbhelper (master)  1
fd_inputstream.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 
21 
22 #include <com/sun/star/io/IOException.hpp>
23 #include <osl/diagnose.h>
24 #include <sal/log.hxx>
25 #include <algorithm>
26 
27 using namespace com::sun::star::uno;
28 using namespace com::sun::star::lang;
29 using namespace com::sun::star::io;
30 
31 namespace ucbhelper
32 {
33  FdInputStream::FdInputStream( oslFileHandle tmpfl )
34  : m_tmpfl(tmpfl)
35  , m_nLength( 0 )
36  {
37  if ( !m_tmpfl )
38  osl_createTempFile( nullptr, &m_tmpfl, nullptr );
39  OSL_ENSURE( m_tmpfl, "input stream without tempfile!" );
40 
41  if ( osl_setFilePos( m_tmpfl, osl_Pos_End, 0 ) == osl_File_E_None )
42  {
43  sal_uInt64 nFileSize = 0;
44  if ( osl_getFilePos( m_tmpfl, &nFileSize ) == osl_File_E_None )
45  m_nLength = nFileSize;
46  oslFileError rc = osl_setFilePos( m_tmpfl, osl_Pos_Absolut, 0 );
47  SAL_WARN_IF(rc != osl_File_E_None, "ucbhelper", "osl_setFilePos failed");
48  }
49  }
50 
52  {
53  if ( nullptr != m_tmpfl)
54  osl_closeFile(m_tmpfl);
55  }
56 
58  sal_Int32 nBytesToRead)
59  {
60  osl::MutexGuard aGuard(m_aMutex);
61 
62  sal_uInt64 nBeforePos( 0 );
63  sal_uInt64 nBytesRequested( nBytesToRead );
64  sal_uInt64 nBytesRead( 0 );
65 
66  osl_getFilePos( m_tmpfl, &nBeforePos );
67 
68  if ( 0 == ( nBytesRequested = std::min< sal_uInt64 >( m_nLength - nBeforePos, nBytesRequested ) ) )
69  return 0;
70 
71  if ( 0 <= nBytesToRead && aData.getLength() < nBytesToRead )
72  aData.realloc( nBytesToRead );
73 
74  if ( osl_readFile( m_tmpfl, aData.getArray(), nBytesRequested, &nBytesRead ) != osl_File_E_None )
75  throw IOException();
76 
77  return sal_Int32( nBytesRead );
78  }
79 
80 
82  sal_Int32 nMaxBytesToRead )
83  {
84  return readBytes(aData,nMaxBytesToRead);
85  }
86 
87 
88  void SAL_CALL FdInputStream::skipBytes(sal_Int32 nBytesToSkip)
89  {
90  osl::MutexGuard aGuard(m_aMutex);
91  if(!m_tmpfl)
92  throw IOException();
93 
94  oslFileError rc = osl_setFilePos( m_tmpfl, osl_Pos_Current, nBytesToSkip );
95  SAL_WARN_IF(rc != osl_File_E_None, "ucbhelper", "osl_setFilePos failed");
96  }
97 
98 
99  sal_Int32 SAL_CALL FdInputStream::available()
100  {
101  return std::min<sal_Int64>(SAL_MAX_INT32, m_nLength - getPosition());
102  }
103 
104 
106  {
107  osl::MutexGuard aGuard(m_aMutex);
108  if(m_tmpfl)
109  {
110  osl_closeFile(m_tmpfl);
111  m_tmpfl = nullptr;
112  }
113  }
114 
115 
116  void SAL_CALL FdInputStream::seek(sal_Int64 location)
117  {
118  osl::MutexGuard aGuard(m_aMutex);
119  if(!m_tmpfl)
120  throw IOException();
121 
122  oslFileError rc = osl_setFilePos( m_tmpfl, osl_Pos_Absolut, location );
123  SAL_WARN_IF(rc != osl_File_E_None, "ucbhelper", "osl_setFilePos failed");
124  }
125 
126 
127  sal_Int64 SAL_CALL
129  {
130  osl::MutexGuard aGuard(m_aMutex);
131  if(!m_tmpfl)
132  throw IOException();
133 
134  sal_uInt64 nFilePos = 0;
135  osl_getFilePos( m_tmpfl, &nFilePos );
136  return nFilePos;
137  }
138 
139 
140  sal_Int64 SAL_CALL FdInputStream::getLength()
141  {
142  return m_nLength;
143  }
144 }
145 
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const sal_Int32 m_nLength
virtual ~FdInputStream() override
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
virtual sal_Int32 SAL_CALL available() override
mutable::osl::Mutex m_aMutex
virtual void SAL_CALL seek(sal_Int64 location) override
XSeekable.
virtual sal_Int64 SAL_CALL getLength() override
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
virtual sal_Int64 SAL_CALL getPosition() override
#define SAL_MAX_INT32
virtual void SAL_CALL closeInput() override
#define SAL_WARN_IF(condition, area, stream)
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override