Line data Source code
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 :
20 : #include "ucbhelper/fd_inputstream.hxx"
21 :
22 : #include <rtl/alloc.h>
23 : #include <algorithm>
24 : #include <stdio.h>
25 :
26 : using namespace com::sun::star::uno;
27 : using namespace com::sun::star::lang;
28 : using namespace com::sun::star::io;
29 :
30 : namespace ucbhelper
31 : {
32 0 : FdInputStream::FdInputStream(FILE* tmpfl)
33 0 : : m_tmpfl(tmpfl ? tmpfl : tmpfile())
34 : {
35 0 : fseek(m_tmpfl,0,SEEK_END);
36 0 : long pos = ftell(m_tmpfl);
37 0 : rewind(m_tmpfl);
38 0 : m_nLength = sal_Int64(pos);
39 0 : }
40 :
41 :
42 :
43 0 : FdInputStream::~FdInputStream()
44 : {
45 0 : if ( 0 != m_tmpfl)
46 0 : fclose(m_tmpfl);
47 0 : }
48 :
49 :
50 0 : Any SAL_CALL FdInputStream::queryInterface(
51 : const Type& rType
52 : )
53 : throw(
54 : RuntimeException
55 : )
56 : {
57 : Any aRet = ::cppu::queryInterface(rType,
58 : (static_cast< XInputStream* >(this)),
59 0 : (static_cast< XSeekable* >(this)) );
60 :
61 0 : return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
62 : }
63 :
64 :
65 0 : void SAL_CALL FdInputStream::acquire( void ) throw() {
66 0 : OWeakObject::acquire();
67 0 : }
68 :
69 0 : void SAL_CALL FdInputStream::release( void ) throw() {
70 0 : OWeakObject::release();
71 0 : }
72 :
73 :
74 0 : sal_Int32 SAL_CALL FdInputStream::readBytes(Sequence< sal_Int8 >& aData,
75 : sal_Int32 nBytesToRead)
76 : throw(NotConnectedException,
77 : BufferSizeExceededException,
78 : IOException,
79 : RuntimeException)
80 : {
81 0 : osl::MutexGuard aGuard(m_aMutex);
82 :
83 0 : if(0 <= nBytesToRead && aData.getLength() < nBytesToRead)
84 0 : aData.realloc(nBytesToRead);
85 :
86 0 : size_t nWanted = static_cast<size_t>(nBytesToRead);
87 0 : size_t nRead = fread(aData.getArray(), 1, nWanted, m_tmpfl);
88 0 : if (nRead != nWanted && ferror(m_tmpfl))
89 0 : throw IOException();
90 :
91 0 : return static_cast<sal_Int32>(nRead);
92 : }
93 :
94 :
95 0 : sal_Int32 SAL_CALL FdInputStream::readSomeBytes( Sequence< sal_Int8 >& aData,
96 : sal_Int32 nMaxBytesToRead )
97 : throw( NotConnectedException,
98 : BufferSizeExceededException,
99 : IOException,
100 : RuntimeException)
101 : {
102 0 : return readBytes(aData,nMaxBytesToRead);
103 : }
104 :
105 :
106 :
107 0 : void SAL_CALL FdInputStream::skipBytes(sal_Int32 nBytesToSkip)
108 : throw(NotConnectedException,
109 : BufferSizeExceededException,
110 : IOException,
111 : RuntimeException)
112 : {
113 0 : osl::MutexGuard aGuard(m_aMutex);
114 0 : if(!m_tmpfl)
115 0 : throw IOException();
116 :
117 0 : fseek(m_tmpfl,long(nBytesToSkip),SEEK_CUR);
118 0 : }
119 :
120 :
121 :
122 0 : sal_Int32 SAL_CALL FdInputStream::available(void)
123 : throw(NotConnectedException,
124 : IOException,
125 : RuntimeException)
126 : {
127 0 : return sal::static_int_cast<sal_Int32>(m_nLength - getPosition());
128 : }
129 :
130 :
131 :
132 0 : void SAL_CALL FdInputStream::closeInput(void)
133 : throw(NotConnectedException,
134 : IOException,
135 : RuntimeException)
136 : {
137 0 : osl::MutexGuard aGuard(m_aMutex);
138 0 : if(m_tmpfl)
139 0 : fclose(m_tmpfl),m_tmpfl = 0;
140 0 : }
141 :
142 :
143 :
144 0 : void SAL_CALL FdInputStream::seek(sal_Int64 location)
145 : throw( IllegalArgumentException,
146 : IOException,
147 : RuntimeException )
148 : {
149 0 : osl::MutexGuard aGuard(m_aMutex);
150 0 : if(!m_tmpfl)
151 0 : throw IOException();
152 :
153 0 : fseek(m_tmpfl,long(location),SEEK_SET);
154 0 : }
155 :
156 :
157 :
158 : sal_Int64 SAL_CALL
159 0 : FdInputStream::getPosition(
160 : void )
161 : throw( IOException,
162 : RuntimeException )
163 : {
164 0 : osl::MutexGuard aGuard(m_aMutex);
165 0 : if(!m_tmpfl)
166 0 : throw IOException();
167 :
168 : // fpos_t pos;
169 : // fgetpos(m_tmpfl,&pos);
170 : long pos;
171 0 : pos = ftell(m_tmpfl);
172 0 : return sal_Int64(pos);
173 : }
174 :
175 :
176 :
177 0 : sal_Int64 SAL_CALL FdInputStream::getLength(
178 : void
179 : ) throw(
180 : IOException,RuntimeException
181 : )
182 : {
183 0 : return m_nLength;
184 : }
185 : }
186 :
187 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|