code: private int readPacket(byte[] packet, int offset, int psize) throws IOException {
          int length = 0;
          if(position+psize<source.length)
      	{
      		length=psize;
      		System.arraycopy(source, position, packet, offset, length);
      		position+=psize;
      	}
      	else if(position<source.length)
      	{
      		length=source.length-position;
      		System.arraycopy(source, position, packet, offset, length);
      		position+=length;
      	}
          
          return length;
      }
docs: Reads packet from currently opened stream.
  
  @param packet
  the packet to read
  @param offset
  the offset from which new data will be inserted
  @return the number of actualy read bytes.
  @throws java.io.IOException