xref: /trunk/main/ucb/source/ucp/gio/gio_inputstream.cxx (revision 9d37da743abb7db0a497688391f6e55a19f27c44)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <rtl/memory.h>
25 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
26 #include <ucbhelper/cancelcommandexecution.hxx>
27 #include <string.h>
28 
29 #include "gio_inputstream.hxx"
30 #include "gio_content.hxx"
31 
32 using namespace com::sun::star;
33 
34 namespace gio
35 {
36 
37 InputStream::InputStream(GFileInputStream *pStream) : Seekable(G_SEEKABLE(pStream)), mpStream(pStream)
38 {
39     if (!mpStream)
40         throw io::NotConnectedException();
41 }
42 
43 InputStream::~InputStream( void )
44 {
45     closeInput();
46 }
47 
48 sal_Int32 SAL_CALL InputStream::available()
49 {
50     return 0;
51 }
52 
53 void SAL_CALL InputStream::closeInput()
54 {
55     if (mpStream)
56         g_input_stream_close(G_INPUT_STREAM(mpStream), NULL, NULL);
57 }
58 
59 void SAL_CALL InputStream::skipBytes( sal_Int32 nBytesToSkip )
60 {
61     if (!mpStream)
62         throw io::NotConnectedException();
63 
64     if (!g_seekable_can_seek(G_SEEKABLE(mpStream)))
65         throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Seek unsupported")),
66             static_cast< cppu::OWeakObject * >(this));
67 
68     GError *pError=NULL;
69     if (!g_seekable_seek(G_SEEKABLE(mpStream), nBytesToSkip, G_SEEK_CUR, NULL, &pError))
70         convertToException(pError, static_cast< cppu::OWeakObject * >(this));
71 }
72 
73 sal_Int32 SAL_CALL InputStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
74 {
75     if (!mpStream)
76         throw io::NotConnectedException();
77 
78     try
79     {
80         aData.realloc( nBytesToRead );
81     }
82     catch ( const uno::Exception &e )
83     {
84         throw io::BufferSizeExceededException();
85     }
86 
87     gsize nBytesRead = 0;
88     GError *pError=NULL;
89     if (!g_input_stream_read_all(G_INPUT_STREAM(mpStream), aData.getArray(), nBytesToRead, &nBytesRead, NULL, &pError))
90         convertToException(pError, static_cast< cppu::OWeakObject * >(this));
91     aData.realloc(nBytesRead);
92     return nBytesRead;
93 }
94 
95 sal_Int32 SAL_CALL InputStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
96 {
97     return readBytes(aData, nMaxBytesToRead);
98 }
99 
100 uno::Any InputStream::queryInterface( const uno::Type &type )
101 {
102     uno::Any aRet = ::cppu::queryInterface ( type,
103         static_cast< XInputStream * >( this ) );
104 
105     return aRet.hasValue() ? aRet : Seekable::queryInterface( type );
106 }
107 
108 }
109