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

📄 bufferednapletoutputstream.java

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

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

/**
 * Buffered byte-output stream from priviledged services to naplets. 
 * 
 * The class implements a buffered output stream. By setting up such an output
 * stream, the priviledged services can write bytes to the underlying 
 * ServiceWriter without necessarily causing a call to the underlying system 
 * for each byte written. The data is written into an internal buffer, 
 * and then written to the underlying stream if the buffer reaches its 
 * capacity, the buffer output is closed, or the buffer output stream is 
 * explicitly flushed. 
 *
 * @version 0.0.1, 1/1/2001
 * @author C. Xu (czxu@yahoo.com)
 */
public class BufferedNapletOutputStream extends OutputStream 
{
      protected NapletOutputStream outStream;
      protected final int BUFFER_SIZE = 1024;

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

      /**
       * The number of valid bytes in the internal buffer. 
       * 
       * This value is always in the range 0 through buffer.length;
       * elements buffer[0] through buffer[count - 1] contains valid 
       * byte data.
       */
      protected int count = 0;
      
      /**
       * Creates a new buffered output stream to write data to the specified
       * underlying output stream with a default 1024 buffer size.
       *
       * @param out The underlying output stream.
       *
       */
      public BufferedNapletOutputStream( NapletOutputStream  out )
      {
	 outStream = out;
	 buffer = new byte[BUFFER_SIZE];
      }
      /**
       * Creates a new buffered output stream to write data to the specified 
       * underlying output stream with the specified buffer size.
       *
       * @param out The underlying output stream.
       * @param size the buffer size
       *
       * @exception IOException if an I/O error occurs
       * @exception IllegalSrgumentException if size is <= 0
       *
       */
      public BufferedNapletOutputStream( NapletOutputStream out, int size )
      {
	 this.outStream = out;

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

	 buffer = new byte[size];
      }

      /**
       * Pour the data in internal buffer to the underlying output stream if 
       * the buffer is full, this buffered stream is closed, or an explicit 
       * flush is called.
       *
       * @exception IOException if an I/O error occurs
       *
       */
      private void pour( )
	 throws IOException
      {
	 outStream.write( buffer, 0, count ); 
	 count = 0;
      }

      /**
       * Writes specified byte to this buffered output stream.
       *
       * @param c the byte to be written
       *
       * @exception IOException if an I/O error occurs
       *
       */
      public void write( int c )
	 throws IOException
      {
	 synchronized ( this )
	 {
	    // No readable byte is available in the buffer, buffer 
	    // needs to be refilled.
	    if ( count >= buffer.length )
	    {
	       pour( );
	    } 
	    
	    buffer[count++] = ( byte ) c;
	    
	 }
      }

      /**
       * Writes len bytes from the specified character array starting
       * at offset off to this buffered output stream.
       *
       * @param b 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[] 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( );
	 }
	 
	 // Ordinarily, the bytes to write from the given array 
	 // is stored into this stream's buffer, flushing the buffer to the 
	 // underlying output stream as needed. If the requested length is 
	 // at least as large as this stream's buffer, however, then this 
	 // method will flush the buffer and write the bytes directly to the 
	 // underlying output stream. Thus redundant BufferedServiceWriter 
	 // will not copy data unnecessarily.
	 synchronized ( this )
	 {
	    int rlen = buffer.length - count;

	    if ( rlen <= 0 )
	    {
	       pour( );
	       
	       int i = len / buffer.length;
	       int remain = len % buffer.length;
  
	       while ( i > 0 )
	       {
		  outStream.write( b, off, buffer.length );
		  off += buffer.length;
		  i--;
	       }

	       System.arraycopy( b, off, buffer, count, remain );
	    }
	    else
	    {
	       int rrlen = rlen - len;
	       if ( rrlen >= 0 )
	       {
		  System.arraycopy( b, off, buffer, count, len );
	       }
	       else
	       {
		  System.arraycopy( b, off, buffer, count, rrlen );
		  pour( );
		  off += rrlen;

		  int j = ( len + rrlen ) / buffer.length;
		  int re = ( len + rrlen ) % buffer.length;
		  while ( j > 0 )
		  {
		     outStream.write( b, off, buffer.length );
		     off += buffer.length;
		     j--;
		  }
		  
		  System.arraycopy( b, off, buffer, count, re );
	       }
	    }
	 }
      }
   
      /**
       * Close this buffered output 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 )
	 {
	    pour( );
	    outStream.close( );
	 }	
      }
      
      /**
       * Flushes this buffered output stream. This forces any buffered output 
       * bytes to be written out to the underlying output stream.
       *
       * @exception IOException if an I/O error occurs
       *
       */
      public void flush( )
	 throws IOException
      {
	 pour( );
      }
}

⌨️ 快捷键说明

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