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>
26 using namespace com::sun::star::uno
;
27 using namespace com::sun::star::lang
;
28 using namespace com::sun::star::io
;
32 FdInputStream::FdInputStream(FILE* tmpfl
)
33 : m_tmpfl(tmpfl
? tmpfl
: tmpfile())
35 fseek(m_tmpfl
,0,SEEK_END
);
36 long pos
= ftell(m_tmpfl
);
38 m_nLength
= sal_Int64(pos
);
43 FdInputStream::~FdInputStream()
50 Any SAL_CALL
FdInputStream::queryInterface(
57 Any aRet
= ::cppu::queryInterface(rType
,
58 (static_cast< XInputStream
* >(this)),
59 (static_cast< XSeekable
* >(this)) );
61 return aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
);
65 void SAL_CALL
FdInputStream::acquire( void ) throw() {
66 OWeakObject::acquire();
69 void SAL_CALL
FdInputStream::release( void ) throw() {
70 OWeakObject::release();
74 sal_Int32 SAL_CALL
FdInputStream::readBytes(Sequence
< sal_Int8
>& aData
,
75 sal_Int32 nBytesToRead
)
76 throw(NotConnectedException
,
77 BufferSizeExceededException
,
81 osl::MutexGuard
aGuard(m_aMutex
);
83 if(0 <= nBytesToRead
&& aData
.getLength() < nBytesToRead
)
84 aData
.realloc(nBytesToRead
);
86 size_t nWanted
= static_cast<size_t>(nBytesToRead
);
87 size_t nRead
= fread(aData
.getArray(), 1, nWanted
, m_tmpfl
);
88 if (nRead
!= nWanted
&& ferror(m_tmpfl
))
91 return static_cast<sal_Int32
>(nRead
);
95 sal_Int32 SAL_CALL
FdInputStream::readSomeBytes( Sequence
< sal_Int8
>& aData
,
96 sal_Int32 nMaxBytesToRead
)
97 throw( NotConnectedException
,
98 BufferSizeExceededException
,
102 return readBytes(aData
,nMaxBytesToRead
);
107 void SAL_CALL
FdInputStream::skipBytes(sal_Int32 nBytesToSkip
)
108 throw(NotConnectedException
,
109 BufferSizeExceededException
,
113 osl::MutexGuard
aGuard(m_aMutex
);
117 fseek(m_tmpfl
,long(nBytesToSkip
),SEEK_CUR
);
122 sal_Int32 SAL_CALL
FdInputStream::available(void)
123 throw(NotConnectedException
,
127 return sal::static_int_cast
<sal_Int32
>(m_nLength
- getPosition());
132 void SAL_CALL
FdInputStream::closeInput(void)
133 throw(NotConnectedException
,
137 osl::MutexGuard
aGuard(m_aMutex
);
139 fclose(m_tmpfl
),m_tmpfl
= 0;
144 void SAL_CALL
FdInputStream::seek(sal_Int64 location
)
145 throw( IllegalArgumentException
,
149 osl::MutexGuard
aGuard(m_aMutex
);
153 fseek(m_tmpfl
,long(location
),SEEK_SET
);
159 FdInputStream::getPosition(
164 osl::MutexGuard
aGuard(m_aMutex
);
169 // fgetpos(m_tmpfl,&pos);
171 pos
= ftell(m_tmpfl
);
172 return sal_Int64(pos
);
177 sal_Int64 SAL_CALL
FdInputStream::getLength(
180 IOException
,RuntimeException
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */