一个小段关于FilterOutputStream类的程序,不明白其原理。请高手指教
write()和flush()看不懂,这样做的原理是什么?请高手指教
比如为什么要super.write(toBase64[(_buffer[0]&0xfc)>>2]);
===========================================
class Base64OutputStream
extends FilterOutputStream {
/**
* The constructor.
*
* @param out The stream used to write to.
*/
public Base64OutputStream(OutputStream out)
{
super(out);
}
/**
* Write a byte to be encoded.
*
* @param c The character to be written.
* @exception java.io.IOException
*/
public void write(int c) throws IOException
{
_buffer[_index] = c;
_index++;
if ( _index==3 ) {
super.write(toBase64[(_buffer[0]&0xfc)>>2]);
super.write(toBase64[((_buffer[0] &0x03)<<4) |
((_buffer[1]&0xf0)>>4)]);
super.write(toBase64[((_buffer[1]&0x0f)<<2) |
((_buffer[2]&0xc0)>>6)]);
super.write(toBase64[_buffer[2]&0x3f]);
_column+=4;
_index=0;
if ( _column>=76 ) {
super.write('\n');
_column = 0;
}
}
}
/**
* Ensure all bytes are written.
*
* @exception java.io.IOException
*/
public void flush()
throws IOException
{
if ( _index==1 ) {
super.write(toBase64[(_buffer[2]&0x3f) >> 2]);
super.write(toBase64[(_buffer[0]&0x03) << 4]);
super.write('=');
super.write('=');
} else if ( _index==2 ) {
super.write(toBase64[(_buffer[0]&0xfc) >> 2]);
super.write(toBase64[((_buffer[0]&0x03)<<4)|
((_buffer[1]&0xf0)>>4)]);
super.write(toBase64[(_buffer[1]&0x0f)<<2]);
super.write('=');
}
}
/**
* Allowable characters for base-64.
*/
private static char[] toBase64 =
{ 'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3',
'4','5','6','7','8','9','+','/'};
/**
* Current column.
*/
private int _column=0;
/**
* Current index.
*/
private int _index=0;
/**
* Outbound buffer.
*/
private int _buffer[] = new int[3];
}
新手没多少分了,对不住阿
问题点数:30、回复次数:2Top
1 楼zrtl(刚从软件园回来)回复于 2005-03-02 15:55:28 得分 30
write()方法是把数据写到流里去,这个很容易懂的吧。
flush()方法是把流里的数据清掉,并把这些数据保存到文件中。如果你只是用write来写数据,在程序运行中,文件的内容是不会更新的,用flush后就把文件保存了最新的内容。
同时注意,如果用了close()把流关闭,那么等于自动使用了一次flush()Top
2 楼HughesCN(Hughes)回复于 2005-03-02 20:07:42 得分 0
单个语句我明白,我不明白的是
为什么要
super.write(toBase64[(_buffer[0]&0xfc)>>2]);
super.write(toBase64[((_buffer[0] &0x03)<<4) |
((_buffer[1]&0xf0)>>4)]);
super.write(toBase64[((_buffer[1]&0x0f)<<2) |
((_buffer[2]&0xc0)>>6)]);
super.write(toBase64[_buffer[2]&0x3f]);
这样操作的原理是什么
Top




