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 :
25 : using namespace com::sun::star::uno;
26 : using namespace com::sun::star::lang;
27 : using namespace com::sun::star::io;
28 :
29 : namespace ucbhelper
30 : {
31 0 : FdInputStream::FdInputStream( oslFileHandle tmpfl )
32 : : m_tmpfl(tmpfl)
33 0 : , m_nLength( 0 )
34 : {
35 0 : if ( !m_tmpfl )
36 0 : osl_createTempFile( NULL, &m_tmpfl, NULL );
37 : OSL_ENSURE( m_tmpfl, "input stream without tempfile!" );
38 :
39 0 : if ( osl_setFilePos( m_tmpfl, osl_Pos_End, 0 ) == osl_File_E_None )
40 : {
41 0 : sal_uInt64 nFileSize = 0;
42 0 : if ( osl_getFilePos( m_tmpfl, &nFileSize ) == osl_File_E_None )
43 0 : m_nLength = nFileSize;
44 0 : oslFileError rc = osl_setFilePos( m_tmpfl, osl_Pos_Absolut, 0 );
45 : SAL_WARN_IF(rc != osl_File_E_None, "ucbhelper", "osl_setFilePos failed");
46 : }
47 0 : }
48 :
49 0 : FdInputStream::~FdInputStream()
50 : {
51 0 : if ( 0 != m_tmpfl)
52 0 : osl_closeFile(m_tmpfl);
53 0 : }
54 :
55 0 : sal_Int32 SAL_CALL FdInputStream::readBytes(Sequence< sal_Int8 >& aData,
56 : sal_Int32 nBytesToRead)
57 : throw(NotConnectedException,
58 : BufferSizeExceededException,
59 : IOException,
60 : RuntimeException, std::exception)
61 : {
62 0 : osl::MutexGuard aGuard(m_aMutex);
63 :
64 0 : sal_uInt64 nBeforePos( 0 );
65 0 : sal_uInt64 nBytesRequested( nBytesToRead );
66 0 : sal_uInt64 nBytesRead( 0 );
67 :
68 0 : osl_getFilePos( m_tmpfl, &nBeforePos );
69 :
70 0 : if ( 0 == ( nBytesRequested = std::min< sal_uInt64 >( m_nLength - nBeforePos, nBytesRequested ) ) )
71 0 : return 0;
72 :
73 0 : if ( 0 <= nBytesToRead && aData.getLength() < nBytesToRead )
74 0 : aData.realloc( nBytesToRead );
75 :
76 0 : if ( osl_readFile( m_tmpfl, aData.getArray(), nBytesRequested, &nBytesRead ) != osl_File_E_None )
77 0 : throw IOException();
78 :
79 0 : return sal_Int32( nBytesRead );
80 : }
81 :
82 :
83 0 : sal_Int32 SAL_CALL FdInputStream::readSomeBytes( Sequence< sal_Int8 >& aData,
84 : sal_Int32 nMaxBytesToRead )
85 : throw( NotConnectedException,
86 : BufferSizeExceededException,
87 : IOException,
88 : RuntimeException, std::exception)
89 : {
90 0 : return readBytes(aData,nMaxBytesToRead);
91 : }
92 :
93 :
94 :
95 0 : void SAL_CALL FdInputStream::skipBytes(sal_Int32 nBytesToSkip)
96 : throw(NotConnectedException,
97 : BufferSizeExceededException,
98 : IOException,
99 : RuntimeException, std::exception)
100 : {
101 0 : osl::MutexGuard aGuard(m_aMutex);
102 0 : if(!m_tmpfl)
103 0 : throw IOException();
104 :
105 0 : oslFileError rc = osl_setFilePos( m_tmpfl, osl_Pos_Current, nBytesToSkip );
106 0 : SAL_WARN_IF(rc != osl_File_E_None, "ucbhelper", "osl_setFilePos failed");
107 0 : }
108 :
109 :
110 :
111 0 : sal_Int32 SAL_CALL FdInputStream::available(void)
112 : throw(NotConnectedException,
113 : IOException,
114 : RuntimeException, std::exception)
115 : {
116 0 : return sal::static_int_cast<sal_Int32>(m_nLength - getPosition());
117 : }
118 :
119 :
120 :
121 0 : void SAL_CALL FdInputStream::closeInput(void)
122 : throw(NotConnectedException,
123 : IOException,
124 : RuntimeException, std::exception)
125 : {
126 0 : osl::MutexGuard aGuard(m_aMutex);
127 0 : if(m_tmpfl)
128 0 : osl_closeFile(m_tmpfl),m_tmpfl = 0;
129 0 : }
130 :
131 :
132 :
133 0 : void SAL_CALL FdInputStream::seek(sal_Int64 location)
134 : throw( IllegalArgumentException,
135 : IOException,
136 : RuntimeException, std::exception )
137 : {
138 0 : osl::MutexGuard aGuard(m_aMutex);
139 0 : if(!m_tmpfl)
140 0 : throw IOException();
141 :
142 0 : oslFileError rc = osl_setFilePos( m_tmpfl, osl_Pos_Absolut, location );
143 0 : SAL_WARN_IF(rc != osl_File_E_None, "ucbhelper", "osl_setFilePos failed");
144 0 : }
145 :
146 :
147 :
148 : sal_Int64 SAL_CALL
149 0 : FdInputStream::getPosition(
150 : void )
151 : throw( IOException,
152 : RuntimeException, std::exception )
153 : {
154 0 : osl::MutexGuard aGuard(m_aMutex);
155 0 : if(!m_tmpfl)
156 0 : throw IOException();
157 :
158 0 : sal_uInt64 nFilePos = 0;
159 0 : osl_getFilePos( m_tmpfl, &nFilePos );
160 0 : return nFilePos;
161 : }
162 :
163 :
164 :
165 0 : sal_Int64 SAL_CALL FdInputStream::getLength(
166 : void
167 : ) throw(
168 : IOException,RuntimeException, std::exception
169 : )
170 : {
171 0 : return m_nLength;
172 : }
173 : }
174 :
175 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|