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