📄 bufferedservicewriter.java
字号:
/*
* @<#> BufferedServiceWriter.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.Writer;
import java.io.IOException;
import java.lang.*;
import naplet.*;
import naplet.serviceChannel.*;
/**
* Buffered character-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 characters to the underlying
* ServiceWriter without necessarily causing a call to the underlying system
* for each character 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 BufferedServiceWriter extends Writer
{
protected ServiceWriter out;
protected final int BUFFER_SIZE = 1024;
/**
* The internal local buffer array where the data is stored.
*
*/
protected char[] buffer;
/**
* The number of valid characters in the internal buffer.
*
* This value is always in the range 0 through buffer.length;
* elements buffer[0] through buffer[count - 1] contains valid
* character 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 BufferedServiceWriter( ServiceWriter out )
{
this.out = out;
buffer = new char[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 BufferedServiceWriter( ServiceWriter out, int size )
{
this.out = out;
if ( size <= 0 )
{
try
{
out.close( );
}
catch ( IOException e )
{
System.err.println( e.getMessage( ) );
}
catch ( IllegalArgumentException iae )
{
System.err.println( iae.getMessage( ) );
}
}
buffer = new char[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 synchronized void pour( )
throws IOException
{
out.write( buffer, 0, count );
count = 0;
}
/**
* Writes specified character to this buffered output stream.
*
* @param c the character 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++] = ( char ) c;
}
}
/**
* Writes len characters 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 characters to write
*
* @exception IOException if an I/O error occurs
*
*/
public void write( char[] 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 characters 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 )
{
out.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 )
{
out.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( );
out.close( );
}
}
/**
* Flushes this buffered output stream. This forces any buffered output
* characters 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 + -