1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package com.sun.star.lib.uno.adapter; 28 29 import java.io.IOException; 30 import com.sun.star.io.XInputStream; 31 import java.io.InputStream; 32 33 /** 34 * The <code>XInputStreamToInputStreamAdapter</code> wraps 35 * the UNO <code>XInputStream</code> object in a Java 36 * <code>InputStream</code>. This allows users to access 37 * an <code>XInputStream</code> as if it were an 38 * <code>InputStream</code>. 39 * 40 * @author Brian Cameron 41 */ 42 public class XInputStreamToInputStreamAdapter extends InputStream { 43 44 /** 45 * Internal handle to the XInputStream 46 */ 47 private XInputStream xin; 48 49 /** 50 * Constructor. 51 * 52 * @param in The <code>XInputStream</code> to be 53 * accessed as an <code>InputStream</code>. 54 */ 55 public XInputStreamToInputStreamAdapter (XInputStream in) { 56 xin = in; 57 } 58 59 public int available() throws IOException { 60 61 int bytesAvail; 62 63 try { 64 bytesAvail = xin.available(); 65 } catch (Exception e) { 66 throw new IOException(e.toString()); 67 } 68 69 return(bytesAvail); 70 } 71 72 public void close() throws IOException { 73 try { 74 xin.closeInput(); 75 } catch (Exception e) { 76 throw new IOException(e.toString()); 77 } 78 } 79 80 public int read () throws IOException { 81 byte [][] tmp = new byte [1][1]; 82 try { 83 long bytesRead = xin.readBytes(tmp, 1); 84 85 if (bytesRead <= 0) { 86 return (-1); 87 } else { 88 int tmpInt = tmp[0][0]; 89 if (tmpInt< 0 ){ 90 tmpInt = 256 +tmpInt; 91 } 92 return(tmpInt); 93 } 94 95 } catch (Exception e) { 96 throw new IOException(e.toString()); 97 } 98 } 99 100 public int read (byte[] b) throws IOException { 101 102 byte [][] tmp = new byte [1][b.length]; 103 int bytesRead; 104 105 try { 106 bytesRead = xin.readBytes(tmp, b.length); 107 if (bytesRead <= 0) { 108 return(-1); 109 } else if (bytesRead < b.length) { 110 System.arraycopy(tmp[0], 0, b, 0, bytesRead); 111 } else { 112 System.arraycopy(tmp[0], 0, b, 0, b.length); 113 } 114 } catch (Exception e) { 115 throw new IOException(e.toString()); 116 } 117 118 return (bytesRead); 119 } 120 121 public int read(byte[] b, int off, int len) throws IOException { 122 int count = 0; 123 byte [][] tmp = new byte [1][b.length]; 124 try { 125 long bytesRead=0; 126 int av = xin.available(); 127 if ( av != 0 && len > av) { 128 bytesRead = xin.readBytes(tmp, av); 129 } 130 else{ 131 bytesRead = xin.readBytes(tmp,len); 132 } 133 // Casting bytesRead to an int is okay, since the user can 134 // only pass in an integer length to read, so the bytesRead 135 // must <= len. 136 // 137 if (bytesRead <= 0) { 138 return(-1); 139 } else if (bytesRead < len) { 140 System.arraycopy(tmp[0], 0, b, off, (int)bytesRead); 141 } else { 142 System.arraycopy(tmp[0], 0, b, off, len); 143 } 144 145 return ((int)bytesRead); 146 147 148 } catch (Exception e) { 149 throw new IOException("reader error: "+e.toString()); 150 } 151 } 152 153 public long skip(long n) throws IOException { 154 155 int avail; 156 long tmpLongVal = n; 157 int tmpIntVal; 158 159 try { 160 avail = xin.available(); 161 } catch (Exception e) { 162 throw new IOException(e.toString()); 163 } 164 165 do { 166 if (tmpLongVal >= Integer.MAX_VALUE) { 167 tmpIntVal = Integer.MAX_VALUE; 168 } else { 169 // Casting is safe here. 170 tmpIntVal = (int)tmpLongVal; 171 } 172 tmpLongVal -= tmpIntVal; 173 174 try { 175 xin.skipBytes(tmpIntVal); 176 } catch (Exception e) { 177 throw new IOException(e.toString()); 178 } 179 } while (tmpLongVal > 0); 180 181 if ( avail != 0 && avail < n) { 182 return(avail); 183 } else { 184 return(n); 185 } 186 } 187 188 /** 189 * Tests if this input stream supports the mark and reset methods. 190 * The markSupported method of 191 * <code>XInputStreamToInputStreamAdapter</code> returns false. 192 * 193 * @returns false 194 */ 195 public boolean markSupported() { 196 return false; 197 } 198 199 public void mark(int readlimit) { 200 // Not supported. 201 } 202 203 public void reset() throws IOException { 204 // Not supported. 205 } 206 } 207 208