1 package com.daffodilwoods.rmi; 2 import java.io.*; 3 import com.daffodilwoods.database.resource.*; 4 import com.daffodilwoods.rmi.interfaces._RmiInputStream; 5 import com.daffodilwoods.rmi.server.RmiInputStreamServerSide; 6 import java.rmi.RemoteException ; 7 public class RmiInputStream extends InputStream { 8 _RmiInputStream inputStream; 9 private int BUFFER_SIZE = 1000; 10 byte[] buf = null; 11 private int lastIndex = 0; 12 13 public RmiInputStream(_RmiInputStream inputStream) { 14 this.inputStream = inputStream; 15 } 16 17 public int read(byte b[]) throws IOException { 18 try { 19 if (lastIndex == -1) 20 return -1; 21 if (buf == null) { 22 buf = inputStream.getBytes(0, BUFFER_SIZE); 23 if (buf == null) { 24 lastIndex = -1; 25 return -1; 26 } 27 } 28 return readBuf(b); 29 } 30 catch (RemoteException re) { 31 return Integer.MIN_VALUE; 32 } 33 } 34 35 private int readBuf(byte[] b) throws 36 RemoteException { 37 int len = buf.length - lastIndex; 38 int givenLen = b.length; 39 if (givenLen <= len) { 40 System.arraycopy(buf, lastIndex, b, 0, givenLen); 41 lastIndex += givenLen; 42 return givenLen; 43 } 44 if (buf.length < BUFFER_SIZE) { 45 System.arraycopy(buf, lastIndex, b, 0, len); 46 lastIndex = -1; 47 return len == 0 ? -1 : len; 48 } 49 if(len != 0) 50 System.arraycopy(buf, lastIndex, b, 0, len); 51 52 while (givenLen > len) { 53 buf = inputStream.getBytes(0, BUFFER_SIZE); 54 if (buf == null) { 55 lastIndex = -1; 56 return len == 0 ? -1 : len; 57 } 58 int len1 = buf.length; 59 int lastPos = len; 60 len += len1; 61 if (givenLen <= len) { 62 System.arraycopy(buf, 0, b, lastPos, givenLen-lastPos); 63 lastIndex = givenLen-lastPos; 64 return givenLen; 65 } 66 if (len1 < BUFFER_SIZE) { 67 System.arraycopy(buf, 0, b, lastPos, len1); 68 lastIndex = -1; 69 return len; 70 } 71 System.arraycopy(buf, 0, b, lastPos, len1); 72 } 73 return len; 74 } 75 76 public int read() throws IOException { 77 return read(new byte[]{0}); 78 } 79 80 81 public int read(byte b[], int off, int len) throws IOException { 82 try { 83 byte[] returnedBytes = new byte[len]; 84 int length = read(returnedBytes); 85 if (length == -1) 86 return -1; 87 System.arraycopy(returnedBytes, 0, b, off, length); 88 return length; 89 } 90 catch (RemoteException re) { 91 return Integer.MIN_VALUE; 92 } 93 } 94 95 public long skip(long n) throws IOException { 96 try { 97 return inputStream.skip(n); 98 }catch(RemoteException re) { 99 return Long.MIN_VALUE; 100 } 101 102 } 103 104 public int available() throws IOException { 105 try { 106 return inputStream.available(); 107 }catch(RemoteException re) { 108 return Integer.MIN_VALUE; 109 } 110 } 111 112 public void close() throws IOException { 113 try { 114 inputStream.close(); 115 }catch(RemoteException re) { 116 } 117 } 118 119 public void mark(int readlimit){ 120 try { 121 inputStream.mark(readlimit); 122 }catch(RemoteException re) { 123 } 124 } 125 126 public void reset() throws IOException { 127 try { 128 inputStream.reset(); 129 }catch(RemoteException re) { 130 } 131 } 132 133 public boolean markSupported() { 134 try { 135 return inputStream.markSupported(); 136 }catch(RemoteException re) { 137 return false; 138 } 139 } 140 } 141 | Popular Tags |