1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <rtl/memory.h>
29 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
30 #include <ucbhelper/cancelcommandexecution.hxx>
31 #include <string.h>
32 
33 #include "gio_inputstream.hxx"
34 #include "gio_content.hxx"
35 
36 using namespace com::sun::star;
37 
38 namespace gio
39 {
40 
41 InputStream::InputStream(GFileInputStream *pStream) : Seekable(G_SEEKABLE(pStream)), mpStream(pStream)
42 {
43     if (!mpStream)
44         throw io::NotConnectedException();
45 }
46 
47 InputStream::~InputStream( void )
48 {
49     closeInput();
50 }
51 
52 sal_Int32 SAL_CALL InputStream::available()
53     throw( io::NotConnectedException, io::IOException, uno::RuntimeException )
54 {
55     return 0;
56 }
57 
58 void SAL_CALL InputStream::closeInput()
59     throw( io::NotConnectedException, io::IOException, uno::RuntimeException )
60 {
61     if (mpStream)
62         g_input_stream_close(G_INPUT_STREAM(mpStream), NULL, NULL);
63 }
64 
65 void SAL_CALL InputStream::skipBytes( sal_Int32 nBytesToSkip )
66     throw( io::NotConnectedException, io::BufferSizeExceededException,
67       io::IOException, uno::RuntimeException )
68 {
69     if (!mpStream)
70         throw io::NotConnectedException();
71 
72     if (!g_seekable_can_seek(G_SEEKABLE(mpStream)))
73         throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Seek unsupported")),
74             static_cast< cppu::OWeakObject * >(this));
75 
76     GError *pError=NULL;
77     if (!g_seekable_seek(G_SEEKABLE(mpStream), nBytesToSkip, G_SEEK_CUR, NULL, &pError))
78         convertToException(pError, static_cast< cppu::OWeakObject * >(this));
79 }
80 
81 sal_Int32 SAL_CALL InputStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
82     throw( io::NotConnectedException, io::BufferSizeExceededException,
83       io::IOException, uno::RuntimeException )
84 {
85     if (!mpStream)
86         throw io::NotConnectedException();
87 
88     try
89     {
90         aData.realloc( nBytesToRead );
91     }
92     catch ( const uno::Exception &e )
93     {
94         throw io::BufferSizeExceededException();
95     }
96 
97     gsize nBytesRead = 0;
98     GError *pError=NULL;
99     if (!g_input_stream_read_all(G_INPUT_STREAM(mpStream), aData.getArray(), nBytesToRead, &nBytesRead, NULL, &pError))
100         convertToException(pError, static_cast< cppu::OWeakObject * >(this));
101     aData.realloc(nBytesRead);
102     return nBytesRead;
103 }
104 
105 sal_Int32 SAL_CALL InputStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
106     throw( io::NotConnectedException, io::BufferSizeExceededException,
107       io::IOException, uno::RuntimeException )
108 {
109     return readBytes(aData, nMaxBytesToRead);
110 }
111 
112 uno::Any InputStream::queryInterface( const uno::Type &type ) throw( uno::RuntimeException )
113 {
114     uno::Any aRet = ::cppu::queryInterface ( type,
115         static_cast< XInputStream * >( this ) );
116 
117     return aRet.hasValue() ? aRet : Seekable::queryInterface( type );
118 }
119 
120 }
121