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 package com.sun.star.lib.uno.adapter;
25 
26 import java.io.IOException;
27 import com.sun.star.io.XOutputStream;
28 import java.io.OutputStream;
29 
30 /**
31  * The <code>XOutputStreamToOutputStreamAdapter</code> wraps
32  * the UNO <code>XOutputStream</code> object in a Java
33  * <code>OutputStream</code>.  This allows users to access
34  * an <code>XOutputStream</code> as if it were an
35  * <code>OutputStream</code>.
36  *
37  * @author  Brian Cameron
38  */
39 public class XOutputStreamToOutputStreamAdapter extends OutputStream {
40 
41     /**
42      *  Internal handle to the XInputStream
43      */
44     XOutputStream xout;
45 
46     /**
47      *  Constructor.
48      *
49      *  @param  out  The <code>XOutputStream</code> to be
50      *          accessed as an <code>OutputStream</code>.
51      */
XOutputStreamToOutputStreamAdapter(XOutputStream out)52     public XOutputStreamToOutputStreamAdapter(XOutputStream out) {
53         xout = out;
54     }
55 
close()56     public void close() throws IOException {
57         try {
58             xout.closeOutput();
59         } catch (Exception e) {
60             throw new IOException(e.toString());
61         }
62     }
63 
flush()64     public void flush() throws IOException {
65         try {
66             xout.flush();
67         } catch (Exception e) {
68             throw new IOException(e.toString());
69         }
70     }
71 
write(byte[] b)72     public void write(byte[] b) throws IOException {
73 
74 	try {
75             xout.writeBytes(b);
76         } catch (Exception e) {
77             throw new IOException(e.toString());
78         }
79     }
80 
write(byte[] b, int off, int len)81     public void write(byte[] b, int off, int len) throws IOException {
82 
83         byte[] tmp = new byte[len];
84 
85         // Copy the input array into a temp array, and write it out.
86         //
87         System.arraycopy(b, off, tmp, 0, len);
88 
89         try {
90             xout.writeBytes(tmp);
91         } catch (Exception e) {
92             throw new IOException(e.toString());
93         }
94     }
95 
write(int b)96     public void write(int b) throws IOException {
97 
98         byte [] oneByte = new byte [1];
99         oneByte[0] = (byte) b;
100 
101         try {
102             xout.writeBytes(oneByte);
103         } catch (Exception e) {
104             throw new IOException(e.toString());
105         }
106     }
107 }
108