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 24 package com.sun.star.script.framework.io; 25 26 import com.sun.star.io.XInputStream; 27 import java.io.InputStream; 28 import java.io.IOException; 29 30 public class XInputStreamImpl implements XInputStream 31 { 32 InputStream is; XInputStreamImpl( InputStream is )33 public XInputStreamImpl( InputStream is ) 34 { 35 this.is = is; 36 } 37 readBytes( byte[][] aData, int nBytesToRead )38 public int readBytes( /*OUT*/byte[][] aData, /*IN*/int nBytesToRead ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException 39 { 40 aData[ 0 ] = new byte[ nBytesToRead ]; 41 42 int totalBytesRead = 0; 43 44 try 45 { 46 int bytesRead = 0; 47 while ( ( nBytesToRead > 0 ) && ( bytesRead = is.read( aData[ 0 ], totalBytesRead, nBytesToRead ) ) > 0 ) 48 { 49 totalBytesRead += bytesRead; 50 nBytesToRead -= bytesRead; 51 } 52 if ( totalBytesRead < aData[ 0 ].length ) 53 { 54 byte[] out = new byte[ totalBytesRead ]; 55 System.arraycopy( aData[ 0 ], 0, out, 0, totalBytesRead ); 56 aData[ 0 ] = out; 57 } 58 } 59 catch ( IOException e ) 60 { 61 throw new com.sun.star.io.IOException( e.toString() ); 62 } 63 catch ( IndexOutOfBoundsException aie ) 64 { 65 throw new com.sun.star.io.BufferSizeExceededException( aie.toString() ); 66 } 67 return totalBytesRead; 68 } 69 readSomeBytes( byte[][] aData, int nMaxBytesToRead )70 public int readSomeBytes( /*OUT*/byte[][] aData, /*IN*/int nMaxBytesToRead ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException 71 { 72 int bytesToRead = nMaxBytesToRead; 73 int availableBytes = available(); 74 if ( 0 < availableBytes && availableBytes < nMaxBytesToRead ) 75 { 76 bytesToRead = availableBytes; 77 } 78 int read = readBytes( aData, bytesToRead ); 79 return read; 80 } 81 skipBytes( int nBytesToSkip )82 public void skipBytes( /*IN*/int nBytesToSkip ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.BufferSizeExceededException, com.sun.star.io.IOException 83 { 84 long bytesSkipped = 0; 85 try 86 { 87 bytesSkipped = is.skip( (long)nBytesToSkip ); 88 } 89 catch ( IOException e ) 90 { 91 throw new com.sun.star.io.IOException( e.toString() ); 92 } 93 } 94 available( )95 public int available( ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException 96 { 97 int bytesAvail = 0; 98 try 99 { 100 bytesAvail = is.available(); 101 } 102 catch ( IOException e ) 103 { 104 throw new com.sun.star.io.IOException( e.toString() ); 105 } 106 return bytesAvail; 107 } 108 closeInput( )109 public void closeInput( ) throws com.sun.star.io.NotConnectedException, com.sun.star.io.IOException 110 { 111 try 112 { 113 is.close(); 114 } 115 catch( IOException e ) 116 { 117 throw new com.sun.star.io.IOException( e.toString() ); 118 } 119 } 120 121 } 122