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 :
21 : #include "inputstream.hxx"
22 :
23 : #include <cppuhelper/queryinterface.hxx>
24 :
25 :
26 : using namespace chelp;
27 : using namespace com::sun::star;
28 : using namespace com::sun::star::ucb;
29 :
30 :
31 :
32 0 : XInputStream_impl::XInputStream_impl( const OUString& aUncPath )
33 : : m_bIsOpen( false ),
34 0 : m_aFile( aUncPath )
35 : {
36 0 : m_bIsOpen = ( osl::FileBase::E_None == m_aFile.open( osl_File_OpenFlag_Read ) );
37 0 : }
38 :
39 :
40 0 : XInputStream_impl::~XInputStream_impl()
41 : {
42 0 : closeInput();
43 0 : }
44 :
45 :
46 : uno::Any SAL_CALL
47 0 : XInputStream_impl::queryInterface(
48 : const uno::Type& rType )
49 : throw( uno::RuntimeException, std::exception)
50 : {
51 : uno::Any aRet = cppu::queryInterface( rType,
52 : (static_cast< io::XInputStream* >(this)),
53 0 : (static_cast< io::XSeekable* >(this)) );
54 0 : return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
55 : }
56 :
57 :
58 : void SAL_CALL
59 0 : XInputStream_impl::acquire(
60 : void )
61 : throw()
62 : {
63 0 : OWeakObject::acquire();
64 0 : }
65 :
66 :
67 : void SAL_CALL
68 0 : XInputStream_impl::release(
69 : void )
70 : throw()
71 : {
72 0 : OWeakObject::release();
73 0 : }
74 :
75 :
76 :
77 : sal_Int32 SAL_CALL
78 0 : XInputStream_impl::readBytes(
79 : uno::Sequence< sal_Int8 >& aData,
80 : sal_Int32 nBytesToRead )
81 : throw( io::NotConnectedException,
82 : io::BufferSizeExceededException,
83 : io::IOException,
84 : uno::RuntimeException, std::exception)
85 : {
86 0 : if( ! m_bIsOpen )
87 0 : throw io::IOException();
88 :
89 0 : aData.realloc(nBytesToRead);
90 : //TODO! translate memory exhaustion (if it were detectable...) into
91 : // io::BufferSizeExceededException
92 :
93 : sal_uInt64 nrc;
94 0 : m_aFile.read( aData.getArray(),sal_uInt64(nBytesToRead),nrc );
95 :
96 : // Shrink aData in case we read less than nBytesToRead (XInputStream
97 : // documentation does not tell whether this is required, and I do not know
98 : // if any code relies on this, so be conservative---SB):
99 0 : if (nrc != sal::static_int_cast<sal_uInt64>( nBytesToRead) )
100 0 : aData.realloc(sal_Int32(nrc));
101 0 : return ( sal_Int32 ) nrc;
102 : }
103 :
104 : sal_Int32 SAL_CALL
105 0 : XInputStream_impl::readSomeBytes(
106 : uno::Sequence< sal_Int8 >& aData,
107 : sal_Int32 nMaxBytesToRead )
108 : throw( io::NotConnectedException,
109 : io::BufferSizeExceededException,
110 : io::IOException,
111 : uno::RuntimeException, std::exception)
112 : {
113 0 : return readBytes( aData,nMaxBytesToRead );
114 : }
115 :
116 :
117 : void SAL_CALL
118 0 : XInputStream_impl::skipBytes(
119 : sal_Int32 nBytesToSkip )
120 : throw( io::NotConnectedException,
121 : io::BufferSizeExceededException,
122 : io::IOException,
123 : uno::RuntimeException, std::exception)
124 : {
125 0 : if (m_aFile.setPos(osl_Pos_Current, sal_uInt64(nBytesToSkip)) != osl::FileBase::E_None)
126 : {
127 0 : throw io::IOException("XInputStream_impl::skipBytes failed seek");
128 : }
129 0 : }
130 :
131 :
132 : sal_Int32 SAL_CALL
133 0 : XInputStream_impl::available(
134 : void )
135 : throw( io::NotConnectedException,
136 : io::IOException,
137 : uno::RuntimeException, std::exception)
138 : {
139 0 : return 0;
140 : }
141 :
142 :
143 : void SAL_CALL
144 0 : XInputStream_impl::closeInput(
145 : void )
146 : throw( io::NotConnectedException,
147 : io::IOException,
148 : uno::RuntimeException, std::exception )
149 : {
150 0 : if( m_bIsOpen )
151 : {
152 0 : osl::FileBase::RC err = m_aFile.close();
153 0 : if( err != osl::FileBase::E_None )
154 0 : throw io::IOException();
155 0 : m_bIsOpen = false;
156 : }
157 0 : }
158 :
159 :
160 : void SAL_CALL
161 0 : XInputStream_impl::seek(
162 : sal_Int64 location )
163 : throw( lang::IllegalArgumentException,
164 : io::IOException,
165 : uno::RuntimeException, std::exception )
166 : {
167 0 : if( location < 0 )
168 0 : throw lang::IllegalArgumentException();
169 0 : if( osl::FileBase::E_None != m_aFile.setPos( osl_Pos_Absolut, sal_uInt64( location ) ) )
170 0 : throw io::IOException();
171 0 : }
172 :
173 :
174 : sal_Int64 SAL_CALL
175 0 : XInputStream_impl::getPosition(
176 : void )
177 : throw( io::IOException,
178 : uno::RuntimeException, std::exception )
179 : {
180 : sal_uInt64 uPos;
181 0 : if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
182 0 : throw io::IOException();
183 0 : return sal_Int64( uPos );
184 : }
185 :
186 : sal_Int64 SAL_CALL
187 0 : XInputStream_impl::getLength(
188 : void )
189 : throw( io::IOException,
190 : uno::RuntimeException, std::exception )
191 : {
192 : osl::FileBase::RC err;
193 : sal_uInt64 uCurrentPos, uEndPos;
194 :
195 0 : err = m_aFile.getPos( uCurrentPos );
196 0 : if( err != osl::FileBase::E_None )
197 0 : throw io::IOException();
198 :
199 0 : err = m_aFile.setPos( osl_Pos_End, 0 );
200 0 : if( err != osl::FileBase::E_None )
201 0 : throw io::IOException();
202 :
203 0 : err = m_aFile.getPos( uEndPos );
204 0 : if( err != osl::FileBase::E_None )
205 0 : throw io::IOException();
206 :
207 0 : err = m_aFile.setPos( osl_Pos_Absolut, uCurrentPos );
208 0 : if( err != osl::FileBase::E_None )
209 0 : throw io::IOException();
210 : else
211 0 : return sal_Int64( uEndPos );
212 : }
213 :
214 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|