📄 napletwriter.java
字号:
/*
* @<#> NapletWriter.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.Writer;
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 NapletWriter extends Writer
{
ServiceReader sink = null;
/**
* Create a <code>NapletWriter</code> to be connected with
* <code>ServiceReader</code>.
*
*/
public NapletWriter( ServiceReader snk )
throws IOException
{
snk.connect( this );
}
/**
* Create a <code>NapletWriter</code>
*
*/
public NapletWriter( )
{
}
/**
* Writes the specified <code>char</code> to the piped output stream.
* If a thread was reading data characters 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( "NapletWriter: Pipe not connected" );
}
sink.receive( c );
}
/**
* Writes <code>len</code> characters from the specified character array
* starting at offset <code>off</code> to this piped output stream.
* If a thread was reading data characters 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 characters to write.
* @exception IOException if an I/O error occurs.
*/
public void write( char cbuf[], int off, int len )
throws IOException
{
if ( sink == null )
{
throw new IOException( "NapletWriter: Pipe not connected" );
}
else if ( ( off < 0 ) || ( off >= cbuf.length )
|| ( len < 0 ) || ( ( off + len ) > cbuf.length )
|| ( ( off + len ) < 0 ) )
{
throw new IndexOutOfBoundsException(
"Index out of boundary.");
}
sink.receive( cbuf, off, len );
}
/**
* Flushes this output stream and forces any buffered output characters
* to be written out.
* This will notify any readers that characters 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 ( lock )
{
lock.notifyAll( );
}
}
/**
* Closes this piped output stream and releases any system resources
* associated with this stream. This stream cannot be used for
* writing characters.
*
* @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 + -