LibreOffice Module xmlscript (master) 1
xml_byteseq.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 <string.h>
21
24#include <com/sun/star/io/XInputStream.hpp>
25#include <com/sun/star/io/XOutputStream.hpp>
26
27using namespace osl;
28using namespace com::sun::star;
29using namespace com::sun::star::uno;
30
31
32namespace xmlscript
33{
34
35namespace {
36
37class BSeqInputStream
38 : public ::cppu::WeakImplHelper< io::XInputStream >
39{
40 std::vector<sal_Int8> _seq;
41 sal_Int32 _nPos;
42
43public:
44 explicit BSeqInputStream( std::vector<sal_Int8>&& rSeq )
45 : _seq( std::move(rSeq) )
46 , _nPos( 0 )
47 {}
48
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;
58};
59
60}
61
62sal_Int32 BSeqInputStream::readBytes(
63 Sequence< sal_Int8 > & rData, sal_Int32 nBytesToRead )
64{
65 nBytesToRead = ((nBytesToRead > static_cast<sal_Int32>(_seq.size()) - _nPos)
66 ? _seq.size() - _nPos
67 : nBytesToRead);
68
69 if (rData.getLength() != nBytesToRead)
70 rData.realloc( nBytesToRead );
71 if (nBytesToRead != 0) {
72 memcpy(rData.getArray(), &_seq[_nPos], nBytesToRead);
73 }
74 _nPos += nBytesToRead;
75 return nBytesToRead;
76}
77
78sal_Int32 BSeqInputStream::readSomeBytes(
79 Sequence< sal_Int8 > & rData, sal_Int32 nMaxBytesToRead )
80{
81 return readBytes( rData, nMaxBytesToRead );
82}
83
84void BSeqInputStream::skipBytes(
85 sal_Int32 /*nBytesToSkip*/ )
86{
87}
88
89sal_Int32 BSeqInputStream::available()
90{
91 return _seq.size() - _nPos;
92}
93
94void BSeqInputStream::closeInput()
95{
96}
97
98namespace {
99
100class BSeqOutputStream
101 : public ::cppu::WeakImplHelper< io::XOutputStream >
102{
103 std::vector<sal_Int8> * _seq;
104
105public:
106 explicit BSeqOutputStream( std::vector<sal_Int8> * seq )
107 : _seq( seq )
108 {}
109
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;
115};
116
117}
118
119void BSeqOutputStream::writeBytes( Sequence< sal_Int8 > const & rData )
120{
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() );
127 }
128}
129void BSeqOutputStream::flush()
130{
131}
132
133void BSeqOutputStream::closeOutput()
134{
135}
136
137Reference< io::XInputStream > createInputStream( std::vector<sal_Int8>&& rInData )
138{
139 return new BSeqInputStream( std::move(rInData) );
140}
141
143{
144 std::vector<sal_Int8> rInData(len);
145 if (len != 0) {
146 memcpy( rInData.data(), pData, len);
147 }
148 return new BSeqInputStream( std::move(rInData) );
149}
150
151Reference< io::XOutputStream > createOutputStream( std::vector<sal_Int8> * pOutData )
152{
153 return new BSeqOutputStream( pOutData );
154}
155
156}
157
158/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 nPos
std::unique_ptr< sal_Int32[]> pData
Reference< io::XInputStream > createInputStream(std::vector< sal_Int8 > &&rInData)
Reference< io::XOutputStream > createOutputStream(std::vector< sal_Int8 > *pOutData)
signed char sal_Int8
std::vector< sal_Int8 > _seq
Definition: xml_byteseq.cxx:40
sal_Int32 _nPos
Definition: xml_byteseq.cxx:41