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