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

📄 napletoutputstream.java

📁 移动Agent编程工具Naplet
💻 JAVA
字号:
/*
 * @<#> NapletOutputStream.java version 0.0.1 1/1/2001
 *
 * THIS PROGRAM IS FREE SOFTWARE; YOU CAN DISTRIBUTE IT AND/OR
 * MODIFY IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE 
 * AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.
 *
 * THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
 * BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE 
 * GNU GENERAL PUBLIC LICENSE FOR MORE DETAILS.
 *
 * Copyright (c) 2000 Wayne State University. All Rights Reserved.
 */
package naplet.serviceChannel;

import java.io.IOException;
import java.io.OutputStream;

import naplet.*;
import naplet.serviceChannel.*;

/**
 * A output piped stream from naplets to priviledged services.
 *
 * @version 0.0.1, 1/1/2001
 * @author C. Xu (czxu@yahoo.com)
 */
public class NapletOutputStream extends OutputStream 
{
      ServiceInputStream sink = null;

      /**
       * Create a <code>NapletOutputStream</code> to be connected with 
       * <code>ServiceInputStream</code>. 
       *
       */
      public NapletOutputStream( ServiceInputStream snk )  
	 throws IOException 
      {
	 snk.connect( this );
      }
      
      /**
       * Create a <code>NapletWriter</code> 
       *
       */
      public NapletOutputStream( ) 
      {
      }
    
      /**
       * Writes the specified <code>byte</code> to the piped output stream. 
       * If a thread was reading data bytes from the connected piped 
       * input stream, but the thread is no longer alive, then an 
       * <code>IOException</code> is thrown.
       * <p>
       * Implements the <code>write</code> method of <code>Writer</code>.
       *
       * @param      c   the <code>char</code> to be written.
       * @exception  IOException  if an I/O error occurs.
       */
      public void write( int c )
	 throws IOException 
      {
	 if ( sink == null )
	 {
            throw new IOException( "Pipe not connected" );
	 }

	 sink.receive( c );
      }

      /**
       * Writes <code>len</code> bytes from the specified byte array
       * starting at offset <code>off</code> to this piped output stream. 
       * If a thread was reading data bytes from the connected piped 
       * input stream, but the thread is no longer alive, then an 
       * <code>IOException</code> is thrown.
       *
       * @param      cbuf  the data.
       * @param      off   the start offset in the data.
       * @param      len   the number of bytes to write.
       * @exception  IOException  if an I/O error occurs.
       */
      public void write( byte cbuf[], int off, int len )
	 throws IOException 
      {
	 if ( sink == null )
	 {
            throw new IOException( "Pipe not connected" );
	 } 
	 else if ( ( off < 0 ) || ( off >= cbuf.length ) 
		   || ( len < 0 ) || ( ( off + len ) > cbuf.length ) 
		   || ( ( off + len ) < 0 ) )
	 {
	    throw new IndexOutOfBoundsException( );
	 }
	 
	 sink.receive( cbuf, off, len );
      }

      /**
       * Flushes this output stream and forces any buffered output bytes 
       * to be written out. 
       * This will notify any readers that bytes are waiting in the pipe.
       *
       * @exception IOException if an I/O error occurs.
       */
      public  void flush( ) 
	 throws IOException
      {
	 if ( sink == null )
	 {
            throw new IOException( "Pipe is not connected" );
	 }
	 
	 synchronized ( this )
	 {
	    notifyAll( );
	 }
      }

      /**
       * Closes this piped output stream and releases any system resources 
       * associated with this stream. This stream cannot be used for 
       * writing bytes.
       *
       * @exception  IOException  if an I/O error occurs.
       */
      public void close( )
	 throws IOException
      {
	 if ( sink != null )
	 {
	    synchronized ( sink )
	    {
	       while ( sink.in >= 0 )
	       {
		  try 
		  {
		     sink.wait( );
		  } 
		  catch ( InterruptedException ie ) 
		  {
		     System.err.println( "Error: " + ie.getMessage( ) ); 
		  }
	       } 

	       
	       sink.closedByNaplet = true;
	       sink.notifyAll( );

	       // disconnect the naplet from service
	       sink.connected = false;
	    }
	 }
      }
}

⌨️ 快捷键说明

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