Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / xmlscript / source / xml_helper / xml_byteseq.cxx
blob23322324bf4ecb80c35833a651a1fe865ee8441a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <string.h>
22 #include <cppuhelper/implbase.hxx>
23 #include <xmlscript/xml_helper.hxx>
24 #include <com/sun/star/io/XInputStream.hpp>
25 #include <com/sun/star/io/XOutputStream.hpp>
27 using namespace osl;
28 using namespace com::sun::star;
29 using namespace com::sun::star::uno;
32 namespace xmlscript
35 class BSeqInputStream
36 : public ::cppu::WeakImplHelper< io::XInputStream >
38 std::vector<sal_Int8> _seq;
39 sal_Int32 _nPos;
41 public:
42 explicit BSeqInputStream( std::vector<sal_Int8> const & rSeq )
43 : _seq( rSeq )
44 , _nPos( 0 )
47 // XInputStream
48 virtual sal_Int32 SAL_CALL readBytes(
49 Sequence< sal_Int8 > & rData, sal_Int32 nBytesToRead ) override;
50 virtual sal_Int32 SAL_CALL readSomeBytes(
51 Sequence< sal_Int8 > & rData, sal_Int32 nMaxBytesToRead ) override;
52 virtual void SAL_CALL skipBytes(
53 sal_Int32 nBytesToSkip ) override;
54 virtual sal_Int32 SAL_CALL available() override;
55 virtual void SAL_CALL closeInput() override;
58 sal_Int32 BSeqInputStream::readBytes(
59 Sequence< sal_Int8 > & rData, sal_Int32 nBytesToRead )
61 nBytesToRead = ((nBytesToRead > static_cast<sal_Int32>(_seq.size()) - _nPos)
62 ? _seq.size() - _nPos
63 : nBytesToRead);
65 if (rData.getLength() != nBytesToRead)
66 rData.realloc( nBytesToRead );
67 if (nBytesToRead != 0) {
68 memcpy(rData.getArray(), &_seq[_nPos], nBytesToRead);
70 _nPos += nBytesToRead;
71 return nBytesToRead;
74 sal_Int32 BSeqInputStream::readSomeBytes(
75 Sequence< sal_Int8 > & rData, sal_Int32 nMaxBytesToRead )
77 return readBytes( rData, nMaxBytesToRead );
80 void BSeqInputStream::skipBytes(
81 sal_Int32 /*nBytesToSkip*/ )
85 sal_Int32 BSeqInputStream::available()
87 return _seq.size() - _nPos;
90 void BSeqInputStream::closeInput()
94 class BSeqOutputStream
95 : public ::cppu::WeakImplHelper< io::XOutputStream >
97 std::vector<sal_Int8> * _seq;
99 public:
100 explicit BSeqOutputStream( std::vector<sal_Int8> * seq )
101 : _seq( seq )
104 // XOutputStream
105 virtual void SAL_CALL writeBytes(
106 Sequence< sal_Int8 > const & rData ) override;
107 virtual void SAL_CALL flush() override;
108 virtual void SAL_CALL closeOutput() override;
111 void BSeqOutputStream::writeBytes( Sequence< sal_Int8 > const & rData )
113 sal_Int32 nPos = _seq->size();
114 _seq->resize( nPos + rData.getLength() );
115 if (rData.getLength() != 0) {
116 memcpy( _seq->data() + nPos,
117 rData.getConstArray(),
118 rData.getLength() );
121 void BSeqOutputStream::flush()
125 void BSeqOutputStream::closeOutput()
129 Reference< io::XInputStream > createInputStream( std::vector<sal_Int8> const & rInData )
131 return new BSeqInputStream( rInData );
134 Reference< io::XInputStream > createInputStream( const sal_Int8* pData, int len )
136 std::vector<sal_Int8> rInData(len);
137 if (len != 0) {
138 memcpy( rInData.data(), pData, len);
140 return new BSeqInputStream( rInData );
143 Reference< io::XOutputStream > createOutputStream( std::vector<sal_Int8> * pOutData )
145 return new BSeqOutputStream( pOutData );
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */