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 
InputStream(GFileInputStream * pStream)37 InputStream::InputStream(GFileInputStream *pStream) : Seekable(G_SEEKABLE(pStream)), mpStream(pStream)
38 {
39     if (!mpStream)
40         throw io::NotConnectedException();
41 }
42 
~InputStream(void)43 InputStream::~InputStream( void )
44 {
45     closeInput();
46 }
47 
available()48 sal_Int32 SAL_CALL InputStream::available()
49     throw( io::NotConnectedException, io::IOException, uno::RuntimeException )
50 {
51     return 0;
52 }
53 
closeInput()54 void SAL_CALL InputStream::closeInput()
55     throw( io::NotConnectedException, io::IOException, uno::RuntimeException )
56 {
57     if (mpStream)
58         g_input_stream_close(G_INPUT_STREAM(mpStream), NULL, NULL);
59 }
60 
skipBytes(sal_Int32 nBytesToSkip)61 void SAL_CALL InputStream::skipBytes( sal_Int32 nBytesToSkip )
62     throw( io::NotConnectedException, io::BufferSizeExceededException,
63       io::IOException, uno::RuntimeException )
64 {
65     if (!mpStream)
66         throw io::NotConnectedException();
67 
68     if (!g_seekable_can_seek(G_SEEKABLE(mpStream)))
69         throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Seek unsupported")),
70             static_cast< cppu::OWeakObject * >(this));
71 
72     GError *pError=NULL;
73     if (!g_seekable_seek(G_SEEKABLE(mpStream), nBytesToSkip, G_SEEK_CUR, NULL, &pError))
74         convertToException(pError, static_cast< cppu::OWeakObject * >(this));
75 }
76 
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)77 sal_Int32 SAL_CALL InputStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
78     throw( io::NotConnectedException, io::BufferSizeExceededException,
79       io::IOException, uno::RuntimeException )
80 {
81     if (!mpStream)
82         throw io::NotConnectedException();
83 
84     try
85     {
86         aData.realloc( nBytesToRead );
87     }
88     catch ( const uno::Exception &e )
89     {
90         throw io::BufferSizeExceededException();
91     }
92 
93     gsize nBytesRead = 0;
94     GError *pError=NULL;
95     if (!g_input_stream_read_all(G_INPUT_STREAM(mpStream), aData.getArray(), nBytesToRead, &nBytesRead, NULL, &pError))
96         convertToException(pError, static_cast< cppu::OWeakObject * >(this));
97     aData.realloc(nBytesRead);
98     return nBytesRead;
99 }
100 
readSomeBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)101 sal_Int32 SAL_CALL InputStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
102     throw( io::NotConnectedException, io::BufferSizeExceededException,
103       io::IOException, uno::RuntimeException )
104 {
105     return readBytes(aData, nMaxBytesToRead);
106 }
107 
queryInterface(const uno::Type & type)108 uno::Any InputStream::queryInterface( const uno::Type &type ) throw( uno::RuntimeException )
109 {
110     uno::Any aRet = ::cppu::queryInterface ( type,
111         static_cast< XInputStream * >( this ) );
112 
113     return aRet.hasValue() ? aRet : Seekable::queryInterface( type );
114 }
115 
116 }
117