update credits
[LibreOffice.git] / xmlscript / source / xml_helper / xml_byteseq.cxx
blob693ef301384ebbf357f118c6d9b8369592d85350
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 namespace {
37 class BSeqInputStream
38 : public ::cppu::WeakImplHelper< io::XInputStream >
40 std::vector<sal_Int8> _seq;
41 sal_Int32 _nPos;
43 public:
44 explicit BSeqInputStream( std::vector<sal_Int8> const & rSeq )
45 : _seq( rSeq )
46 , _nPos( 0 )
49 // XInputStream
50 virtual sal_Int32 SAL_CALL readBytes(
51 Sequence< sal_Int8 > & rData, sal_Int32 nBytesToRead ) override;
52 virtual sal_Int32 SAL_CALL readSomeBytes(
53 Sequence< sal_Int8 > & rData, sal_Int32 nMaxBytesToRead ) override;
54 virtual void SAL_CALL skipBytes(
55 sal_Int32 nBytesToSkip ) override;
56 virtual sal_Int32 SAL_CALL available() override;
57 virtual void SAL_CALL closeInput() override;
62 sal_Int32 BSeqInputStream::readBytes(
63 Sequence< sal_Int8 > & rData, sal_Int32 nBytesToRead )
65 nBytesToRead = ((nBytesToRead > static_cast<sal_Int32>(_seq.size()) - _nPos)
66 ? _seq.size() - _nPos
67 : nBytesToRead);
69 if (rData.getLength() != nBytesToRead)
70 rData.realloc( nBytesToRead );
71 if (nBytesToRead != 0) {
72 memcpy(rData.getArray(), &_seq[_nPos], nBytesToRead);
74 _nPos += nBytesToRead;
75 return nBytesToRead;
78 sal_Int32 BSeqInputStream::readSomeBytes(
79 Sequence< sal_Int8 > & rData, sal_Int32 nMaxBytesToRead )
81 return readBytes( rData, nMaxBytesToRead );
84 void BSeqInputStream::skipBytes(
85 sal_Int32 /*nBytesToSkip*/ )
89 sal_Int32 BSeqInputStream::available()
91 return _seq.size() - _nPos;
94 void BSeqInputStream::closeInput()
98 namespace {
100 class BSeqOutputStream
101 : public ::cppu::WeakImplHelper< io::XOutputStream >
103 std::vector<sal_Int8> * _seq;
105 public:
106 explicit BSeqOutputStream( std::vector<sal_Int8> * seq )
107 : _seq( seq )
110 // XOutputStream
111 virtual void SAL_CALL writeBytes(
112 Sequence< sal_Int8 > const & rData ) override;
113 virtual void SAL_CALL flush() override;
114 virtual void SAL_CALL closeOutput() override;
119 void BSeqOutputStream::writeBytes( Sequence< sal_Int8 > const & rData )
121 sal_Int32 nPos = _seq->size();
122 _seq->resize( nPos + rData.getLength() );
123 if (rData.getLength() != 0) {
124 memcpy( _seq->data() + nPos,
125 rData.getConstArray(),
126 rData.getLength() );
129 void BSeqOutputStream::flush()
133 void BSeqOutputStream::closeOutput()
137 Reference< io::XInputStream > createInputStream( std::vector<sal_Int8> const & rInData )
139 return new BSeqInputStream( rInData );
142 Reference< io::XInputStream > createInputStream( const sal_Int8* pData, int len )
144 std::vector<sal_Int8> rInData(len);
145 if (len != 0) {
146 memcpy( rInData.data(), pData, len);
148 return new BSeqInputStream( rInData );
151 Reference< io::XOutputStream > createOutputStream( std::vector<sal_Int8> * pOutData )
153 return new BSeqOutputStream( pOutData );
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */