⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ibbreceive.java

📁 JBother是纯Java开发的Jabber(即时消息开源软件)客户端。支持群组聊天
💻 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.smack.*;import org.jivesoftware.smack.filter.*;import org.jivesoftware.smackx.packet.*;import org.jivesoftware.smack.packet.*;import org.jivesoftware.smack.util.*;import java.util.*;/** *  Receives an inband byte stream * *@author    synic */public class IBBReceive extends FileTransfer {    private FileReceive receive;    private XMPPConnection con;    private OutputStream stream;    private IBBOpen req;    private int current = -1;    private ArrayList progress = new ArrayList();    private long total = 0;    private long totalBytes = 0;    private boolean ft = false;    private boolean cancelled = false;    /**     *  Used when receiving a stream in a file transfer     *     *@param  receive  The FileReceive object in charge of this stream     *@param  stream   The stream to write the data to     *@param  req      The request that initiated this stream     */    IBBReceive(FileReceive receive, OutputStream stream, IBBOpen req) {        this.receive = receive;        this.stream = stream;        this.req = req;        this.total = receive.getSI().getFileDetails().getFileSize();        this.con = receive.getManager().getConnection();        this.ft = true;    }    /**     *  Constructor used for a standalone IBB stream     *     *@param  connection  the connection associated with this stream     *@param  stream      The stream to write the data to     *@param  req         The request that initiated this stream     */    public IBBReceive(XMPPConnection connection, OutputStream stream, IBBOpen req) {        this.req = req;        this.stream = stream;        this.con = connection;    }    /**     *  The progress listeners (for file transfer)     *     *@param  listeners  the listeners interested in the progress of this stream     */    protected void setProgressListeners(ArrayList listeners) {        this.progress = listeners;    }    /**     * Cancels the transfer    */    public void cancel() { cancelled = true; }    /**     *  Saves a file transfer to an OutputStream     *     *@throws  XMPPException  Thrown when an error occurs in communicating     *      with sender     *@throws  IOException    Thrown when an error occurs writing to the     *      output stream     */    public void save() throws XMPPException, IOException {        OrFilter f = new OrFilter(new PacketExtensionFilter("data",        "http://jabber.org/protocol/ibb"),        new PacketTypeFilter(IBBClose.class));        PacketCollector collector = con.createPacketCollector(f);        con.sendPacket(req.createConfirmation());        int timeout = SmackConfiguration.getPacketReplyTimeout();        if (ft) {            timeout = receive.getManager().getTimeout();        }        Packet result = null;        while ((result = (Packet) collector.nextResult(timeout)) != null) {            if (result instanceof Message) {                Message message = (Message) result;                if(cancelled)                {                    if(ft)                    {                        FileTransferManager.update(FileTransferManager.Event.CANCELLED, progress,                            (float) totalBytes / (float) total, totalBytes);                    }                    collector.cancel();                    stream.close();                    message.setError(new XMPPError(403, "Cancelled"));                    message.setTo(message.getFrom());                    message.setFrom(receive.getManager().getConnection().getUser());                    receive.getManager().getConnection().sendPacket(message);                    return;                }                IBBChunk chunk = (IBBChunk) message.getExtension("data",                "http://jabber.org/protocol/ibb");                if (chunk == null) {                    continue;                }                if (!chunk.getSid().equals(req.getSid())) {                    continue;                }                if (chunk.getSeq() != current + 1) {                    throw new XMPPException("File transfer failed - a packet was lost.");                }                current++;                byte bytes[] = StringUtils.decodeBase64(chunk.getData().trim());                totalBytes += bytes.length;                if (ft) {                    FileTransferManager.update(FileTransferManager.Event.TRANSFERRING,                    progress, (float) totalBytes / (float) total, totalBytes);                }                try {                    stream.write(bytes);                } catch( IOException iox ) {                    Message error = new Message(result.getFrom());                    error.setError(new XMPPError(504, "Error saving file"));                    con.sendPacket(message);                    stream.close();                    throw iox;                }            } else if (result instanceof IBBClose) {                IBBClose close = (IBBClose) result;                if (close.getSid().equals(req.getSid())) {                    if (ft) {                        FileTransferManager.update(FileTransferManager.Event.DONE,                        progress, 1.00, totalBytes);                    }                    collector.cancel();                    return;                }            } else if (result instanceof IQ) {                IQ r = (IQ) result;                if (r.getType().equals("error")) {                    throw new XMPPException("Error receiving file.");                }            }        }        collector.cancel();        throw new XMPPException("File transfer did not complete.");    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -