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

📄 businessouthelper.java

📁 局域网聊天系统
💻 JAVA
字号:
package com.soft.QQ;

import java.nio.*;
import java.io.*;
import static com.soft.QQ.Config.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2008</p>
 *
 * <p>Company: soft136</p>
 *
 * @author Michael
 * @version 1.0
 */
public class BusinessOutHelper {

    private ByteBuffer bb = null;
    private int length = 0;
    private int pos;

    public BusinessOutHelper(int allocatesize,int i) {
        bb = ByteBuffer.allocate(allocatesize);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        bb.position(0);

        initHead(allocatesize,i);//初始化包头
    }

    private void initHead(int allocatesize,int i) {
        bb.putInt(PROTOCOL_UDP);
        if(i==1){
                 bb.putInt(BUSINESS_SIMPLEMESSAGE);
        }else if(i==2){
                 bb.putInt(BUSINESS_USERINFO);
        }else if(i==3){
                 bb.putInt(BUSINESS_LISTINFO);
        }
        bb.putInt(PROTOCOL_VERSION);
        bb.putInt(SOFT_VERSION);
        pos = bb.position();
        bb.putInt(length);
        bb.putLong(Config.getSerialNo());
    }

    public void writeString(String input, int length) throws
            IOException {
        int i = 0;
        int strPos = 0;
        byte[] buf = null;
        try {
            buf = input.getBytes("GBK");
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        int strLen = buf.length;
        while (i < length) {
            byte ch = (strPos < strLen ? buf[strPos++] : 0);
            bb.put(ch);
            i++;
        }
    }

    public byte[] getBytes() throws IOException {
        if (bb.hasArray()) {
            length = bb.position();
            bb.position(pos);
            bb.putInt(length);
            bb.position(length);
            return bb.array();
        } else {
            return null;
        }

    }

    public String getString(int length) throws IOException {
        byte[] buf = new byte[length];
        for (int i = 0; i < length; i++) {
            buf[i] = bb.get();
        }
        return new String(buf, "GBK").trim();
    }

}

⌨️ 快捷键说明

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