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