1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmlscript.hxx"
26 #include <rtl/memory.h>
27
28 #include <cppuhelper/implbase1.hxx>
29 #include <xmlscript/xml_helper.hxx>
30
31
32 using namespace rtl;
33 using namespace osl;
34 using namespace com::sun::star;
35 using namespace com::sun::star::uno;
36
37
38 namespace xmlscript
39 {
40
41 //==================================================================================================
42 class BSeqInputStream
43 : public ::cppu::WeakImplHelper1< io::XInputStream >
44 {
45 ByteSequence _seq;
46 sal_Int32 _nPos;
47
48 public:
49 inline BSeqInputStream( ByteSequence const & rSeq )
50 SAL_THROW( () )
51 : _seq( rSeq )
52 , _nPos( 0 )
53 {}
54
55 // XInputStream
56 virtual sal_Int32 SAL_CALL readBytes(
57 Sequence< sal_Int8 > & rData, sal_Int32 nBytesToRead )
58 throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException);
59 virtual sal_Int32 SAL_CALL readSomeBytes(
60 Sequence< sal_Int8 > & rData, sal_Int32 nMaxBytesToRead )
61 throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException);
62 virtual void SAL_CALL skipBytes(
63 sal_Int32 nBytesToSkip )
64 throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException);
65 virtual sal_Int32 SAL_CALL available()
66 throw (io::NotConnectedException, io::IOException, RuntimeException);
67 virtual void SAL_CALL closeInput()
68 throw (io::NotConnectedException, io::IOException, RuntimeException);
69 };
70 //__________________________________________________________________________________________________
readBytes(Sequence<sal_Int8> & rData,sal_Int32 nBytesToRead)71 sal_Int32 BSeqInputStream::readBytes(
72 Sequence< sal_Int8 > & rData, sal_Int32 nBytesToRead )
73 throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException)
74 {
75 nBytesToRead = ((nBytesToRead > _seq.getLength() - _nPos)
76 ? _seq.getLength() - _nPos
77 : nBytesToRead);
78
79 ByteSequence aBytes( _seq.getConstArray() + _nPos, nBytesToRead );
80 rData = toUnoSequence( aBytes );
81 _nPos += nBytesToRead;
82 return nBytesToRead;
83 }
84 //__________________________________________________________________________________________________
readSomeBytes(Sequence<sal_Int8> & rData,sal_Int32 nMaxBytesToRead)85 sal_Int32 BSeqInputStream::readSomeBytes(
86 Sequence< sal_Int8 > & rData, sal_Int32 nMaxBytesToRead )
87 throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException)
88 {
89 return readBytes( rData, nMaxBytesToRead );
90 }
91 //__________________________________________________________________________________________________
skipBytes(sal_Int32)92 void BSeqInputStream::skipBytes(
93 sal_Int32 /*nBytesToSkip*/ )
94 throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException)
95 {
96 }
97 //__________________________________________________________________________________________________
available()98 sal_Int32 BSeqInputStream::available()
99 throw (io::NotConnectedException, io::IOException, RuntimeException)
100 {
101 return (_seq.getLength() - _nPos);
102 }
103 //__________________________________________________________________________________________________
closeInput()104 void BSeqInputStream::closeInput()
105 throw (io::NotConnectedException, io::IOException, RuntimeException)
106 {
107 }
108
109 //##################################################################################################
110
111 //==================================================================================================
112 class BSeqOutputStream
113 : public ::cppu::WeakImplHelper1< io::XOutputStream >
114 {
115 ByteSequence * _seq;
116
117 public:
118 inline BSeqOutputStream( ByteSequence * seq )
119 SAL_THROW( () )
120 : _seq( seq )
121 {}
122
123 // XOutputStream
124 virtual void SAL_CALL writeBytes(
125 Sequence< sal_Int8 > const & rData )
126 throw (io::NotConnectedException, io::BufferSizeExceededException, RuntimeException);
127 virtual void SAL_CALL flush()
128 throw (io::NotConnectedException, io::BufferSizeExceededException, RuntimeException);
129 virtual void SAL_CALL closeOutput()
130 throw (io::NotConnectedException, io::BufferSizeExceededException, RuntimeException);
131 };
132 //__________________________________________________________________________________________________
writeBytes(Sequence<sal_Int8> const & rData)133 void BSeqOutputStream::writeBytes( Sequence< sal_Int8 > const & rData )
134 throw (io::NotConnectedException, io::BufferSizeExceededException, RuntimeException)
135 {
136 sal_Int32 nPos = _seq->getLength();
137 _seq->realloc( nPos + rData.getLength() );
138 ::rtl_copyMemory( (char *)_seq->getArray() + nPos,
139 (char const *)rData.getConstArray(),
140 rData.getLength() );
141 }
142 //__________________________________________________________________________________________________
flush()143 void BSeqOutputStream::flush()
144 throw (io::NotConnectedException, io::BufferSizeExceededException, RuntimeException)
145 {
146 }
147 //__________________________________________________________________________________________________
closeOutput()148 void BSeqOutputStream::closeOutput()
149 throw (io::NotConnectedException, io::BufferSizeExceededException, RuntimeException)
150 {
151 }
152
153 //##################################################################################################
154
155 //==================================================================================================
createInputStream(ByteSequence const & rInData)156 Reference< io::XInputStream > SAL_CALL createInputStream( ByteSequence const & rInData )
157 SAL_THROW( () )
158 {
159 return new BSeqInputStream( rInData );
160 }
161
162 //==================================================================================================
createOutputStream(ByteSequence * pOutData)163 Reference< io::XOutputStream > SAL_CALL createOutputStream( ByteSequence * pOutData )
164 SAL_THROW( () )
165 {
166 return new BSeqOutputStream( pOutData );
167 }
168
169 }
170