2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package com
.sun
.star
.lib
.uno
.adapter
;
20 import java
.io
.IOException
;
22 import com
.sun
.star
.io
.XInputStream
;
24 import java
.io
.InputStream
;
27 * The <code>XInputStreamToInputStreamAdapter</code> wraps
28 * the UNO <code>XInputStream</code> object in a Java
29 * <code>InputStream</code>. This allows users to access
30 * an <code>XInputStream</code> as if it were an
31 * <code>InputStream</code>.
33 public final class XInputStreamToInputStreamAdapter
extends InputStream
{
36 * Internal handle to the XInputStream
38 private final XInputStream xin
;
43 * @param in The <code>XInputStream</code> to be
44 * accessed as an <code>InputStream</code>.
46 public XInputStreamToInputStreamAdapter (XInputStream in
) {
51 public int available() throws IOException
{
56 bytesAvail
= xin
.available();
57 } catch (Exception e
) {
58 IOException newEx
= new IOException(e
.getMessage());
67 public void close() throws IOException
{
70 } catch (Exception e
) {
71 IOException newEx
= new IOException(e
.getMessage());
78 public int read () throws IOException
{
79 byte [][] tmp
= new byte [1][1];
81 long bytesRead
= xin
.readBytes(tmp
, 1);
86 int tmpInt
= tmp
[0][0];
93 } catch (Exception e
) {
94 IOException newEx
= new IOException(e
.getMessage());
101 public int read (byte[] b
) throws IOException
{
103 byte [][] tmp
= new byte [1][b
.length
];
107 bytesRead
= xin
.readBytes(tmp
, b
.length
);
108 if (bytesRead
<= 0) {
110 } else if (bytesRead
< b
.length
) {
111 System
.arraycopy(tmp
[0], 0, b
, 0, bytesRead
);
113 System
.arraycopy(tmp
[0], 0, b
, 0, b
.length
);
115 } catch (Exception e
) {
116 IOException newEx
= new IOException(e
.getMessage());
125 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
);
149 } catch (Exception e
) {
150 IOException newEx
= new IOException(e
.getMessage());
157 public long skip(long n
) throws IOException
{
164 avail
= xin
.available();
165 } catch (Exception e
) {
166 IOException newEx
= new IOException(e
.getMessage());
172 if (tmpLongVal
>= Integer
.MAX_VALUE
) {
173 tmpIntVal
= Integer
.MAX_VALUE
;
175 // Casting is safe here.
176 tmpIntVal
= (int)tmpLongVal
;
178 tmpLongVal
-= tmpIntVal
;
181 xin
.skipBytes(tmpIntVal
);
182 } catch (Exception e
) {
183 IOException newEx
= new IOException(e
.getMessage());
187 } while (tmpLongVal
> 0);
189 if ( avail
!= 0 && avail
< n
) {
197 * Tests if this input stream supports the mark and reset methods.
198 * The markSupported method of
199 * <code>XInputStreamToInputStreamAdapter</code> returns false.
204 public boolean markSupported() {
209 public synchronized void mark(int readlimit
) {
214 public synchronized void reset() throws IOException
{