public synchronized int read(byte b[],int off,int len) throws IOException {
  if (len <= 0)   return 0;
  int moreSpace;
synchronized (lock) {
    if (pos >= present)     pos=present=0;
 else     if (pos >= waterMark) {
      System.arraycopy(buffer,pos,buffer,0,present - pos);
      present-=pos;
      pos=0;
    }
    int freeSpace=buffer.length - present;
    moreSpace=Math.max(freeSpace - requested,0);
  }
  if (moreSpace > 0)   manager.sendRequest(info,moreSpace);
synchronized (lock) {
    requested+=moreSpace;
    while ((pos >= present) && !disconnected) {
      try {
        lock.wait();
      }
 catch (      InterruptedException e) {
      }
    }
    if (disconnected && pos >= present)     return -1;
    int available=present - pos;
    if (len < available) {
      System.arraycopy(buffer,pos,b,off,len);
      pos+=len;
      return len;
    }
 else {
      System.arraycopy(buffer,pos,b,off,available);
      pos=present=0;
      return available;
    }
  }
}

public synchronized int read ( byte b [ ] , int off , int len ) throws IOException { if ( len <= 0 ) return 0 ; int moreSpace ; synchronized ( lock ) { if ( pos >= present ) pos = present = 0 ; else if ( pos >= waterMark ) { System . arraycopy ( buffer , pos , buffer , 0 , present - pos ) ; present -= pos ; pos = 0 ; } int freeSpace = buffer . length - present ; moreSpace = Math . max ( freeSpace - requested , 0 ) ; } if ( moreSpace > 0 ) manager . sendRequest ( info , moreSpace ) ; synchronized ( lock ) { requested += moreSpace ; while ( ( pos >= present ) && ! disconnected ) { try { lock . wait ( ) ; } catch ( InterruptedException e ) { } } if ( disconnected && pos >= present ) return - 1 ; int available = present - pos ; if ( len < available ) { System . arraycopy ( buffer , pos , b , off , len ) ; pos += len ; return len ; } else { System . arraycopy ( buffer , pos , b , off , available ) ; pos = present = 0 ; return available ; } } }
read a subarray of bytes from connection . this method blocks for at least one byte , and it returns the number of bytes actually read , or - 1 if the end of the stream was detected .
