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 package com.sun.star.lib.uno.adapter;
24 
25 import java.io.IOException;
26 import com.sun.star.io.XInputStream;
27 import java.io.InputStream;
28 
29 /**
30  * The <code>XInputStreamToInputStreamAdapter</code> wraps
31  * the UNO <code>XInputStream</code> object in a Java
32  * <code>InputStream</code>.  This allows users to access
33  * an <code>XInputStream</code> as if it were an
34  * <code>InputStream</code>.
35  *
36  * @author  Brian Cameron
37  */
38 public class XInputStreamToInputStreamAdapter extends InputStream {
39 
40     /**
41      *  Internal handle to the XInputStream
42      */
43     private XInputStream xin;
44 
45     /**
46      *  Constructor.
47      *
48      *  @param  in  The <code>XInputStream</code> to be
49      *              accessed as an <code>InputStream</code>.
50      */
XInputStreamToInputStreamAdapter(XInputStream in)51     public XInputStreamToInputStreamAdapter (XInputStream in) {
52         xin = in;
53     }
54 
available()55     public int available() throws IOException {
56 
57         int bytesAvail;
58 
59         try {
60             bytesAvail = xin.available();
61         } catch (Exception e) {
62             throw new IOException(e.toString());
63         }
64 
65         return(bytesAvail);
66     }
67 
close()68     public void close() throws IOException {
69         try {
70             xin.closeInput();
71         } catch (Exception e) {
72             throw new IOException(e.toString());
73         }
74     }
75 
read()76     public int read () throws IOException {
77         byte [][] tmp = new byte [1][1];
78         try {
79             long bytesRead = xin.readBytes(tmp, 1);
80 
81             if (bytesRead <= 0) {
82                return (-1);
83             } else {
84 		int tmpInt = tmp[0][0];
85 		if (tmpInt< 0 ){
86 		    tmpInt = 256 +tmpInt;
87 		}
88                 return(tmpInt);
89             }
90 
91         } catch (Exception e) {
92             throw new IOException(e.toString());
93         }
94     }
95 
read(byte[] b)96     public int read (byte[] b) throws IOException {
97 
98         byte [][] tmp = new byte [1][b.length];
99         int bytesRead;
100 
101         try {
102             bytesRead = xin.readBytes(tmp, b.length);
103             if (bytesRead <= 0) {
104                 return(-1);
105             } else if (bytesRead < b.length) {
106                 System.arraycopy(tmp[0], 0, b, 0, bytesRead);
107             } else {
108                 System.arraycopy(tmp[0], 0, b, 0, b.length);
109             }
110         } catch (Exception e) {
111             throw new IOException(e.toString());
112         }
113 
114         return (bytesRead);
115     }
116 
read(byte[] b, int off, int len)117     public int read(byte[] b, int off, int len) throws IOException {
118         int count = 0;
119         byte [][] tmp = new byte [1][b.length];
120         try {
121 	    long bytesRead=0;
122             int av = xin.available();
123 	    if ( av != 0 && len > av) {
124 		  bytesRead = xin.readBytes(tmp, av);
125 	    }
126 	    else{
127 		bytesRead = xin.readBytes(tmp,len);
128 	    }
129             // Casting bytesRead to an int is okay, since the user can
130             // only pass in an integer length to read, so the bytesRead
131             // must <= len.
132             //
133             if (bytesRead <= 0) {
134                 return(-1);
135 	    } else if (bytesRead < len) {
136 		System.arraycopy(tmp[0], 0, b, off, (int)bytesRead);
137 	    } else {
138                 System.arraycopy(tmp[0], 0, b, off, len);
139 	    }
140 
141 	    return ((int)bytesRead);
142 
143 
144         } catch (Exception e) {
145             throw new IOException("reader error: "+e.toString());
146         }
147     }
148 
skip(long n)149     public long skip(long n) throws IOException {
150 
151         int avail;
152         long tmpLongVal = n;
153         int  tmpIntVal;
154 
155         try {
156             avail = xin.available();
157         } catch (Exception e) {
158             throw new IOException(e.toString());
159         }
160 
161         do {
162             if (tmpLongVal >= Integer.MAX_VALUE) {
163                tmpIntVal = Integer.MAX_VALUE;
164             } else {
165                // Casting is safe here.
166                tmpIntVal = (int)tmpLongVal;
167             }
168             tmpLongVal -= tmpIntVal;
169 
170             try {
171                 xin.skipBytes(tmpIntVal);
172             } catch (Exception e) {
173                 throw new IOException(e.toString());
174             }
175         } while (tmpLongVal > 0);
176 
177         if ( avail != 0 && avail < n) {
178             return(avail);
179         } else {
180             return(n);
181         }
182     }
183 
184    /**
185     *  Tests if this input stream supports the mark and reset methods.
186     *  The markSupported method of
187     *  <code>XInputStreamToInputStreamAdapter</code> returns false.
188     *
189     *  @returns  false
190     */
markSupported()191     public boolean markSupported() {
192        return false;
193     }
194 
mark(int readlimit)195     public void mark(int readlimit) {
196         // Not supported.
197     }
198 
reset()199     public void reset() throws IOException {
200         // Not supported.
201     }
202 }
203 
204