📄 filesend.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 org.jivesoftware.smack.filter.*;import org.jivesoftware.smackx.packet.*;import org.jivesoftware.smack.*;import org.jivesoftware.smack.packet.*;import java.util.*;import java.io.*;/** * Used to send a file to a user. Used internally by the FileTransferManager * *@author Adam Olsen */public class FileSend extends FileTransfer { private StreamInitiation si = null; private FileTransferManager manager = null; private StreamInitiation.FileDetails details = null; private InputStream stream = null; private ArrayList progress = new ArrayList(); private FileTransfer transfer; private File file; /** * Initializes a file to send * *@param manager The containing FileTransferManager class *@param stream The InputStream to read from *@param name The name of the file to send *@param desc The description of the file to send *@param size The size of the file */ FileSend(FileTransferManager manager, InputStream stream, String name, String desc, long size) { this.stream = stream; this.manager = manager; int preferred = manager.getPreferredType(); si = new StreamInitiation(preferred); details = new StreamInitiation.FileDetails(name, desc, size); si.setFileDetails(details); } /** * Initializes a file to send * *@param manager The containing FileTransferManager class *@param file The file to send *@param desc The description of the file to send *throws IOException Description of the Exception *@throws IOException thrown if there is an error opening the file */ FileSend(FileTransferManager manager, File file, String desc) throws IOException { this.file = file; this.manager = manager; si = new StreamInitiation(); stream = new FileInputStream(file); details = new StreamInitiation.FileDetails(file); details.setDescription(desc); si.setFileDetails(details); } FileTransferManager getManager() { return manager; } StreamInitiation getSI() { return si; } /** *@return the size of the file */ public long getSize() { return details.getFileSize(); } public String getFrom() { return si.getFrom(); } public String getTo() { return si.getTo(); } public String getFileLocation() { if(file == null) return ""; return file.getPath(); } public void setTo(String to) { si.setTo(to); } /** *@return the name of the file */ public String getName() { return details.getFileName(); } /** *@return the description of the file */ public String getDescription() { return details.getDescription(); } /** * Adds a progress listener interested in the progress of this file * *@param listener The listener interested in the progress of this file */ public synchronized void addProgressListener( FileProgressListener listener) { progress.add(listener); } /** * Removes a progress listener * *@param listener The listener to remove */ public synchronized void removeProgressListener( FileProgressListener listener) { int index = progress.indexOf(listener); if (index != -1) { progress.remove(index); } } /** * Cancels the transfer */ public void cancel() { FileTransferManager.update(FileTransferManager.Event.CANCELLED, progress, 0, 0); if(transfer != null) transfer.cancel(); } /** * Sends the file * *@param to The JID of the user to send this file to *@param type Either FileTransferManager.TYPE_SOCKS5 or TYPE_IBB *@throws XMPPException When there is an error sending the file *@throws IOException When there is an error reading the inputstream */ public void send(String to) throws XMPPException, IOException { si.setTo(to); si.setType(IQ.Type.SET); si.setFrom(manager.getConnection().getUser()); FileTransferManager.update(FileTransferManager.Event.CONNECTING, progress, 0, 0); PacketCollector collector = manager.getConnection().createPacketCollector( new PacketIDFilter(si.getPacketID())); manager.getConnection().sendPacket(si); IQ result = (IQ) collector.nextResult(manager.getTimeout()); collector.cancel(); if (result == null || !(result instanceof StreamInitiation) || result.getType() == IQ.Type.ERROR) { if(result != null && result.getError() != null) { throw new XMPPException(result.getError()); } throw new XMPPException("File transfer could not be initiated. "); } int type = manager.getPreferredType(); if(type == FileTransferManager.TYPE_SOCKS5 || type == -1) { Socks5Send s = new Socks5Send(this,stream); s.setProgressListeners(progress); transfer = s; s.send(to); } else { IBBSend send = new IBBSend(this, stream); send.setProgressListeners(progress); transfer = send; send.send(to); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -