📄 socks5receive.java
字号:
/** * $RCSfile$ * $Revision: 2495 $ * $Date: 2005-05-30 10:14:25 -0500 (Mon, 30 May 2005) $ * * Copyright 2003-2004 Jive Software. * * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.jivesoftware.smackx.filetransfer;import java.io.*;import org.jivesoftware.smackx.packet.*;import org.jivesoftware.smackx.filetransfer.jsocks.*;import org.jivesoftware.smack.*;import java.util.*;/** * Allows for receiving data via Socks5 Proxy * <br> * See <a href="http://www.jabber.org/jeps/jep-0065.html">JEP-0065</a> * * @author synic */public class Socks5Receive extends FileTransfer { private FileReceive rec; private OutputStream stream; private StreamHost host; private XMPPConnection connection; private long totalSize = 0; private ArrayList progress = new ArrayList(); private boolean ft = false; private boolean cancelled = false; /** * Used internally by the FileTransferManager for receiving files *@param rec The containing FileReceive object *@param stream The output stream to write the data to *@param host The host to read from */ protected Socks5Receive(FileReceive rec, OutputStream stream, StreamHost host) { this.rec = rec; this.stream = stream; this.host = host; this.connection = rec.getManager().getConnection(); this.totalSize = rec.getSize(); ft = true; } /** * Used for standalone receiving of Socks5 data * *@param connection The connection to use for this transfer *@param stream The output stream to save to *@param host The stream host to read from */ public Socks5Receive(XMPPConnection connection, OutputStream stream, StreamHost host) { this.connection = connection; this.stream = stream; this.host = host; } /** * Cancels the transfer */ public void cancel() { cancelled = true; } /** * Sets the FileProgressListeners who are interested in the progress of this transfer *@param list The listeners */ protected void setProgressListeners(ArrayList list) { this.progress = list; } /** * Reads from the Socks5 proxy and saves to the output stream *@throws XMPPException when there is an error getting the data from the host *@throws IOException when there is an error saving to the output stream */ public void save() throws XMPPException, IOException { String digest = FileTransferManager.getDigest(host.getSid() + host.getFrom() + connection.getUser()); StreamHost.Host h = null; ArrayList list = host.getHosts(); SocksSocket s = null; for(int i = 0; i < list.size(); i++) { try { h = (StreamHost.Host)list.get(i); s = new SocksSocket(new Socks5Proxy(h.getHost(), h.getPort()), digest, 0); s.getInputStream(); s.close(); break; } catch(Exception iox) { iox.printStackTrace(); } } int total = 0; if(h != null && s != null) { StreamHostUsed used = new StreamHostUsed(h.getJid()); used.setTo(host.getFrom()); used.setPacketID(host.getPacketID()); used.setFrom(connection.getUser()); connection.sendPacket(used); byte[] buf = new byte[4096]; int size = 0; boolean done = false; InputStream in = s.getInputStream(); while(!done) { if(cancelled) { if(ft) { FileTransferManager.update(FileTransferManager.Event.CANCELLED, progress, (float) total / (float) totalSize, total); } in.close(); stream.close(); return; } size = in.read(buf); if(ft) { FileTransferManager.update(FileTransferManager.Event.TRANSFERRING, progress, (float) total / (float) totalSize, total); } if(size != -1) { total += size; stream.write(buf, 0, size); } else done = true; } stream.close(); in.close(); if(ft && totalSize != total) { FileTransferManager.update(FileTransferManager.Event.CANCELLED, progress, (float) total / (float) totalSize, total); return; } if(ft) { FileTransferManager.update(FileTransferManager.Event.DONE, progress, 1, total); } return; } else { throw new XMPPException("Couldn't establish a connection."); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -