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