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      */
InputStreamToXInputStreamAdapter(InputStream in)48     public InputStreamToXInputStreamAdapter (InputStream in)
49 	{
50         iIn = in;
51     }
52 
available()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 
closeInput()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 
readBytes(byte[][] b, int len)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 (len >iIn.available()) {
85 			bytesRead = iIn.read(b[0], 0, iIn.available());
86 	    }
87 	    else{
88 			bytesRead = iIn.read(b[0], 0, len);
89 	    }
90             // Casting bytesRead to an int is okay, since the user can
91             // only pass in an integer length to read, so the bytesRead
92             // must <= len.
93             //
94             if (bytesRead <= 0) {
95                 return(0);
96 	    }
97 	    return ((int)bytesRead);
98 
99 
100         } catch (IOException e) {
101             throw new com.sun.star.io.IOException("reader error: "+e.toString());
102         }
103     }
104 
readSomeBytes(byte[][] b, int len)105     public int readSomeBytes(byte[][] b, int len) throws
106 			com.sun.star.io.IOException
107 	{
108         int count = 0;
109         try {
110 	    long bytesRead=0;
111 	    if (len >iIn.available()) {
112 			bytesRead = iIn.read(b[0], 0, iIn.available());
113 	    }
114 	    else{
115 			bytesRead = iIn.read(b[0], 0, len);
116 	    }
117             // Casting bytesRead to an int is okay, since the user can
118             // only pass in an integer length to read, so the bytesRead
119             // must <= len.
120             //
121             if (bytesRead <= 0) {
122                 return(0);
123 	    }
124 	    return ((int)bytesRead);
125 
126 
127         } catch (IOException e) {
128             throw new com.sun.star.io.IOException("reader error: "+e.toString());
129         }
130     }
131 
skipBytes(int n)132     public void skipBytes(int n) throws
133 			com.sun.star.io.IOException
134 	{
135         int avail;
136         int tmpLongVal = n;
137         int  tmpIntVal;
138 
139         try {
140             avail = iIn.available();
141         } catch (IOException e) {
142             throw new com.sun.star.io.IOException(e.toString());
143         }
144 
145         do {
146             if (tmpLongVal >= Integer.MAX_VALUE) {
147                tmpIntVal = Integer.MAX_VALUE;
148             } else {
149                // Casting is safe here.
150                tmpIntVal = (int)tmpLongVal;
151             }
152             tmpLongVal -= tmpIntVal;
153 
154             try {
155                 iIn.skip(tmpIntVal);
156             } catch (IOException e) {
157                 throw new com.sun.star.io.IOException(e.toString());
158             }
159         } while (tmpLongVal > 0);
160     }
161 }
162 
163