1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package com.sun.star.lib.uno.adapter;
28 
29 import java.io.IOException;
30 import com.sun.star.io.XInputStream;
31 import java.io.InputStream;
32 
33 /**	The <code>InputStreamToInputXStreamAdapter</code> wraps the
34 	Java <code>InputStream</code> object into a
35 	UNO <code>XInputStream</code> object.
36 	This allows users to access an <code>InputStream</code>
37 	as if it were an <code>XInputStream</code>.
38  */
39 public class InputStreamToXInputStreamAdapter implements XInputStream {
40 
41     /**
42      *  Internal store to the InputStream
43      */
44     private InputStream iIn;
45 
46     /**
47      *  Constructor.
48      *
49      *  @param  in  The <code>XInputStream</code> to be
50      *              accessed as an <code>InputStream</code>.
51      */
52     public InputStreamToXInputStreamAdapter (InputStream in)
53 	{
54         iIn = in;
55     }
56 
57     public int available() throws
58 			com.sun.star.io.IOException
59 	{
60 
61         int bytesAvail;
62 
63         try {
64             bytesAvail = iIn.available();
65         } catch (IOException e) {
66             throw new com.sun.star.io.IOException(e.toString());
67         }
68 
69         return(bytesAvail);
70     }
71 
72     public void closeInput() throws
73 			com.sun.star.io.IOException
74 	{
75         try {
76             iIn.close();
77         } catch (IOException e) {
78             throw new com.sun.star.io.IOException(e.toString());
79         }
80     }
81 
82     public int readBytes(byte[][] b, int len) throws
83 			com.sun.star.io.IOException
84 	{
85         int count = 0;
86         try {
87 	    long bytesRead=0;
88 	    if (len >iIn.available()) {
89 			bytesRead = iIn.read(b[0], 0, iIn.available());
90 	    }
91 	    else{
92 			bytesRead = iIn.read(b[0], 0, len);
93 	    }
94             // Casting bytesRead to an int is okay, since the user can
95             // only pass in an integer length to read, so the bytesRead
96             // must <= len.
97             //
98             if (bytesRead <= 0) {
99                 return(0);
100 	    }
101 	    return ((int)bytesRead);
102 
103 
104         } catch (IOException e) {
105             throw new com.sun.star.io.IOException("reader error: "+e.toString());
106         }
107     }
108 
109     public int readSomeBytes(byte[][] b, int len) throws
110 			com.sun.star.io.IOException
111 	{
112         int count = 0;
113         try {
114 	    long bytesRead=0;
115 	    if (len >iIn.available()) {
116 			bytesRead = iIn.read(b[0], 0, iIn.available());
117 	    }
118 	    else{
119 			bytesRead = iIn.read(b[0], 0, len);
120 	    }
121             // Casting bytesRead to an int is okay, since the user can
122             // only pass in an integer length to read, so the bytesRead
123             // must <= len.
124             //
125             if (bytesRead <= 0) {
126                 return(0);
127 	    }
128 	    return ((int)bytesRead);
129 
130 
131         } catch (IOException e) {
132             throw new com.sun.star.io.IOException("reader error: "+e.toString());
133         }
134     }
135 
136     public void skipBytes(int n) throws
137 			com.sun.star.io.IOException
138 	{
139         int avail;
140         int tmpLongVal = n;
141         int  tmpIntVal;
142 
143         try {
144             avail = iIn.available();
145         } catch (IOException e) {
146             throw new com.sun.star.io.IOException(e.toString());
147         }
148 
149         do {
150             if (tmpLongVal >= Integer.MAX_VALUE) {
151                tmpIntVal = Integer.MAX_VALUE;
152             } else {
153                // Casting is safe here.
154                tmpIntVal = (int)tmpLongVal;
155             }
156             tmpLongVal -= tmpIntVal;
157 
158             try {
159                 iIn.skip(tmpIntVal);
160             } catch (IOException e) {
161                 throw new com.sun.star.io.IOException(e.toString());
162             }
163         } while (tmpLongVal > 0);
164     }
165 }
166 
167