📄 volatiledownloader.java
字号:
/*
* @(#)VolatileDownloader.java
*
* Copyright (c) 2001-2002, JangHo Hwang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the JangHo Hwang nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: VolatileDownloader.java,v 1.2 2002/03/05 13:35:33 xrath Exp $
*/
package rath.msnm.ftp;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.net.Socket;
import rath.msnm.MSNMessenger;
import rath.msnm.msg.MimeMessage;
/**
* 颇老阑 荐脚夸没阑 倾遏沁阑锭, 角力肺 颇老阑 促款肺靛窍绰 努贰胶捞促.
*
* @author Jang-Ho Hwang, rath@linuxkorea.co.kr
* @version $Id: VolatileDownloader.java,v 1.2 2002/03/05 13:35:33 xrath Exp $
*/
public class VolatileDownloader extends Thread implements VolatileTransfer
{
String cookie = null;
private final MSNMessenger msn;
private boolean isLive = true;
private String host = null;
private int port;
private String authCookie = null;
private String loginName = null;
private File file = null;
private int filesize = -1;
private volatile int offset;
private Socket socket = null;
private InputStream rawIn = null;
private InputStreamReader in = null;
private PrintWriter out = null;
private VolatileDownloader( MSNMessenger msn )
{
this.msn = msn;
}
/**
* VolatileDownloader狼 牢胶畔胶甫 积己茄促.
*/
public static VolatileDownloader getInstance( MSNMessenger msn, File toReceive,
MimeMessage msg )
{
String host = msg.getProperty("IP-Address");
int port = Integer.parseInt(msg.getProperty("Port"));
String authCookie = msg.getProperty("AuthCookie");
String loginName = msn.getLoginName();
if( host==null || authCookie==null )
throw new IllegalArgumentException("Insufficient mime property");
VolatileDownloader vd = new VolatileDownloader(msn);
vd.host = host;
vd.port = port;
vd.authCookie = authCookie;
vd.loginName = loginName;
vd.file = toReceive;
return vd;
}
/**
* 价脚螟 Host 林家甫 掘绢柯促.
*/
public String getHostAddress()
{
return this.host;
}
/**
* 价脚螟 port 锅龋甫 掘绢柯促.
*/
public int getPort()
{
return this.port;
}
/**
* 脚盔犬牢阑 困秦 傈价且 格利栏肺 汲沥等 牢刘捻虐蔼阑 掘绢柯促.
*/
public String getAuthCookie()
{
return this.authCookie;
}
/**
* 捞 颇老傈价 技记俊 措茄 捻虐蔼阑 掘绢柯促.
*/
public String getCookie()
{
return this.cookie;
}
/**
* 荐脚瞪 颇老 按眉甫 馆券茄促.
*/
public File getFile()
{
return this.file;
}
public String getFilename()
{
return this.file.getName();
}
/**
* 泅犁 角力肺 荐脚等 byte 荐甫 馆券茄促.
*/
public int getCurrentFileSize()
{
return this.offset;
}
public int getCommitPercent()
{
return (int)(((double)offset / (double)filesize) * 100.0D);
}
/**
* 罐阑 颇老狼 傈眉 农扁甫 掘绢柯促. 捞 荐摹绰 ProgressBar殿阑
* 备泅窍绰单 蜡侩茄 蔼捞 登绢 临 巴捞促.
* 父距 -1捞 馆券等促搁, 酒流 颇老农扁 沥焊甫 掘瘤 给茄巴捞聪,
* 炼陛 扁促赴 饶 促矫 荐青窍扁 官鄂促.
*/
public int getFileSize()
{
return this.filesize;
}
/**
* Thread main loop 贸府 何盒捞促.
*/
public final void run()
{
try
{
fireStart();
makeConnection();
sendMessage( "VER", "MSNFTP" );
while( isLive )
{
String line = readLine();
if( line==null )
break;
String header = line.substring( 0, 3 );
String body = "";
if( line.length()>4 )
body = line.substring(4);
processMessage( header, body );
}
}
catch( Throwable e )
{
fireError( e );
}
finally
{
close();
}
}
/**
* InputStreamReader甫 烹秦 茄临阑 佬绢林绰 皋家靛.
* BufferedReader甫 荤侩窍搁 buffering登绰 苞沥俊辑 binary file data甫
* 佬绢滚府绰 版快啊 惯积且 困氰捞 乐栏骨肺, 流立 备泅登菌促.
*/
private String readLine() throws IOException
{
CharArrayWriter caw = new CharArrayWriter(40);
int buf;
while( (buf=in.read())!=-1 )
{
if( buf=='\r' )
continue;
if( buf=='\n' )
return caw.toString();
caw.write( buf );
}
if( caw.size()!=0 )
return caw.toString();
return null;
}
public void processMessage( String header, String body ) throws Throwable
{
if( header.equals("VER") )
{
sendMessage( "USR", loginName + " " + authCookie );
}
else
if( header.equals("FIL") )
{
this.filesize = Integer.parseInt(body);
sendMessage( "TFR" );
getFileContent();
sendMessage( "BYE", "16777989" );
isLive = false;
}
}
public void sendMessage( String header ) throws IOException
{
StringBuffer sb = new StringBuffer(6);
sb.append( header );
sb.append( "\r\n" );
out.print( sb.toString() );
out.flush();
}
public void sendMessage( String header, String body ) throws IOException
{
StringBuffer sb = new StringBuffer(64);
sb.append( header );
sb.append( ' ' );
sb.append( body );
sb.append( "\r\n" );
out.print( sb.toString() );
out.flush();
}
public void getFileContent() throws IOException
{
byte[] buf = new byte[ 2045 ];
FileOutputStream fos = new FileOutputStream(this.file);
try
{
while( true )
{
int pad = rawIn.read();
int size = rawIn.read() | (rawIn.read()<<8);
if( size==0 )
break;
int block, readlen = 0;
while( (block=rawIn.read(buf, readlen, size-readlen))!=-1 )
{
readlen += block;
if( readlen==size ) break;
}
if( readlen!=size )
throw new IOException( "stream closed" );
offset += readlen;
fos.write( buf, 0, readlen ); // IndexOutOfBounds error!
fos.flush();
if( offset >= filesize )
break;
}
}
finally
{
try { fos.close(); } catch( IOException e ) {}
}
}
public void close()
{
isLive = false;
if( in!=null )
{
try{ in.close(); } catch( Exception e ) {}
}
if( out!=null )
{
try{ out.close(); } catch( Exception e ) {}
}
if( socket!=null )
{
try{ socket.close(); } catch( Exception e ) {}
}
}
protected void fireStart()
{
msn.fireFileReceiveStartedEvent( this );
}
/**
* 颇老 傈价档吝 抗扁摹 给茄 抗寇啊 惯积窍看阑锭, 阂妨柳促.
* 捞 皋家靛俊辑绰 抗寇 捞亥飘甫 惯价秦临 巴捞促.
*/
protected void fireError( Throwable e )
{
msn.fireFileReceiveErrorEvent( this, e );
}
/**
* 辑滚俊 立加窍绊 胶飘覆甸阑 积己茄促.
*/
protected void makeConnection() throws IOException
{
this.socket = new Socket( this.host, this.port );
this.rawIn = socket.getInputStream();
this.in = new InputStreamReader(rawIn);
this.out = new PrintWriter(socket.getOutputStream());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -