1 package com.daffodilwoods.rmi; 2 3 import java.io.Reader ; 4 import com.daffodilwoods.database.resource.*; 5 import java.io.IOException ; 6 import java.rmi.RemoteException ; 7 import com.daffodilwoods.rmi.interfaces.*; 8 9 public class RmiReader 10 extends Reader { 11 12 _RmiReader rmiReaderInterface; 13 private int BUFFER_SIZE = 1000; 14 char[] buf = null; 15 private int lastIndex = 0; 16 17 public void close() throws IOException { 18 try { 19 rmiReaderInterface.close(); 20 } 21 catch (RemoteException re) { 22 } 23 } 24 25 public int read(char b[]) throws IOException { 26 try { 27 if (lastIndex == -1) 28 return -1; 29 if (buf == null) { 30 buf = rmiReaderInterface.getChar(0, BUFFER_SIZE); 31 if (buf == null) { 32 lastIndex = -1; 33 return -1; 34 } 35 } 36 return readBuf(b); 37 } 38 catch (RemoteException re) { 39 return Integer.MIN_VALUE; 40 } 41 } 42 43 private int readBuf(char[] b) throws 44 RemoteException { 45 int len = buf.length - lastIndex; 46 int givenLen = b.length; 47 if (givenLen <= len) { 48 System.arraycopy(buf, lastIndex, b, 0, givenLen); 49 lastIndex += givenLen; 50 return givenLen; 51 } 52 if (buf.length < BUFFER_SIZE) { 53 System.arraycopy(buf, lastIndex, b, 0, len); 54 lastIndex = -1; 55 return len == 0 ? -1 : len; 56 } 57 if(len != 0) 58 System.arraycopy(buf, lastIndex, b, 0, len); 59 60 while (givenLen > len) { 61 buf = rmiReaderInterface.getChar(0, BUFFER_SIZE); 62 if (buf == null) { 63 lastIndex = -1; 64 return len == 0 ? -1 : len; 65 } 66 int len1 = buf.length; 67 int lastPos = len; 68 len += len1; 69 if (givenLen <= len) { 70 System.arraycopy(buf, 0, b, lastPos, givenLen-lastPos); 71 lastIndex = givenLen-lastPos; 72 return givenLen; 73 } 74 if (len1 < BUFFER_SIZE) { 75 System.arraycopy(buf, 0, b, lastPos, len1); 76 lastIndex = -1; 77 return len; 78 } 79 System.arraycopy(buf, 0, b, lastPos, len1); 80 } 81 return len; 82 } 83 84 public int read(char b[], int off, int len) throws IOException { 85 try { 86 char[] returnedBytes = new char[len]; 87 int length = read(returnedBytes); 88 if (length == -1) 89 return -1; 90 System.arraycopy(returnedBytes, 0, b, off, length); 91 return length; 92 } 93 catch (RemoteException re) { 94 return Integer.MIN_VALUE; 95 } 96 } 97 98 public RmiReader(_RmiReader rmiReaderInterface) { 99 this.rmiReaderInterface = rmiReaderInterface; 100 } 101 } 102 | Popular Tags |