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 /**	The <code>InputStreamToInputXStreamAdapter</code> wraps the
30 	Java <code>InputStream</code> object into a
31 	UNO <code>XInputStream</code> object.
32 	This allows users to access an <code>InputStream</code>
33 	as if it were an <code>XInputStream</code>.
34  */
35 public class InputStreamToXInputStreamAdapter implements XInputStream {
36 
37     /**
38      *  Internal store to the InputStream
39      */
40     private InputStream iIn;
41 
42     /**
43      *  Constructor.
44      *
45      *  @param  in  The <code>XInputStream</code> to be
46      *              accessed as an <code>InputStream</code>.
47      */
48     public InputStreamToXInputStreamAdapter (InputStream in)
49 	{
50         iIn = in;
51     }
52 
53     public int available() throws
54 			com.sun.star.io.IOException
55 	{
56 
57         int bytesAvail;
58 
59         try {
60             bytesAvail = iIn.available();
61         } catch (IOException e) {
62             throw new com.sun.star.io.IOException(e.toString());
63         }
64 
65         return(bytesAvail);
66     }
67 
68     public void closeInput() throws
69 			com.sun.star.io.IOException
70 	{
71         try {
72             iIn.close();
73         } catch (IOException e) {
74             throw new com.sun.star.io.IOException(e.toString());
75         }
76     }
77 
78     public int readBytes(byte[][] b, int len) throws
79 			com.sun.star.io.IOException
80 	{
81         int count = 0;
82         try {
83 	    long bytesRead=0;
84             if (b[0] == null || b[0].length < len) {
85                 b[0] = new byte[len];
86             }
87 	    if (len >iIn.available()) {
88 			bytesRead = iIn.read(b[0], 0, iIn.available());
89 	    }
90 	    else{
91 			bytesRead = iIn.read(b[0], 0, len);
92 	    }
93             // Casting bytesRead to an int is okay, since the user can
94             // only pass in an integer length to read, so the bytesRead
95             // must <= len.
96             //
97             if (bytesRead < b[0].length) {
98                 int outSize = bytesRead > 0 ? (int)bytesRead : 0;
99                 byte[] out = new byte[outSize];
100                 System.arraycopy(b[0], 0, out, 0, outSize);
101                 b[0] = out;
102             }
103             if (bytesRead <= 0) {
104                 return(0);
105 	    }
106 	    return ((int)bytesRead);
107 
108 
109         } catch (IOException e) {
110             throw new com.sun.star.io.IOException("reader error: "+e.toString());
111         }
112     }
113 
114     public int readSomeBytes(byte[][] b, int len) throws
115 			com.sun.star.io.IOException
116 	{
117         int count = 0;
118         try {
119 	    long bytesRead=0;
120             if (b[0] == null || b[0].length < len) {
121                 b[0] = new byte[len];
122             }
123 	    if (len >iIn.available()) {
124 			bytesRead = iIn.read(b[0], 0, iIn.available());
125 	    }
126 	    else{
127 			bytesRead = iIn.read(b[0], 0, 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 < b[0].length) {
134                 int outSize = bytesRead > 0 ? (int)bytesRead : 0;
135                 byte[] out = new byte[outSize];
136                 System.arraycopy(b[0], 0, out, 0, outSize);
137                 b[0] = out;
138             }
139             if (bytesRead <= 0) {
140                 return(0);
141 	    }
142 	    return ((int)bytesRead);
143 
144 
145         } catch (IOException e) {
146             throw new com.sun.star.io.IOException("reader error: "+e.toString());
147         }
148     }
149 
150     public void skipBytes(int n) throws
151 			com.sun.star.io.IOException
152 	{
153         int avail;
154         int tmpLongVal = n;
155         int  tmpIntVal;
156 
157         try {
158             avail = iIn.available();
159         } catch (IOException e) {
160             throw new com.sun.star.io.IOException(e.toString());
161         }
162 
163         do {
164             if (tmpLongVal >= Integer.MAX_VALUE) {
165                tmpIntVal = Integer.MAX_VALUE;
166             } else {
167                // Casting is safe here.
168                tmpIntVal = (int)tmpLongVal;
169             }
170             tmpLongVal -= tmpIntVal;
171 
172             try {
173                 iIn.skip(tmpIntVal);
174             } catch (IOException e) {
175                 throw new com.sun.star.io.IOException(e.toString());
176             }
177         } while (tmpLongVal > 0);
178     }
179 }
180 
181