code: private int readPacket(byte[] packet, int offset, int psize)
              throws IOException {
          int length = 0;
          try {
              while (length < psize) {
                  int len = stream.read(packet, offset + length, psize - length);
                  if (len == -1) {
                      return length;
                  }
                  length += len;
              }
              return length;
          } catch (Exception e) {
          	logger.error(e);
          }
          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