Update ooo320-m1
[ooovba.git] / javaunohelper / com / sun / star / lib / uno / adapter / InputStreamToXInputStreamAdapter.java
blobdf5b5450ebd6aea8149a6fd212068cd1fef47187
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: InputStreamToXInputStreamAdapter.java,v $
10 * $Revision: 1.5 $
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 {
44 /**
45 * Internal store to the InputStream
47 private InputStream iIn;
49 /**
50 * Constructor.
52 * @param in The <code>XInputStream</code> to be
53 * accessed as an <code>InputStream</code>.
55 public InputStreamToXInputStreamAdapter (InputStream in)
57 iIn = in;
60 public int available() throws
61 com.sun.star.io.IOException
64 int bytesAvail;
66 try {
67 bytesAvail = iIn.available();
68 } catch (IOException e) {
69 throw new com.sun.star.io.IOException(e.toString());
72 return(bytesAvail);
75 public void closeInput() throws
76 com.sun.star.io.IOException
78 try {
79 iIn.close();
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
88 int count = 0;
89 try {
90 long bytesRead=0;
91 if (len >iIn.available()) {
92 bytesRead = iIn.read(b[0], 0, iIn.available());
94 else{
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
99 // must <= len.
101 if (bytesRead <= 0) {
102 return(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
115 int count = 0;
116 try {
117 long bytesRead=0;
118 if (len >iIn.available()) {
119 bytesRead = iIn.read(b[0], 0, iIn.available());
121 else{
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
126 // must <= len.
128 if (bytesRead <= 0) {
129 return(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
142 int avail;
143 int tmpLongVal = n;
144 int tmpIntVal;
146 try {
147 avail = iIn.available();
148 } catch (IOException e) {
149 throw new com.sun.star.io.IOException(e.toString());
152 do {
153 if (tmpLongVal >= Integer.MAX_VALUE) {
154 tmpIntVal = Integer.MAX_VALUE;
155 } else {
156 // Casting is safe here.
157 tmpIntVal = (int)tmpLongVal;
159 tmpLongVal -= tmpIntVal;
161 try {
162 iIn.skip(tmpIntVal);
163 } catch (IOException e) {
164 throw new com.sun.star.io.IOException(e.toString());
166 } while (tmpLongVal > 0);