bump product version to 4.1.6.2
[LibreOffice.git] / ucbhelper / source / provider / fd_inputstream.cxx
blob835fd38b6ef13ab0666935f1510689a6749b363d
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 <rtl/alloc.h>
23 #include <algorithm>
24 #include <stdio.h>
26 using namespace com::sun::star::uno;
27 using namespace com::sun::star::lang;
28 using namespace com::sun::star::io;
30 namespace ucbhelper
32 FdInputStream::FdInputStream(FILE* tmpfl)
33 : m_tmpfl(tmpfl ? tmpfl : tmpfile())
35 fseek(m_tmpfl,0,SEEK_END);
36 long pos = ftell(m_tmpfl);
37 rewind(m_tmpfl);
38 m_nLength = sal_Int64(pos);
43 FdInputStream::~FdInputStream()
45 if ( 0 != m_tmpfl)
46 fclose(m_tmpfl);
50 Any SAL_CALL FdInputStream::queryInterface(
51 const Type& rType
53 throw(
54 RuntimeException
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,
78 IOException,
79 RuntimeException)
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))
89 throw IOException();
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,
99 IOException,
100 RuntimeException)
102 return readBytes(aData,nMaxBytesToRead);
107 void SAL_CALL FdInputStream::skipBytes(sal_Int32 nBytesToSkip)
108 throw(NotConnectedException,
109 BufferSizeExceededException,
110 IOException,
111 RuntimeException)
113 osl::MutexGuard aGuard(m_aMutex);
114 if(!m_tmpfl)
115 throw IOException();
117 fseek(m_tmpfl,long(nBytesToSkip),SEEK_CUR);
122 sal_Int32 SAL_CALL FdInputStream::available(void)
123 throw(NotConnectedException,
124 IOException,
125 RuntimeException)
127 return sal::static_int_cast<sal_Int32>(m_nLength - getPosition());
132 void SAL_CALL FdInputStream::closeInput(void)
133 throw(NotConnectedException,
134 IOException,
135 RuntimeException)
137 osl::MutexGuard aGuard(m_aMutex);
138 if(m_tmpfl)
139 fclose(m_tmpfl),m_tmpfl = 0;
144 void SAL_CALL FdInputStream::seek(sal_Int64 location)
145 throw( IllegalArgumentException,
146 IOException,
147 RuntimeException )
149 osl::MutexGuard aGuard(m_aMutex);
150 if(!m_tmpfl)
151 throw IOException();
153 fseek(m_tmpfl,long(location),SEEK_SET);
158 sal_Int64 SAL_CALL
159 FdInputStream::getPosition(
160 void )
161 throw( IOException,
162 RuntimeException )
164 osl::MutexGuard aGuard(m_aMutex);
165 if(!m_tmpfl)
166 throw IOException();
168 // fpos_t pos;
169 // fgetpos(m_tmpfl,&pos);
170 long pos;
171 pos = ftell(m_tmpfl);
172 return sal_Int64(pos);
177 sal_Int64 SAL_CALL FdInputStream::getLength(
178 void
179 ) throw(
180 IOException,RuntimeException
183 return m_nLength;
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */