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

📄 message.java

📁 j2me简单实例,j2me教程加源码,希望大家喜欢
💻 JAVA
字号:
package com.j2medev.chapter5;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Message {
    //定义type的常量
    public static final int ALL_FRAMES = -1;
    public static final int SIGNUP = 0;
    public static final int SIGNUP_ACK = 1;
    public static final int CLIENT_STATUS = 2;
    public static final int SERVER_STATUS = 3;
    public static final int ERROR = 4;
    public static final int SIGNOFF = 5;

    private int type;//消息类型
    private int size;//body的大小
    private int[] body;//body的内容

    public Message(int type) {
        this.type = type;
        this.size = 0;
    }

    public Message(int type, int data) {
        this.type = type;
        this.size = 1;
        body = new int[] {
            data
        };
    }
    
    public Message(int type,int[] data){
        this.type = type;
        this.size = data.length;
        this.body = data;
    }

    public int getType() {
        return type;
    }

    public int getSize() {
        return size;
    }

    public int[] getBody() {
        return body;
    }
    //发送数据 
    public void send(DataOutputStream out) throws IOException {
        out.writeInt(type);
        out.writeInt(size);
        for (int i = 0; i < size; i++) {
            out.writeInt(body[i]);
        }
        out.flush();
    }
    //解析收到的数据
    public static Message read(DataInputStream in)
    throws IOException {
        Message msg = new Message(in.readInt()); 
        msg.size = in.readInt();
        msg.body = new int[msg.getSize()];
        
        for (int i = 0; i < msg.getSize(); i++) {
            msg.body[i] = in.readInt();
        }
        return msg;
    }
    
}

⌨️ 快捷键说明

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