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