📄 filetransfermessage.java
字号:
/*
* @(#)FileTransferMessage.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: FileTransferMessage.java,v 1.5 2002/04/18 16:47:54 xrath Exp $
*/
package rath.msnm.msg;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.ServerSocket;
import java.util.Random;
/**
* 颇老傈价俊 包访茄 MIME 皋矫瘤甸阑 颊奖霸 积己窍绊, 促逢 荐 乐霸秦林绰
* 努贰胶捞促.
*
* @author Jang-Ho Hwang, rath@linuxkorea.co.kr
* @version $Id: FileTransferMessage.java,v 1.5 2002/04/18 16:47:54 xrath Exp $
*/
public class FileTransferMessage extends MimeMessage
{
private static final Random random = new Random(System.currentTimeMillis());
private FileTransferMessage()
{
setKind( KIND_FILE_TRANSFER );
}
/**
* 颇老 价脚 夸没 皋矫瘤甫 积己茄促. 价脚螟俊辑 积己窍绰 皋矫瘤捞促.
* <p>
* 烙狼肺 积己等 捻虐蔼阑 舅绊磊 茄促搁, 积己等 颇老傈价皋矫瘤 按眉俊
* getProperty("Invitation-Cookie") 甫 invoke窍咯 舅酒杭 荐 乐促.
*
* @param toSend 傈价窍绊磊 窍绰 颇老
*/
public synchronized static FileTransferMessage createInviteMessage( File toSend )
{
int cookie = random.nextInt( 999998 ) + 1;
return createInviteMessage( toSend, cookie );
}
/**
* 颇老 价脚 夸没 皋矫瘤甫 积己茄促. 价脚螟俊辑 积己窍绰 皋矫瘤捞促.
*
* @param toSend 傈价窍绊磊 窍绰 颇老
* @param cookie 檬措 捻虐蔼. 0焊促 奴 箭磊咯具父 茄促.
*/
public static FileTransferMessage createInviteMessage( File toSend,
int cookie )
{
if( cookie < 1 )
throw new IllegalArgumentException( "cookie must larger than 0" );
if( !toSend.exists() )
throw new IllegalArgumentException( toSend.getName() + " not found" );
FileTransferMessage msg = new FileTransferMessage();
msg.setProperty( "Application-Name", "颇老 傈价" );
msg.setProperty( "Application-GUID", "{5D3E02AB-6190-11d3-BBBB-00C04F795683}" );
msg.setProperty( "Invitation-Command", "INVITE" );
msg.setProperty( "Invitation-Cookie", String.valueOf(cookie) );
msg.setProperty( "Application-File", toSend.getName() );
msg.setProperty( "Application-FileSize", String.valueOf(toSend.length()) );
return msg;
}
/**
* 颇老 荐脚阑 倾遏窍绰 皋矫瘤甫 积己茄促. 荐脚螟俊辑 积己窍绰 皋矫瘤捞促.
*
* @param cookie 价脚磊肺何磐 罐篮 捻虐蔼.
*/
public static FileTransferMessage createAcceptMessage( int cookie )
{
FileTransferMessage msg = new FileTransferMessage();
msg.setProperty( "Invitation-Command", "ACCEPT" );
msg.setProperty( "Invitation-Cookie", String.valueOf(cookie) );
msg.setProperty( "Launch-Application", "FALSE" );
msg.setProperty( "Request-Data", "IP-Address:" );
return msg;
}
/**
* 颇老 荐脚阑 芭何窍绰 皋矫瘤甫 积己茄促. 荐脚螟俊辑 积己窍绰 皋矫瘤捞促.
*
* @param cookie 价脚磊肺何磐 罐篮 捻虐蔼.
*/
public static FileTransferMessage createRejectMessage( int cookie )
{
FileTransferMessage msg = new FileTransferMessage();
msg.setProperty( "Invitation-Command", "CANCEL" );
msg.setProperty( "Invitation-Cookie", String.valueOf(cookie) );
msg.setProperty( "Cancel-Code", "REJECT" );
return msg;
}
/**
* 辑肺埃狼 颇老 价/荐脚 倾遏捞 冻绢脸栏搁 价脚螟俊辑 颇老阑 傈价且
* 磊脚 辑滚狼 沥焊甫 焊郴绰 皋矫瘤捞促. <br>
* 积己瞪锭 烙狼狼 牢刘捻虐甫 窍唱 歹 积己窍绰单, 捞 蔼篮 AuthCookie 扼绰
* key肺 积己登骨肺, getProperty 皋家靛甫 烹秦 馆靛矫 犬牢窍档废 茄促.
* <p>
* 捞 皋矫瘤甫 荐脚窍搁 梆官肺 荐脚螟俊辑 立加窍霸 登骨肺 bind甫 茄 饶俊
* 傈价秦具父 茄促.
* <p>
* 肚茄 磊脚狼 ip绰 InetAddress.getLocalHost()甫 荤侩窍咯 掘绢辰促.<br>
* port 锅龋绰 扁夯蔼牢 6891阑 货侩窍霸 等促.
* <p>
* 父距 秦寸 器飘锅龋啊 荤侩吝捞扼搁, 6891~7000荤捞狼 促弗 器飘锅龋甫 荤侩茄促.
*
* @param cookie 檬扁 傈价窍看带 捻虐蔼.
*/
public static FileTransferMessage createTransferMessage( int cookie )
{
return createTransferMessage( cookie, 6891 );
}
/**
* 辑肺埃狼 颇老 价/荐脚 倾遏捞 冻绢脸栏搁 价脚螟俊辑 颇老阑 傈价且
* 磊脚 辑滚狼 沥焊甫 焊郴绰 皋矫瘤捞促. <br>
* 积己瞪锭 烙狼狼 牢刘捻虐甫 窍唱 歹 积己窍绰单, 捞 蔼篮 AuthCookie 扼绰
* key肺 积己登骨肺, getProperty 皋家靛甫 烹秦 馆靛矫 犬牢窍档废 茄促.
* <p>
* 捞 皋矫瘤甫 荐脚窍搁 梆官肺 荐脚螟俊辑 立加窍霸 登骨肺 bind甫 茄 饶俊
* 傈价秦具父 茄促.
* <p>
* 肚茄 磊脚狼 ip绰 InetAddress.getLocalHost()甫 荤侩窍咯 掘绢辰促.<br>
* 父距 盔窍绰 port锅龋啊 捞固 bind等巴捞扼搁 7000锅鳖瘤 1究 刘啊窍哥 bind啊
* 瞪锭鳖瘤 馆汗窍霸 等促.
*
* @param cookie 檬扁 傈价窍看带 捻虐蔼.
* @param port 颇老 傈价俊 荤侩且 器飘 锅龋.
*/
public static FileTransferMessage createTransferMessage( int cookie, int port )
{
String host = null;
try
{
host = InetAddress.getLocalHost().getHostAddress();
host = System.getProperty("jmsn.file.host", host);
}
catch( UnknownHostException e )
{
throw new Error("Can't get localhost address");
}
FileTransferMessage msg = new FileTransferMessage();
msg.setProperty( "Invitation-Command", "ACCEPT" );
msg.setProperty( "Invitation-Cookie", String.valueOf(cookie) );
msg.setProperty( "AuthCookie", createAuthCookie() );
msg.setProperty( "Launch-Application", "FALSE" );
msg.setProperty( "Request-Data", "IP-Address:" );
msg.setProperty( "IP-Address", host );
msg.setProperty( "Port", String.valueOf(msg.getAvailablePort(port)) );
return msg;
}
/**
* 林绢柳 port何磐 port+100鳖瘤 1究 刘啊窍搁辑 葛电 器飘甫 八荤窍咯 bind啊瓷茄
* port 锅龋甫 馆券茄促.
*
* @return bind啊瓷茄 器飘锅龋. 父距 啊瓷茄 器飘锅龋啊 窍唱档 绝促搁 -1阑 馆券茄促.
*/
protected synchronized int getAvailablePort( int port )
{
for(int i=port, limit=port+100; i<limit; i++)
{
ServerSocket socket = null;
try
{
socket = new ServerSocket(i);
}
catch( IOException e ) {}
finally
{
try {
socket.close();
} catch( Exception e ) {}
}
if( socket!=null )
return i;
}
return -1;
}
/**
* 8磊府狼 烙狼狼 牢刘 捻虐蔼阑 积己秦辰促.
*/
private static String createAuthCookie()
{
StringBuffer sb = new StringBuffer();
for(int i=0; i<8; i++)
sb.append( (int)(Math.random()*9.0D) + 1 );
return sb.toString();
}
public byte[] getBytes() throws UnsupportedEncodingException
{
if( getKind()!=KIND_FILE_TRANSFER )
return super.getBytes();
StringBuffer buf = createMimeHeader( STR_FILE_TRANSFER + "; charset=UTF-8" );
buf.append( "\r\n" );
fillMimeProperties( buf );
buf.append( "\r\n" );
String msg = buf.toString();
return msg.getBytes("UTF-8");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -