1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: InputStreamToXInputStreamAdapter.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package com
.sun
.star
.lib
.uno
.adapter
;
32 import java
.io
.IOException
;
33 import com
.sun
.star
.io
.XInputStream
;
34 import java
.io
.InputStream
;
36 /** The <code>InputStreamToInputXStreamAdapter</code> wraps the
37 Java <code>InputStream</code> object into a
38 UNO <code>XInputStream</code> object.
39 This allows users to access an <code>InputStream</code>
40 as if it were an <code>XInputStream</code>.
42 public class InputStreamToXInputStreamAdapter
implements XInputStream
{
45 * Internal store to the InputStream
47 private InputStream iIn
;
52 * @param in The <code>XInputStream</code> to be
53 * accessed as an <code>InputStream</code>.
55 public InputStreamToXInputStreamAdapter (InputStream in
)
60 public int available() throws
61 com
.sun
.star
.io
.IOException
67 bytesAvail
= iIn
.available();
68 } catch (IOException e
) {
69 throw new com
.sun
.star
.io
.IOException(e
.toString());
75 public void closeInput() throws
76 com
.sun
.star
.io
.IOException
80 } catch (IOException e
) {
81 throw new com
.sun
.star
.io
.IOException(e
.toString());
85 public int readBytes(byte[][] b
, int len
) throws
86 com
.sun
.star
.io
.IOException
91 if (len
>iIn
.available()) {
92 bytesRead
= iIn
.read(b
[0], 0, iIn
.available());
95 bytesRead
= iIn
.read(b
[0], 0, len
);
97 // Casting bytesRead to an int is okay, since the user can
98 // only pass in an integer length to read, so the bytesRead
101 if (bytesRead
<= 0) {
104 return ((int)bytesRead
);
107 } catch (IOException e
) {
108 throw new com
.sun
.star
.io
.IOException("reader error: "+e
.toString());
112 public int readSomeBytes(byte[][] b
, int len
) throws
113 com
.sun
.star
.io
.IOException
118 if (len
>iIn
.available()) {
119 bytesRead
= iIn
.read(b
[0], 0, iIn
.available());
122 bytesRead
= iIn
.read(b
[0], 0, len
);
124 // Casting bytesRead to an int is okay, since the user can
125 // only pass in an integer length to read, so the bytesRead
128 if (bytesRead
<= 0) {
131 return ((int)bytesRead
);
134 } catch (IOException e
) {
135 throw new com
.sun
.star
.io
.IOException("reader error: "+e
.toString());
139 public void skipBytes(int n
) throws
140 com
.sun
.star
.io
.IOException
147 avail
= iIn
.available();
148 } catch (IOException e
) {
149 throw new com
.sun
.star
.io
.IOException(e
.toString());
153 if (tmpLongVal
>= Integer
.MAX_VALUE
) {
154 tmpIntVal
= Integer
.MAX_VALUE
;
156 // Casting is safe here.
157 tmpIntVal
= (int)tmpLongVal
;
159 tmpLongVal
-= tmpIntVal
;
163 } catch (IOException e
) {
164 throw new com
.sun
.star
.io
.IOException(e
.toString());
166 } while (tmpLongVal
> 0);