1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlscript.hxx"
30 #include <rtl/memory.h>
31 
32 #include <cppuhelper/implbase1.hxx>
33 #include <xmlscript/xml_helper.hxx>
34 
35 
36 using namespace rtl;
37 using namespace osl;
38 using namespace com::sun::star;
39 using namespace com::sun::star::uno;
40 
41 
42 namespace xmlscript
43 {
44 
45 //==================================================================================================
46 class BSeqInputStream
47 	: public ::cppu::WeakImplHelper1< io::XInputStream >
48 {
49 	ByteSequence _seq;
50 	sal_Int32 _nPos;
51 
52 public:
53 	inline BSeqInputStream( ByteSequence const & rSeq )
54 		SAL_THROW( () )
55 		: _seq( rSeq )
56 		, _nPos( 0 )
57 		{}
58 
59 	// XInputStream
60     virtual sal_Int32 SAL_CALL readBytes(
61 		Sequence< sal_Int8 > & rData, sal_Int32 nBytesToRead )
62 		throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException);
63 	virtual sal_Int32 SAL_CALL readSomeBytes(
64 		Sequence< sal_Int8 > & rData, sal_Int32 nMaxBytesToRead )
65 		throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException);
66     virtual void SAL_CALL skipBytes(
67 		sal_Int32 nBytesToSkip )
68 		throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException);
69     virtual sal_Int32 SAL_CALL available()
70 		throw (io::NotConnectedException, io::IOException, RuntimeException);
71     virtual void SAL_CALL closeInput()
72 		throw (io::NotConnectedException, io::IOException, RuntimeException);
73 };
74 //__________________________________________________________________________________________________
75 sal_Int32 BSeqInputStream::readBytes(
76 	Sequence< sal_Int8 > & rData, sal_Int32 nBytesToRead )
77 	throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException)
78 {
79 	nBytesToRead = ((nBytesToRead > _seq.getLength() - _nPos)
80 					? _seq.getLength() - _nPos
81 					: nBytesToRead);
82 
83 	ByteSequence aBytes( _seq.getConstArray() + _nPos, nBytesToRead );
84 	rData = toUnoSequence( aBytes );
85 	_nPos += nBytesToRead;
86 	return nBytesToRead;
87 }
88 //__________________________________________________________________________________________________
89 sal_Int32 BSeqInputStream::readSomeBytes(
90 	Sequence< sal_Int8 > & rData, sal_Int32 nMaxBytesToRead )
91 	throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException)
92 {
93 	return readBytes( rData, nMaxBytesToRead );
94 }
95 //__________________________________________________________________________________________________
96 void BSeqInputStream::skipBytes(
97 	sal_Int32 /*nBytesToSkip*/ )
98 	throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, RuntimeException)
99 {
100 }
101 //__________________________________________________________________________________________________
102 sal_Int32 BSeqInputStream::available()
103 	throw (io::NotConnectedException, io::IOException, RuntimeException)
104 {
105 	return (_seq.getLength() - _nPos);
106 }
107 //__________________________________________________________________________________________________
108 void BSeqInputStream::closeInput()
109 	throw (io::NotConnectedException, io::IOException, RuntimeException)
110 {
111 }
112 
113 //##################################################################################################
114 
115 //==================================================================================================
116 class BSeqOutputStream
117 	: public ::cppu::WeakImplHelper1< io::XOutputStream >
118 {
119 	ByteSequence * _seq;
120 
121 public:
122 	inline BSeqOutputStream( ByteSequence * seq )
123 		SAL_THROW( () )
124 		: _seq( seq )
125 		{}
126 
127 	// XOutputStream
128     virtual void SAL_CALL writeBytes(
129 		Sequence< sal_Int8 > const & rData )
130 		throw (io::NotConnectedException, io::BufferSizeExceededException, RuntimeException);
131     virtual void SAL_CALL flush()
132 		throw (io::NotConnectedException, io::BufferSizeExceededException, RuntimeException);
133     virtual void SAL_CALL closeOutput()
134 		throw (io::NotConnectedException, io::BufferSizeExceededException, RuntimeException);
135 };
136 //__________________________________________________________________________________________________
137 void BSeqOutputStream::writeBytes( Sequence< sal_Int8 > const & rData )
138 	throw (io::NotConnectedException, io::BufferSizeExceededException, RuntimeException)
139 {
140 	sal_Int32 nPos = _seq->getLength();
141 	_seq->realloc( nPos + rData.getLength() );
142 	::rtl_copyMemory( (char *)_seq->getArray() + nPos,
143 					  (char const *)rData.getConstArray(),
144 					  rData.getLength() );
145 }
146 //__________________________________________________________________________________________________
147 void BSeqOutputStream::flush()
148 	throw (io::NotConnectedException, io::BufferSizeExceededException, RuntimeException)
149 {
150 }
151 //__________________________________________________________________________________________________
152 void BSeqOutputStream::closeOutput()
153 	throw (io::NotConnectedException, io::BufferSizeExceededException, RuntimeException)
154 {
155 }
156 
157 //##################################################################################################
158 
159 //==================================================================================================
160 Reference< io::XInputStream > SAL_CALL createInputStream( ByteSequence const & rInData )
161 	SAL_THROW( () )
162 {
163 	return new BSeqInputStream( rInData );
164 }
165 
166 //==================================================================================================
167 Reference< io::XOutputStream > SAL_CALL createOutputStream( ByteSequence * pOutData )
168 	SAL_THROW( () )
169 {
170 	return new BSeqOutputStream( pOutData );
171 }
172 
173 }
174