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

📄 napletreader.java

📁 移动Agent编程工具Naplet
💻 JAVA
字号:
/*
 * @<#> NapletReader.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.Reader;
import java.io.IOException;

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

/**
 * Piped character-input streams from naplets to priviledged services .
 *
 * @version 0.0.1, 1/1/2001
 * @author C. Xu (czxu@yahoo.com)
 */

public class NapletReader extends Reader 
{
      ServiceWriter source = null;
   
      /**
       * Creates a <code>NapletReader</code> and connects it to a 
       * <code>SerivceWriter</code> in a piped stream. 
       * Data written to <code>src</code> 
       * will then be  available as input from this stream.
       *
       * @param      src   the stream to connect to.
       * @exception  IOException  if an I/O error occurs.
       */
      public NapletReader( ServiceWriter src )
	 throws IOException 
      {
	 if ( src == null )
	 {
	    throw new IOException( "Service writer doesn't exist" );
	 }

	 src.connect( this );
      }

      /**
       * Creates a <code>NapletReader</code>. It must be
       * connected to a <code>ServiceWriter</code>
       * before being used.
       *
       */
      public NapletReader( )
      {
      }
 
      /**
       * Closes this piped stream and releases any system resources 
       * associated with the stream. 
       *
       * @exception  IOException  if an I/O error occurs.
       */
      public void close( )
	 throws IOException 
      {
	 synchronized ( source )
	 {
	    source.in = -1;
	    source.out = 0;
	    source.closedByNaplet = true;
	    source.connected = false;
	 }
      }

    

      /**
       * Reads the next character of data from this piped stream.
       * If no character is available because the end of the stream 
       * has been reached, the value <code>-1</code> is returned. 
       * This method blocks until input data is available, the end of
       * the stream is detected, or an exception is thrown. 
       *
       * If a thread was providing data characters
       * to the connected piped writer, but
       * the  thread is no longer alive, then an
       * <code>IOException</code> is thrown.
       *
       * @return     the next character of data, or <code>-1</code> if the 
       *             end of the stream is reached.
       * @exception  IOException  if the pipe is broken.
       */
      public int read( )
	 throws IOException
      {
	 return source.get();
      }
	

      /**
       * Reads up to <code>len</code> characters of data from this piped
       * stream into an array of characters. Less than <code>len</code> 
       * characters will be read if the end of the data stream is reached. 
       * This method blocks until at least one character of input is 
       * available. If a thread was providing data characters to the connected
       * piped output, but the thread is no longer alive, then an 
       * <code>IOException</code> is thrown.
       *
       * @param      cbuf     the buffer into which the data is read.
       * @param      off   the start offset of the data.
       * @param      len   the maximum number of characters read.
       * @return     the total number of characters read into the buffer, or
       *             <code>-1</code> if there is no more data because the end 
       *             of the stream has been reached.
       * @exception  IOException  if an I/O error occurs.
       */
      public int read( char cbuf[], int off, int len )
	 throws IOException
      {
	 return source.get( cbuf, off, len );
      }

      /**
       * Tell whether this stream is ready to be read.
       *
       * @return true if the next read is guaranteed not to block for input,
       *         false otherwise. Note that returning false does not guarantee
       *         that the next read will block.
       * 
       * @exception IOException if an I/O error occurs
       */
      public boolean ready( )
	 throws IOException
      {
	 synchronized ( source )
	 {
	    if ( source.closedByServer )
	    {
	       throw new IOException( "Pipe closed" );
	    }

	    if ( source.in < 0 )
	    {
	       return false;
	    }

	    int count;
	    if ( source.out < source.in )
	    {
	       count = source.in - source.out;
	    }
	    else
	    {
	       count = source.buffer.length - source.out - source.in;
	    }
	    
	    return ( count > 0 );
	 }
      }
}

⌨️ 快捷键说明

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