1*a5b190bfSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a5b190bfSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a5b190bfSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a5b190bfSAndrew Rist  * distributed with this work for additional information
6*a5b190bfSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a5b190bfSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a5b190bfSAndrew Rist  * "License"); you may not use this file except in compliance
9*a5b190bfSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a5b190bfSAndrew Rist  *
11*a5b190bfSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a5b190bfSAndrew Rist  *
13*a5b190bfSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a5b190bfSAndrew Rist  * software distributed under the License is distributed on an
15*a5b190bfSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a5b190bfSAndrew Rist  * KIND, either express or implied.  See the License for the
17*a5b190bfSAndrew Rist  * specific language governing permissions and limitations
18*a5b190bfSAndrew Rist  * under the License.
19*a5b190bfSAndrew Rist  *
20*a5b190bfSAndrew Rist  *************************************************************/
21*a5b190bfSAndrew Rist 
22*a5b190bfSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.lib.uno.adapter;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.IOException;
27cdf0e10cSrcweir import com.sun.star.io.XOutputStream;
28cdf0e10cSrcweir import java.io.OutputStream;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir /**
31cdf0e10cSrcweir  * The <code>XOutputStreamToOutputStreamAdapter</code> wraps
32cdf0e10cSrcweir  * the UNO <code>XOutputStream</code> object in a Java
33cdf0e10cSrcweir  * <code>OutputStream</code>.  This allows users to access
34cdf0e10cSrcweir  * an <code>XOutputStream</code> as if it were an
35cdf0e10cSrcweir  * <code>OutputStream</code>.
36cdf0e10cSrcweir  *
37cdf0e10cSrcweir  * @author  Brian Cameron
38cdf0e10cSrcweir  */
39cdf0e10cSrcweir public class XOutputStreamToOutputStreamAdapter extends OutputStream {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir     /**
42cdf0e10cSrcweir      *  Internal handle to the XInputStream
43cdf0e10cSrcweir      */
44cdf0e10cSrcweir     XOutputStream xout;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     /**
47cdf0e10cSrcweir      *  Constructor.
48cdf0e10cSrcweir      *
49cdf0e10cSrcweir      *  @param  out  The <code>XOutputStream</code> to be
50cdf0e10cSrcweir      *          accessed as an <code>OutputStream</code>.
51cdf0e10cSrcweir      */
XOutputStreamToOutputStreamAdapter(XOutputStream out)52cdf0e10cSrcweir     public XOutputStreamToOutputStreamAdapter(XOutputStream out) {
53cdf0e10cSrcweir         xout = out;
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir 
close()56cdf0e10cSrcweir     public void close() throws IOException {
57cdf0e10cSrcweir         try {
58cdf0e10cSrcweir             xout.closeOutput();
59cdf0e10cSrcweir         } catch (Exception e) {
60cdf0e10cSrcweir             throw new IOException(e.toString());
61cdf0e10cSrcweir         }
62cdf0e10cSrcweir     }
63cdf0e10cSrcweir 
flush()64cdf0e10cSrcweir     public void flush() throws IOException {
65cdf0e10cSrcweir         try {
66cdf0e10cSrcweir             xout.flush();
67cdf0e10cSrcweir         } catch (Exception e) {
68cdf0e10cSrcweir             throw new IOException(e.toString());
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir 
write(byte[] b)72cdf0e10cSrcweir     public void write(byte[] b) throws IOException {
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	try {
75cdf0e10cSrcweir             xout.writeBytes(b);
76cdf0e10cSrcweir         } catch (Exception e) {
77cdf0e10cSrcweir             throw new IOException(e.toString());
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir 
write(byte[] b, int off, int len)81cdf0e10cSrcweir     public void write(byte[] b, int off, int len) throws IOException {
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         byte[] tmp = new byte[len];
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         // Copy the input array into a temp array, and write it out.
86cdf0e10cSrcweir         //
87cdf0e10cSrcweir         System.arraycopy(b, off, tmp, 0, len);
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         try {
90cdf0e10cSrcweir             xout.writeBytes(tmp);
91cdf0e10cSrcweir         } catch (Exception e) {
92cdf0e10cSrcweir             throw new IOException(e.toString());
93cdf0e10cSrcweir         }
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
write(int b)96cdf0e10cSrcweir     public void write(int b) throws IOException {
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         byte [] oneByte = new byte [1];
99cdf0e10cSrcweir         oneByte[0] = (byte) b;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         try {
102cdf0e10cSrcweir             xout.writeBytes(oneByte);
103cdf0e10cSrcweir         } catch (Exception e) {
104cdf0e10cSrcweir             throw new IOException(e.toString());
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir }
108