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: XInputStreamToInputStreamAdapter.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
;
37 * The <code>XInputStreamToInputStreamAdapter</code> wraps
38 * the UNO <code>XInputStream</code> object in a Java
39 * <code>InputStream</code>. This allows users to access
40 * an <code>XInputStream</code> as if it were an
41 * <code>InputStream</code>.
43 * @author Brian Cameron
45 public class XInputStreamToInputStreamAdapter
extends InputStream
{
48 * Internal handle to the XInputStream
50 private XInputStream xin
;
55 * @param in The <code>XInputStream</code> to be
56 * accessed as an <code>InputStream</code>.
58 public XInputStreamToInputStreamAdapter (XInputStream in
) {
62 public int available() throws IOException
{
67 bytesAvail
= xin
.available();
68 } catch (Exception e
) {
69 throw new IOException(e
.toString());
75 public void close() throws IOException
{
78 } catch (Exception e
) {
79 throw new IOException(e
.toString());
83 public int read () throws IOException
{
84 byte [][] tmp
= new byte [1][1];
86 long bytesRead
= xin
.readBytes(tmp
, 1);
91 int tmpInt
= tmp
[0][0];
98 } catch (Exception e
) {
99 throw new IOException(e
.toString());
103 public int read (byte[] b
) throws IOException
{
105 byte [][] tmp
= new byte [1][b
.length
];
109 bytesRead
= xin
.readBytes(tmp
, b
.length
);
110 if (bytesRead
<= 0) {
112 } else if (bytesRead
< b
.length
) {
113 System
.arraycopy(tmp
[0], 0, b
, 0, bytesRead
);
115 System
.arraycopy(tmp
[0], 0, b
, 0, b
.length
);
117 } catch (Exception e
) {
118 throw new IOException(e
.toString());
124 public int read(byte[] b
, int off
, int len
) throws IOException
{
126 byte [][] tmp
= new byte [1][b
.length
];
129 int av
= xin
.available();
130 if ( av
!= 0 && len
> av
) {
131 bytesRead
= xin
.readBytes(tmp
, av
);
134 bytesRead
= xin
.readBytes(tmp
,len
);
136 // Casting bytesRead to an int is okay, since the user can
137 // only pass in an integer length to read, so the bytesRead
140 if (bytesRead
<= 0) {
142 } else if (bytesRead
< len
) {
143 System
.arraycopy(tmp
[0], 0, b
, off
, (int)bytesRead
);
145 System
.arraycopy(tmp
[0], 0, b
, off
, len
);
148 return ((int)bytesRead
);
151 } catch (Exception e
) {
152 throw new IOException("reader error: "+e
.toString());
156 public long skip(long n
) throws IOException
{
163 avail
= xin
.available();
164 } catch (Exception e
) {
165 throw new IOException(e
.toString());
169 if (tmpLongVal
>= Integer
.MAX_VALUE
) {
170 tmpIntVal
= Integer
.MAX_VALUE
;
172 // Casting is safe here.
173 tmpIntVal
= (int)tmpLongVal
;
175 tmpLongVal
-= tmpIntVal
;
178 xin
.skipBytes(tmpIntVal
);
179 } catch (Exception e
) {
180 throw new IOException(e
.toString());
182 } while (tmpLongVal
> 0);
184 if ( avail
!= 0 && avail
< n
) {
192 * Tests if this input stream supports the mark and reset methods.
193 * The markSupported method of
194 * <code>XInputStreamToInputStreamAdapter</code> returns false.
198 public boolean markSupported() {
202 public void mark(int readlimit
) {
206 public void reset() throws IOException
{