1*a5b190bfSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*a5b190bfSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*a5b190bfSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*a5b190bfSAndrew Rist * distributed with this work for additional information 6*a5b190bfSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*a5b190bfSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*a5b190bfSAndrew Rist * "License"); you may not use this file except in compliance 9*a5b190bfSAndrew Rist * with the License. You may obtain a copy of the License at 10*a5b190bfSAndrew Rist * 11*a5b190bfSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*a5b190bfSAndrew Rist * 13*a5b190bfSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*a5b190bfSAndrew Rist * software distributed under the License is distributed on an 15*a5b190bfSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*a5b190bfSAndrew Rist * KIND, either express or implied. See the License for the 17*a5b190bfSAndrew Rist * specific language governing permissions and limitations 18*a5b190bfSAndrew Rist * under the License. 19*a5b190bfSAndrew Rist * 20*a5b190bfSAndrew Rist *************************************************************/ 21*a5b190bfSAndrew Rist 22*a5b190bfSAndrew Rist 23cdf0e10cSrcweir package com.sun.star.lib.uno.adapter; 24cdf0e10cSrcweir 25cdf0e10cSrcweir import java.io.IOException; 26cdf0e10cSrcweir import com.sun.star.io.XInputStream; 27cdf0e10cSrcweir import java.io.InputStream; 28cdf0e10cSrcweir 29cdf0e10cSrcweir /** 30cdf0e10cSrcweir * The <code>XInputStreamToInputStreamAdapter</code> wraps 31cdf0e10cSrcweir * the UNO <code>XInputStream</code> object in a Java 32cdf0e10cSrcweir * <code>InputStream</code>. This allows users to access 33cdf0e10cSrcweir * an <code>XInputStream</code> as if it were an 34cdf0e10cSrcweir * <code>InputStream</code>. 35cdf0e10cSrcweir * 36cdf0e10cSrcweir * @author Brian Cameron 37cdf0e10cSrcweir */ 38cdf0e10cSrcweir public class XInputStreamToInputStreamAdapter extends InputStream { 39cdf0e10cSrcweir 40cdf0e10cSrcweir /** 41cdf0e10cSrcweir * Internal handle to the XInputStream 42cdf0e10cSrcweir */ 43cdf0e10cSrcweir private XInputStream xin; 44cdf0e10cSrcweir 45cdf0e10cSrcweir /** 46cdf0e10cSrcweir * Constructor. 47cdf0e10cSrcweir * 48cdf0e10cSrcweir * @param in The <code>XInputStream</code> to be 49cdf0e10cSrcweir * accessed as an <code>InputStream</code>. 50cdf0e10cSrcweir */ XInputStreamToInputStreamAdapter(XInputStream in)51cdf0e10cSrcweir public XInputStreamToInputStreamAdapter (XInputStream in) { 52cdf0e10cSrcweir xin = in; 53cdf0e10cSrcweir } 54cdf0e10cSrcweir available()55cdf0e10cSrcweir public int available() throws IOException { 56cdf0e10cSrcweir 57cdf0e10cSrcweir int bytesAvail; 58cdf0e10cSrcweir 59cdf0e10cSrcweir try { 60cdf0e10cSrcweir bytesAvail = xin.available(); 61cdf0e10cSrcweir } catch (Exception e) { 62cdf0e10cSrcweir throw new IOException(e.toString()); 63cdf0e10cSrcweir } 64cdf0e10cSrcweir 65cdf0e10cSrcweir return(bytesAvail); 66cdf0e10cSrcweir } 67cdf0e10cSrcweir close()68cdf0e10cSrcweir public void close() throws IOException { 69cdf0e10cSrcweir try { 70cdf0e10cSrcweir xin.closeInput(); 71cdf0e10cSrcweir } catch (Exception e) { 72cdf0e10cSrcweir throw new IOException(e.toString()); 73cdf0e10cSrcweir } 74cdf0e10cSrcweir } 75cdf0e10cSrcweir read()76cdf0e10cSrcweir public int read () throws IOException { 77cdf0e10cSrcweir byte [][] tmp = new byte [1][1]; 78cdf0e10cSrcweir try { 79cdf0e10cSrcweir long bytesRead = xin.readBytes(tmp, 1); 80cdf0e10cSrcweir 81cdf0e10cSrcweir if (bytesRead <= 0) { 82cdf0e10cSrcweir return (-1); 83cdf0e10cSrcweir } else { 84cdf0e10cSrcweir int tmpInt = tmp[0][0]; 85cdf0e10cSrcweir if (tmpInt< 0 ){ 86cdf0e10cSrcweir tmpInt = 256 +tmpInt; 87cdf0e10cSrcweir } 88cdf0e10cSrcweir return(tmpInt); 89cdf0e10cSrcweir } 90cdf0e10cSrcweir 91cdf0e10cSrcweir } catch (Exception e) { 92cdf0e10cSrcweir throw new IOException(e.toString()); 93cdf0e10cSrcweir } 94cdf0e10cSrcweir } 95cdf0e10cSrcweir read(byte[] b)96cdf0e10cSrcweir public int read (byte[] b) throws IOException { 97cdf0e10cSrcweir 98cdf0e10cSrcweir byte [][] tmp = new byte [1][b.length]; 99cdf0e10cSrcweir int bytesRead; 100cdf0e10cSrcweir 101cdf0e10cSrcweir try { 102cdf0e10cSrcweir bytesRead = xin.readBytes(tmp, b.length); 103cdf0e10cSrcweir if (bytesRead <= 0) { 104cdf0e10cSrcweir return(-1); 105cdf0e10cSrcweir } else if (bytesRead < b.length) { 106cdf0e10cSrcweir System.arraycopy(tmp[0], 0, b, 0, bytesRead); 107cdf0e10cSrcweir } else { 108cdf0e10cSrcweir System.arraycopy(tmp[0], 0, b, 0, b.length); 109cdf0e10cSrcweir } 110cdf0e10cSrcweir } catch (Exception e) { 111cdf0e10cSrcweir throw new IOException(e.toString()); 112cdf0e10cSrcweir } 113cdf0e10cSrcweir 114cdf0e10cSrcweir return (bytesRead); 115cdf0e10cSrcweir } 116cdf0e10cSrcweir read(byte[] b, int off, int len)117cdf0e10cSrcweir public int read(byte[] b, int off, int len) throws IOException { 118cdf0e10cSrcweir int count = 0; 119cdf0e10cSrcweir byte [][] tmp = new byte [1][b.length]; 120cdf0e10cSrcweir try { 121cdf0e10cSrcweir long bytesRead=0; 122cdf0e10cSrcweir int av = xin.available(); 123cdf0e10cSrcweir if ( av != 0 && len > av) { 124cdf0e10cSrcweir bytesRead = xin.readBytes(tmp, av); 125cdf0e10cSrcweir } 126cdf0e10cSrcweir else{ 127cdf0e10cSrcweir bytesRead = xin.readBytes(tmp,len); 128cdf0e10cSrcweir } 129cdf0e10cSrcweir // Casting bytesRead to an int is okay, since the user can 130cdf0e10cSrcweir // only pass in an integer length to read, so the bytesRead 131cdf0e10cSrcweir // must <= len. 132cdf0e10cSrcweir // 133cdf0e10cSrcweir if (bytesRead <= 0) { 134cdf0e10cSrcweir return(-1); 135cdf0e10cSrcweir } else if (bytesRead < len) { 136cdf0e10cSrcweir System.arraycopy(tmp[0], 0, b, off, (int)bytesRead); 137cdf0e10cSrcweir } else { 138cdf0e10cSrcweir System.arraycopy(tmp[0], 0, b, off, len); 139cdf0e10cSrcweir } 140cdf0e10cSrcweir 141cdf0e10cSrcweir return ((int)bytesRead); 142cdf0e10cSrcweir 143cdf0e10cSrcweir 144cdf0e10cSrcweir } catch (Exception e) { 145cdf0e10cSrcweir throw new IOException("reader error: "+e.toString()); 146cdf0e10cSrcweir } 147cdf0e10cSrcweir } 148cdf0e10cSrcweir skip(long n)149cdf0e10cSrcweir public long skip(long n) throws IOException { 150cdf0e10cSrcweir 151cdf0e10cSrcweir int avail; 152cdf0e10cSrcweir long tmpLongVal = n; 153cdf0e10cSrcweir int tmpIntVal; 154cdf0e10cSrcweir 155cdf0e10cSrcweir try { 156cdf0e10cSrcweir avail = xin.available(); 157cdf0e10cSrcweir } catch (Exception e) { 158cdf0e10cSrcweir throw new IOException(e.toString()); 159cdf0e10cSrcweir } 160cdf0e10cSrcweir 161cdf0e10cSrcweir do { 162cdf0e10cSrcweir if (tmpLongVal >= Integer.MAX_VALUE) { 163cdf0e10cSrcweir tmpIntVal = Integer.MAX_VALUE; 164cdf0e10cSrcweir } else { 165cdf0e10cSrcweir // Casting is safe here. 166cdf0e10cSrcweir tmpIntVal = (int)tmpLongVal; 167cdf0e10cSrcweir } 168cdf0e10cSrcweir tmpLongVal -= tmpIntVal; 169cdf0e10cSrcweir 170cdf0e10cSrcweir try { 171cdf0e10cSrcweir xin.skipBytes(tmpIntVal); 172cdf0e10cSrcweir } catch (Exception e) { 173cdf0e10cSrcweir throw new IOException(e.toString()); 174cdf0e10cSrcweir } 175cdf0e10cSrcweir } while (tmpLongVal > 0); 176cdf0e10cSrcweir 177cdf0e10cSrcweir if ( avail != 0 && avail < n) { 178cdf0e10cSrcweir return(avail); 179cdf0e10cSrcweir } else { 180cdf0e10cSrcweir return(n); 181cdf0e10cSrcweir } 182cdf0e10cSrcweir } 183cdf0e10cSrcweir 184cdf0e10cSrcweir /** 185cdf0e10cSrcweir * Tests if this input stream supports the mark and reset methods. 186cdf0e10cSrcweir * The markSupported method of 187cdf0e10cSrcweir * <code>XInputStreamToInputStreamAdapter</code> returns false. 188cdf0e10cSrcweir * 189cdf0e10cSrcweir * @returns false 190cdf0e10cSrcweir */ markSupported()191cdf0e10cSrcweir public boolean markSupported() { 192cdf0e10cSrcweir return false; 193cdf0e10cSrcweir } 194cdf0e10cSrcweir mark(int readlimit)195cdf0e10cSrcweir public void mark(int readlimit) { 196cdf0e10cSrcweir // Not supported. 197cdf0e10cSrcweir } 198cdf0e10cSrcweir reset()199cdf0e10cSrcweir public void reset() throws IOException { 200cdf0e10cSrcweir // Not supported. 201cdf0e10cSrcweir } 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204