/**
  * Constructor for the OutputStreamStealer object
  *
  * @param url
  * @param dest
  * @param agent
  */
 public OutputStreamStealer(String url, OutputStream dest, StreamAgent agent, long groupid) {
   this.dest = dest;
   netAgent = agent;
   this.URL = url;
   this.groupid = groupid;
   md = netAgent.newStream(URL, HttpAgent.CLIENT2SERVER, groupid);
 }
  /**
   * This method writes the wtk header which is not the HTTP header but some data that is needed to
   * be passed to the j2se side. Thats for each message.
   */
  public void writeNewMsgWTKHeader() throws IOException {

    long time = System.currentTimeMillis();
    String str = String.valueOf(time) + "\r\n";
    byte[] buff = str.getBytes();
    netAgent.writeBuff(md, buff, 0, buff.length);
  }
  /**
   * The name says it all.
   *
   * @param b
   * @param off
   * @param len
   * @exception IOException
   */
  public void write(byte[] b, int off, int len) throws IOException {
    dest.write(b, off, len);

    if (md >= 0) {

      if (newMsgFlag) {
        newMsgFlag = false;
        writeNewMsgWTKHeader();
      }

      netAgent.writeBuff(md, b, off, len);
    }
  }
  /**
   * The name says it all.
   *
   * @param b
   * @exception IOException
   */
  public void write(int b) throws IOException {

    dest.write(b);
    if (md >= 0) {

      if (newMsgFlag) {
        newMsgFlag = false;
        writeNewMsgWTKHeader();
      }

      netAgent.write(md, b);
    }
  }
 /**
  * Resets the stream stealer for a new usage of the connection
  *
  * @param url New url
  * @param groupid New group id
  */
 public void reset(String url, long groupid) {
   this.URL = url;
   this.groupid = groupid;
   md = netAgent.newStream(URL, HttpAgent.CLIENT2SERVER, groupid);
 }
 /** Report disconnected stream. Do not close the underlying stream, as it may be reused. */
 public void disconnect() {
   if (md >= 0) {
     netAgent.close(md);
   }
 }