📄 message.java
字号:
/* * Copyright (c) 2002, 2003 Sun Microsystems, Inc. All Rights Reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based * names, or the names of contributors may be used to endorse or promote * products derived from this software without specific prior written * permission. * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN * MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or maintenance * of any nuclear facility. $Id: Message.java,v 1.2 2003/05/29 22:39:16 wildcard Exp $ */package com.sun.j2me.blueprints.slugs.shared;import java.io.*;/** * This class represents messages that are being * exchanged between the server and clients. * Messages have a frame number, a type, a size * field, and a body which is an array of integers * of the length described by the size field. */public class Message { 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 static final String[] TYPE_STRING = { "signup", "signup_ack", "client_status", "server_status", "error", "signoff" }; private int frame_no; private int type; private int size; private int[] body; /** * Create a new instance with frame number * 'frame_no', type 'type', and no body. */ public Message(int frame_no, int type) { this.frame_no = frame_no; this.type = type; this.size = 0; } /** * Create a new instance with frame number * 'frame_no', type 'type', and body 'data'. */ public Message(int frame_no, int type, int data) { this.frame_no = frame_no; this.type = type; this.size = 1; body = new int[] { data }; } /** * Create a new instance with frame number * 'frame_no', a type depending on the type of * 'data', and a body that is the integer * representation of 'data'. */ public Message(int frame_no, Data data) { this.frame_no = frame_no; if (data instanceof Item) { type = CLIENT_STATUS; } else if (data instanceof Frame) { type = SERVER_STATUS; } else { throw new IllegalArgumentException(); } body = data.toIntegerArray(); size = body.length; } /** * Return the frame number of this instance */ public int getFrameNumber() { return frame_no; } /** * Return the type of this instance */ public int getType() { return type; } /** * Return the size of this instance */ public int getSize() { return size; } /** * Return the body of this instance */ public int[] getBody() { return body; } /** * Write this instance to 'out' */ public void archive(DataOutputStream out) throws IOException { out.writeInt(frame_no); out.writeInt(type); out.writeInt(size); for (int i = 0; i < size; i++) { out.writeInt(body[i]); } out.flush(); } /** * Return a newly created instance read from 'in' */ public static Message createFromStream(DataInputStream in) throws IOException { Message msg = new Message(in.readInt(), 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; } /** * Return a string representation of this instance */ public String toString() { return "Message: frame # = " + frame_no + ", type = " + TYPE_STRING[type]; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -