📄 receivefile.java
字号:
package com.valhalla.jbother.jabber.smack;import java.io.*;import java.net.*;import java.nio.channels.SocketChannel;import java.nio.channels.FileChannel;import java.nio.channels.ByteChannel;/** * Created by luke on Feb 8, 2005 10:45:47 AM *//** * this class implements methods for proper authentication and acquiring data certain JID */public class ReceiveFile extends FileTransfer{ private static String fId = "$Id$"; public ReceiveFile(StreamInitiation.FileDetails aFileDetails, String aHostname, int aPort, String aSid, String aJidFrom, String aJidTo) { super(aFileDetails, aHostname, aPort, aSid, aJidFrom, aJidTo); } /** * implementation of getByteChannel() * @return ByteChannel associated with the socket * @throws IOException */ public ByteChannel getByteChannel() throws IOException, ConnectException { ByteChannel channel = null; try { channel = SocketChannel.open(new InetSocketAddress(inetAddress,port)); } catch(java.net.ConnectException e) { cleanUp(); } return channel; } /** * implementation of getFileChannel() * @return FileChannel associated with output file * @throws FileNotFoundException */ public FileChannel getFileChannel() throws FileNotFoundException { return new FileOutputStream(fileDetails.getDestFile()).getChannel(); } /** * get the file. The process looks as follows: * <nl> * <li>1) Target sends a list of SOCKS5 authentication methods to Initiator * <li>2) Initiator picks one and returns it to Target * <li>3) Target tries to connect to Initiator using SOCKS5 CONNECT with hostname consisting * of magic sequence SHA1("SID + JID from + JID to") and port 0 * <li>4) Initiator verifies if this data is the same on its side, if so sends SOCKS5 * CONNECT with return code 0x00 * <li>5) Initiator puts the data to the socket * </nl> */ /** * does SOCKS5 authentication * @return true if Initiator authenticated successfully */ public boolean authenticate() { int count; if(isByteChannelNull()) { return false; } count = writeToSocket(Socks5Utils.createSOCKS5AuthenticationMethodsMessage()); count = readFromSocket(); if(buffer.get(1) != Socks5Utils.SOCKS_AUTH_NOAUTH ) { System.err.println("Initiator does not support SOCKS5 NOAUTH authentication."); System.err.println("Cannot continue !"); cleanUp(); return false; } count = writeToSocket(Socks5Utils. createSOCKS5AuthenticationMessage(getSHA1HashString(sid + jidFrom + jidTo),0)); count = readFromSocket(); if (count == -1) { System.err.println("No response from other side!"); cleanUp(); return false; } if(buffer.get(1) != 0x00) { System.err.println("Initiator didn't accept SOCKS5 CONNECT request"); System.err.println("Possible mismatch of stream ID, JID from or JID to"); cleanUp(); return false; } return true; } /** * gets the data from the socket and saves it to the file * @return true if operation succeeded */ public boolean getFile() { return transferFromSocketToFile(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -