⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nsocketoutputstream.java

📁 移动Agent编程工具Naplet
💻 JAVA
字号:
package naplet.nsocket;

import java.io.*;

/**
 * NSocketInputStream is an extension of Java InputStream Socket.It provides
 * standard java inputstream APIs and some additional functions for
 * connection migration.
 *
 * Biggest change is extend write to three versions.
 */
public class NSocketOutputStream
    extends OutputStream
{

  /**
   * Outputstream under this naplet outstream
   */
  private OutputStream oStream = null;

  /**
   * total number of users currently that are using this outstream
   */
  private int iStreamUserCount = 0;

  /**
   * current state of this stream
   */
  private int iStreamState = Global.RESUMED;

  protected NSocketOutputStream( OutputStream outputstream )
  {
    if ( outputstream == null )
    {
      throw new NullPointerException( "oStream" );
    }

    oStream = outputstream;
  }

  public synchronized void close()
      throws IOException
  {
    testState();
    oStream.close();
    iStreamState = Global.CLOSED;
    iStreamUserCount--;
    this.notifyAll();
  }

  public synchronized void flush()
      throws IOException
  {
    testState();
    oStream.flush();
    iStreamUserCount--;
    this.notifyAll();
  }

  /**
   * Resumes output stream in new host.
   * @param outputstream
   */
  protected synchronized void resume( OutputStream outputstream )
  {
    log( "called in resume ostream" );
    if ( iStreamState != Global.RESUMED && iStreamState != Global.CLOSED )
    {
      oStream = outputstream;
      iStreamState = Global.RESUMED;
      this.notifyAll();
    }
  }

  /**
   * Suspends output stream in old host.
   */
  protected synchronized void suspend()
  {
    if ( iStreamState != Global.SUSPENDED && iStreamState != Global.CLOSED )
    {
      iStreamState = Global.SUSPENDED;
      while ( iStreamUserCount > 0 )
      {
        try
        {
          this.wait();
        }
        catch ( Exception exception )
        {
          exception.printStackTrace();
        }
      }

      log( "suspend in outputstream" );
      try
      {
        oStream.close();
      }
      catch ( Exception exception )
      {
        /* empty */
      }
      oStream = null;
    }
  }

  /**
   * Be sure no one else is using this input stream.
   * Wait if someone is using it.
   */
  private synchronized void testState()
  {
    while ( iStreamState == Global.SUSPENDED )
    {
      try
      {
        this.wait();
      }
      catch ( Exception exception )
      {
        exception.printStackTrace();
      }
    }
    iStreamUserCount++;
  }

  /**
   * Writes the specified byte to this output stream. The general
   * contract for <code>write</code> is that one byte is written
   * to the output stream. The byte to be written is the eight
   * low-order bits of the argument <code>b</code>. The 24
   * high-order bits of <code>b</code> are ignored.
   * <p>
   * Subclasses of <code>OutputStream</code> must provide an
   * implementation for this method.
   *
   * @param      b   the <code>byte</code>.
   * @exception  IOException  if an I/O error occurs. In particular,
   *             an <code>IOException</code> may be thrown if the
   *             output stream has been closed.
   */
  public synchronized void write( int b )
      throws IOException
  {
    write( new byte[]
           { ( byte ) b}
           , 0, 1 );
  }

  /**
   * Writes <code>b.length</code> bytes from the specified byte array
   * to this output stream. The general contract for <code>write(b)</code>
   * is that it should have exactly the same effect as the call
   * <code>write(b, 0, b.length)</code>.
   *
   * @param      b   the data.
   * @exception  IOException if the reliable socket has been closed
   * @see        java.io.OutputStream#write(byte[], int, int)
   */
  public synchronized void write( byte b[] )
      throws IOException
  {
    write( b, 0, b.length );
  }

  /**
   * Writes <code>len</code> bytes from the specified byte array
   * starting at offset <code>off</code> to this output stream.
   * The general contract for <code>write(b, off, len)</code> is that
   * some of the bytes in the array <code>b</code> are written to the
   * output stream in order; element <code>b[off]</code> is the first
   * byte written and <code>b[off+len-1]</code> is the last byte written
   * by this operation.
   * <p>
   * The <code>write</code> method of <code>OutputStream</code> calls
   * the write method of one argument on each of the bytes to be
   * written out. Subclasses are encouraged to override this method and
   * provide a more efficient implementation.
   * <p>
   * If <code>b</code> is <code>null</code>, a
   * <code>NullPointerException</code> is thrown.
   * <p>
   * If <code>off</code> is negative, or <code>len</code> is negative, or
   * <code>off+len</code> is greater than the length of the array
   * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
   *
   * @param      b     the data.
   * @param      off   the start offset in the data.
   * @param      len   the number of bytes to write.
   * @exception  IOException if the reliable socket has been closed
   */
  public synchronized void write( byte b[], int off, int len )
      throws IOException
  {
    testState();
    try
    {
      oStream.write( b, off, len );
    }
    catch ( IOException ioexception )
    {
      iStreamState = Global.SUSPENDED;
      iStreamUserCount--;
      ioexception.printStackTrace();
      throw ioexception;
    }
    iStreamUserCount--;
    this.notifyAll();
  }

  private void log( String info )
  {
    if ( SocketController.debug )
    {
      System.out.println( "NapletOutputStream:" + info );
    }
  }

} // end of NSocketOutputStream

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -