Update ooo320-m1
[ooovba.git] / javaunohelper / com / sun / star / lib / uno / adapter / XInputStreamToInputStreamAdapter.java
blob28622a52055302de64431df60e9c1ba1e560595e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XInputStreamToInputStreamAdapter.java,v $
10 * $Revision: 1.7 $
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 /**
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 {
47 /**
48 * Internal handle to the XInputStream
50 private XInputStream xin;
52 /**
53 * Constructor.
55 * @param in The <code>XInputStream</code> to be
56 * accessed as an <code>InputStream</code>.
58 public XInputStreamToInputStreamAdapter (XInputStream in) {
59 xin = in;
62 public int available() throws IOException {
64 int bytesAvail;
66 try {
67 bytesAvail = xin.available();
68 } catch (Exception e) {
69 throw new IOException(e.toString());
72 return(bytesAvail);
75 public void close() throws IOException {
76 try {
77 xin.closeInput();
78 } catch (Exception e) {
79 throw new IOException(e.toString());
83 public int read () throws IOException {
84 byte [][] tmp = new byte [1][1];
85 try {
86 long bytesRead = xin.readBytes(tmp, 1);
88 if (bytesRead <= 0) {
89 return (-1);
90 } else {
91 int tmpInt = tmp[0][0];
92 if (tmpInt< 0 ){
93 tmpInt = 256 +tmpInt;
95 return(tmpInt);
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];
106 int bytesRead;
108 try {
109 bytesRead = xin.readBytes(tmp, b.length);
110 if (bytesRead <= 0) {
111 return(-1);
112 } else if (bytesRead < b.length) {
113 System.arraycopy(tmp[0], 0, b, 0, bytesRead);
114 } else {
115 System.arraycopy(tmp[0], 0, b, 0, b.length);
117 } catch (Exception e) {
118 throw new IOException(e.toString());
121 return (bytesRead);
124 public int read(byte[] b, int off, int len) throws IOException {
125 int count = 0;
126 byte [][] tmp = new byte [1][b.length];
127 try {
128 long bytesRead=0;
129 int av = xin.available();
130 if ( av != 0 && len > av) {
131 bytesRead = xin.readBytes(tmp, av);
133 else{
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
138 // must <= len.
140 if (bytesRead <= 0) {
141 return(-1);
142 } else if (bytesRead < len) {
143 System.arraycopy(tmp[0], 0, b, off, (int)bytesRead);
144 } else {
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 {
158 int avail;
159 long tmpLongVal = n;
160 int tmpIntVal;
162 try {
163 avail = xin.available();
164 } catch (Exception e) {
165 throw new IOException(e.toString());
168 do {
169 if (tmpLongVal >= Integer.MAX_VALUE) {
170 tmpIntVal = Integer.MAX_VALUE;
171 } else {
172 // Casting is safe here.
173 tmpIntVal = (int)tmpLongVal;
175 tmpLongVal -= tmpIntVal;
177 try {
178 xin.skipBytes(tmpIntVal);
179 } catch (Exception e) {
180 throw new IOException(e.toString());
182 } while (tmpLongVal > 0);
184 if ( avail != 0 && avail < n) {
185 return(avail);
186 } else {
187 return(n);
192 * Tests if this input stream supports the mark and reset methods.
193 * The markSupported method of
194 * <code>XInputStreamToInputStreamAdapter</code> returns false.
196 * @returns false
198 public boolean markSupported() {
199 return false;
202 public void mark(int readlimit) {
203 // Not supported.
206 public void reset() throws IOException {
207 // Not supported.