1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <rtl/alloc.h>
23 #include <osl/diagnose.h>
24 #include <sal/log.hxx>
27 using namespace com::sun::star::uno
;
28 using namespace com::sun::star::lang
;
29 using namespace com::sun::star::io
;
33 FdInputStream::FdInputStream( oslFileHandle tmpfl
)
38 osl_createTempFile( NULL
, &m_tmpfl
, NULL
);
39 OSL_ENSURE( m_tmpfl
, "input stream without tempfile!" );
41 if ( osl_setFilePos( m_tmpfl
, osl_Pos_End
, 0 ) == osl_File_E_None
)
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");
51 FdInputStream::~FdInputStream()
54 osl_closeFile(m_tmpfl
);
57 sal_Int32 SAL_CALL
FdInputStream::readBytes(Sequence
< sal_Int8
>& aData
,
58 sal_Int32 nBytesToRead
)
59 throw(NotConnectedException
,
60 BufferSizeExceededException
,
62 RuntimeException
, std::exception
)
64 osl::MutexGuard
aGuard(m_aMutex
);
66 sal_uInt64
nBeforePos( 0 );
67 sal_uInt64
nBytesRequested( nBytesToRead
);
68 sal_uInt64
nBytesRead( 0 );
70 osl_getFilePos( m_tmpfl
, &nBeforePos
);
72 if ( 0 == ( nBytesRequested
= std::min
< sal_uInt64
>( m_nLength
- nBeforePos
, nBytesRequested
) ) )
75 if ( 0 <= nBytesToRead
&& aData
.getLength() < nBytesToRead
)
76 aData
.realloc( nBytesToRead
);
78 if ( osl_readFile( m_tmpfl
, aData
.getArray(), nBytesRequested
, &nBytesRead
) != osl_File_E_None
)
81 return sal_Int32( nBytesRead
);
85 sal_Int32 SAL_CALL
FdInputStream::readSomeBytes( Sequence
< sal_Int8
>& aData
,
86 sal_Int32 nMaxBytesToRead
)
87 throw( NotConnectedException
,
88 BufferSizeExceededException
,
90 RuntimeException
, std::exception
)
92 return readBytes(aData
,nMaxBytesToRead
);
97 void SAL_CALL
FdInputStream::skipBytes(sal_Int32 nBytesToSkip
)
98 throw(NotConnectedException
,
99 BufferSizeExceededException
,
101 RuntimeException
, std::exception
)
103 osl::MutexGuard
aGuard(m_aMutex
);
107 oslFileError rc
= osl_setFilePos( m_tmpfl
, osl_Pos_Current
, nBytesToSkip
);
108 SAL_WARN_IF(rc
!= osl_File_E_None
, "ucbhelper", "osl_setFilePos failed");
113 sal_Int32 SAL_CALL
FdInputStream::available()
114 throw(NotConnectedException
,
116 RuntimeException
, std::exception
)
118 return sal::static_int_cast
<sal_Int32
>(m_nLength
- getPosition());
123 void SAL_CALL
FdInputStream::closeInput()
124 throw(NotConnectedException
,
126 RuntimeException
, std::exception
)
128 osl::MutexGuard
aGuard(m_aMutex
);
130 osl_closeFile(m_tmpfl
),m_tmpfl
= 0;
135 void SAL_CALL
FdInputStream::seek(sal_Int64 location
)
136 throw( IllegalArgumentException
,
138 RuntimeException
, std::exception
)
140 osl::MutexGuard
aGuard(m_aMutex
);
144 oslFileError rc
= osl_setFilePos( m_tmpfl
, osl_Pos_Absolut
, location
);
145 SAL_WARN_IF(rc
!= osl_File_E_None
, "ucbhelper", "osl_setFilePos failed");
151 FdInputStream::getPosition(
154 RuntimeException
, std::exception
)
156 osl::MutexGuard
aGuard(m_aMutex
);
160 sal_uInt64 nFilePos
= 0;
161 osl_getFilePos( m_tmpfl
, &nFilePos
);
167 sal_Int64 SAL_CALL
FdInputStream::getLength(
170 IOException
,RuntimeException
, std::exception
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */