edu.tsinghua.lumaqq.qq.packets.out._05.TransferPacket.java Source code

Java tutorial

Introduction

Here is the source code for edu.tsinghua.lumaqq.qq.packets.out._05.TransferPacket.java

Source

/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 notXX
*                    luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.qq.packets.out._05;

import static org.apache.commons.codec.digest.DigestUtils.md5;
import java.nio.ByteBuffer;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.beans.QQUser;
import edu.tsinghua.lumaqq.qq.packets.PacketParseException;
import edu.tsinghua.lumaqq.qq.packets._05OutPacket;

/**
 * <pre>
 * ??
 * ??
 * 1. 
 * 2. 8
 * 3. session id, 4
 * 4. 4
 * 5. ???2?
 * 6. 25?
 * 7. md5
 * 8. ??md5
 * 9. 4
 * 10. ??2
 * 11. ??
 * 12. 8
 * 13. 
 * 
 * ???
 * 1. 
 * 2. 8??0x1000000000000001?
 * 3. session id, 4
 * 4. 4
 * 5. ?2
 * 6. ?
 * 7. 
 * 
 * ????
 * 1. 
 * 2. 8
 * 3. session id, 4
 * 4. 4
 * 5. ???2
 * 6. 40
 * 7. 
 * 
 * ??
 * 1. 
 * 2. 8
 * 3. session id, 4
 * 4. 4
 * 5. ???2
 * 6. 0x0250x0001
 * 7. 
 * </pre>
 *  
 * @author luma
 */
public class TransferPacket extends _05OutPacket {
    // 
    private int sessionId;

    // ???
    private byte[] md5;
    private int imageLength;
    private String fileName;

    // ???
    private byte[] fragment;

    private boolean data;
    private boolean last;

    private boolean requestSend;
    private boolean dataReply;

    /**
     * @param user
     * @param data
     *       true?
     * @param last
     *       true??
     */
    public TransferPacket(QQUser user, boolean data, boolean last) {
        super(QQ.QQ_05_CMD_TRANSFER, data ? (last ? true : false) : true, user);
        this.data = data;
        this.last = last;
        requestSend = true;
    }

    /**
     * ???
     * 
     * @param user
     */
    public TransferPacket(QQUser user) {
        super(QQ.QQ_05_CMD_TRANSFER, false, user);
        requestSend = false;
        dataReply = false;
    }

    /**
     * @param buf
     * @param length
     * @param user
     * @throws PacketParseException
     */
    public TransferPacket(ByteBuffer buf, int length, QQUser user) throws PacketParseException {
        super(buf, length, user);
    }

    @Override
    public String getPacketName() {
        return "Transfer Packet";
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.packets.Packet#putBody(java.nio.ByteBuffer)
     */
    @Override
    protected void putBody(ByteBuffer buf) {
        if (!requestSend) {
            // 2. 8
            buf.putLong(0x0100000000000000L);
            // 3. session id, 4           
            buf.putInt(sessionId);
            // 4
            buf.putInt(0);
            // ??
            if (dataReply) {
                buf.putChar((char) 0x0001);
                buf.put((byte) 0x02);
            } else {
                buf.putChar((char) 0x04);
                buf.putInt(0);
            }
        } else if (data) {
            // 2. 8??0x1000000000000001?
            if (last)
                buf.putLong(0x0100000000000000L);
            else
                buf.putLong(0x0100000000000001L);
            // 3. session id, 4
            buf.putInt(sessionId);
            // 4. 4
            buf.putInt(0);
            // 5. ?2
            buf.putChar((char) fragment.length);
            // 6. ?
            buf.put(fragment);
        } else {
            // 2. 8
            buf.putLong(0x0100000000000000L);
            // 3. session id, 4
            buf.putInt(sessionId);
            // 4. 4
            buf.putInt(0);
            // 5. ???2
            buf.putChar((char) 0);
            // 6. 25?
            int pos = buf.position();
            buf.putChar((char) 0);
            // 7. md5
            buf.put(md5);
            // 8. ??md5
            byte[] fileNameBytes = fileName.getBytes();
            buf.put(md5(fileNameBytes));
            // 9. 4
            buf.putInt(imageLength);
            // 10. ??2
            buf.putChar((char) fileName.length());
            // 11. ??
            buf.put(fileNameBytes);
            // 12. 8
            buf.putLong(0);

            char len = (char) (buf.position() - pos);
            buf.putChar(pos - 2, len);
            buf.putChar(pos, len);
        }
    }

    /**
     * @return Returns the imageLength.
     */
    public int getImageLength() {
        return imageLength;
    }

    /**
     * @param imageLength The imageLength to set.
     */
    public void setImageLength(int imageLength) {
        this.imageLength = imageLength;
    }

    /**
     * @return Returns the md5.
     */
    public byte[] getMd5() {
        return md5;
    }

    /**
     * @param md5 The md5 to set.
     */
    public void setMd5(byte[] md5) {
        this.md5 = md5;
    }

    /**
     * @return Returns the sessionId.
     */
    public int getSessionId() {
        return sessionId;
    }

    /**
     * @param sessionId The sessionId to set.
     */
    public void setSessionId(int sessionId) {
        this.sessionId = sessionId;
    }

    /**
     * @return Returns the fileName.
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * @param fileName The fileName to set.
     */
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    /**
     * @return Returns the fragment.
     */
    public byte[] getFragment() {
        return fragment;
    }

    /**
     * @param fragment The fragment to set.
     */
    public void setFragment(byte[] fragment) {
        this.fragment = fragment;
    }

    /**
     * @return Returns the requestSend.
     */
    public boolean isRequestSend() {
        return requestSend;
    }

    /**
     * @param requestSend The requestSend to set.
     */
    public void setRequestSend(boolean requestSend) {
        this.requestSend = requestSend;
    }

    public boolean isDataReply() {
        return dataReply;
    }

    public void setDataReply(boolean dataReply) {
        this.dataReply = dataReply;
    }
}