LibreOffice Module ucb (master)  1
gio_inputstream.cxx
Go to the documentation of this file.
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 #include <sal/config.h>
21 
22 #include <com/sun/star/io/BufferSizeExceededException.hpp>
23 #include <com/sun/star/io/NotConnectedException.hpp>
24 
25 #include "gio_inputstream.hxx"
26 #include "gio_content.hxx"
27 
28 namespace gio
29 {
30 
31 InputStream::InputStream(GFileInputStream *pStream): mpStream(pStream)
32 {
33  if (!mpStream)
34  throw css::io::NotConnectedException();
35 }
36 
38 {
39  closeInput();
40 }
41 
42 sal_Int32 SAL_CALL InputStream::available()
43 {
44  return 0;
45 }
46 
47 void SAL_CALL InputStream::closeInput()
48 {
49  if (mpStream)
50  g_input_stream_close(G_INPUT_STREAM(mpStream), nullptr, nullptr);
51 }
52 
53 void SAL_CALL InputStream::skipBytes( sal_Int32 nBytesToSkip )
54 {
55  // Conservatively call readBytes and discard the read data, but given this
56  // InputStream will always be wrapped in comphelper::OSeekableInputWrapper,
57  // this function will never be called anyway:
58  css::uno::Sequence<sal_Int8> data;
59  readBytes(data, nBytesToSkip);
60 }
61 
62 sal_Int32 SAL_CALL InputStream::readBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
63 {
64  if (!mpStream)
65  throw css::io::NotConnectedException();
66 
67  try
68  {
69  aData.realloc( nBytesToRead );
70  }
71  catch ( const css::uno::Exception & )
72  {
73  throw css::io::BufferSizeExceededException();
74  }
75 
76  gsize nBytesRead = 0;
77  GError *pError=nullptr;
78  if (!g_input_stream_read_all(G_INPUT_STREAM(mpStream), aData.getArray(), nBytesToRead, &nBytesRead, nullptr, &pError))
79  convertToIOException(pError, static_cast< cppu::OWeakObject * >(this));
80  aData.realloc(nBytesRead);
81  return nBytesRead;
82 }
83 
84 sal_Int32 SAL_CALL InputStream::readSomeBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
85 {
86  return readBytes(aData, nMaxBytesToRead);
87 }
88 
89 }
90 
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
InputStream(GFileInputStream *pStream)
virtual void SAL_CALL closeInput() override
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
GFileInputStream *const mpStream
void convertToIOException(GError *pError, const css::uno::Reference< css::uno::XInterface > &rContext)
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
virtual sal_Int32 SAL_CALL available() override
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
virtual ~InputStream() override