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

📄 bufferednapletinputstream.java

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

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

/**
 * Buffered byte-input stream from naplets to priviledged services. 
 * 
 * The class implements a buffered input stream. By setting up such an input
 * stream, the naplets can read bytes from the underlying NapletInputStream
 * without necessarily causing a call to the underlying system 
 * for each byte read. The data is read from an internal buffer, 
 * and then the buffer got filled from the underlying stream if there are no
 * valid bytes in the buffer.  
 *
 * @version 0.0.1, 1/1/2001
 * @author C. Xu (czxu@yahoo.com)
 */
public class BufferedNapletInputStream extends InputStream 
{
      protected NapletInputStream in;
      protected final int BUFFER_SIZE = 1024;

      /**
       * The internal buffer array where the data is stored. 
       *
       */
      protected byte[] buffer;

      /**
       * The index one greater than the index of the last valid byte in 
       * the buffer. This is always in the range 0 through buf.length; 
       * elements buf[0] through buf[count - 1] contain buffered input 
       * data obtained from the underlying input stream. 
       *
       */
      protected int count = 0;

      /**
       * The current position in the buffer. This is the index of the next 
       * byte to be read from the buffer array. Its value is always in 
       * the range 0 through count. If it is less than count, then buf[pos] 
       * is the next character to be supplied as input; if it is equal to 
       * count, then the next read operation will require more bytes to be 
       * read from the contained input stream
       *
       */
      protected int pos = 0;
      
      /**
       * Creates a <code>BufferedServiceInputStream</code> that uses a 
       * default-sized input buffer.
       * 
       * @param in An InputStream     
       *
       */
      public BufferedNapletInputStream( NapletInputStream in )
      {
	 this.in = in;
	 buffer = new byte[BUFFER_SIZE];
      }

      /**
       * Creates a <code>BufferedServiceInputStream</code> that uses an 
       * input buffer of the specified size.
       * 
       * @param in An InputStream     
       * @param size Input-buffer size
       * 
       * @exception IOException if an I/O error occurs.
       * @exception IllegalArgumentException if size is <= 0
       */
      public BufferedNapletInputStream( NapletInputStream in, int size )
      {
	 this.in = in;

	 if ( size <= 0 )
	 {
	    try
	    {
	       in.close( );
	    }
	    catch ( IOException e )
	    {
	       System.err.println( e.getMessage( ) );
	    }
	    
	    throw new IllegalArgumentException( ); 
	 }

	 buffer = new byte[size];
      }

      /**
       * Fill the internal buffer by reading bytes from the underlying 
       * input stream. 
       *
       * @return the number of bytes read, or -1 if the end of the 
       *         stream has been reached.
       *
       * @exception IOException if an I/O error occurs.
       */
      private int fill( )
	 throws IOException
      {
	 int rlen 
	    = in.read( buffer, count, buffer.length - count ); 

	 if ( rlen > 0 )
	 {
	    count += rlen;
	 }
	 
	 return rlen;
      }
      
      /**
       * Read a single byte. This method will block until a byte is 
       * available, an I/O error occurs, or the end of the stream is reached.
       *
       * @return the byte read, as an integer in the range 0 through 
       *         255, or -1 if the end of the stream has been reached.
       * @exception IOException if an I/O error occurs.
       */
      public int read( )
	 throws IOException
      {
	 int ret;

	 synchronized ( this )
	 {
	    // No readable byte is available in the buffer, buffer 
	    // needs to be refilled.
	    if ( pos >= count )
	    {
	       if ( count >= buffer.length )
	       {
		  pos = 0;
		  count = 0;
	       }
	      
	       if ( fill( ) < 0 )
	       {
		  return -1;
	       }
	    } 
	    
	    ret = ( int ) buffer[pos++];
	 }

	 return ret;
      }

      /**
       * Read bytes into an array. This method will block until a 
       * byte is available, an I/O error occurs, or the end of the 
       * stream is reached.
       *
       * @param b Destination buffer
       * @param off Offset at which to start storing bytes
       * @param len Maximum number of bytes to read
       * 
       * @return the number of bytes read, or -1 if the end of the 
       *         stream has been reached.
       * @exception IOException if an I/O error occurs.
       */
      public int read( byte[] b, int off, int len )
	 throws IOException
      {
	 if ( ( off < 0 ) || ( off >= b.length ) || ( len < 0 )
	      || ( ( off + len ) > b.length ) || ( ( off + len ) < 0 ) )
	 {
	    throw new IndexOutOfBoundsException( "Index out of boundary" );
	 }
	 else if ( len == 0 )
	 {
	    return 0;
	 }
	 
	 int rlen; 
	 
	 synchronized ( this )
	 {
	    // If no readable byte is available in the buffer, read bytes 
	    // directly from the underlying input stream, meantime fill 
	    // the buffer. Otherwise, read the bytes from the buffer and 
	    // refill the buffer.
	    rlen = count - pos;
	    
	    if ( rlen >= len ) 
	    {
	       rlen = len;
	       System.arraycopy( buffer, pos, b, off, len );
	       pos += len;
	    }
	    else 
	    {
	       if ( rlen > 0 ) 
	       {
		  System.arraycopy( buffer, pos, b, off, rlen );
		  pos = count;
		  rlen += in.read( b, off + rlen, len - rlen );
	       }
	       else
	       {
		  rlen = in.read( b, off, len );
	       }

	       if ( count >= buffer.length )
	       {
		  count = 0;
		  pos = 0;
	       }
	    }
	  
	    fill( );	  
	 }
	 
	 return rlen;
      }
   
      /**
       * Close this buffered input stream and releases any system resources 
       * associated with the stream. 
       *
       * @exception  IOException  if an I/O error occurs.
       */
      public void close( )  
	 throws IOException 
      {
	 synchronized ( this )
	 {
	    pos = 0;
	    count = 0;
	    in.close( );
	 }	
      }
}

⌨️ 快捷键说明

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