/* * Copyright (C) 1995, 1996, 1997 By Radu Sion * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Radu Sion (radus@cs.pub.ro) * * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * (radus@cs.pub.ro, http://www.dsp.pub.ro/radu.html) * */ import java.io.*; import util; import MessageConstants; /** * A class designed to be used as a general network byte message wrapping class.

* For a Message to be valid, it should be created using some MessageCreator * method and then parsed using the MessageParser. It's not enough to create * a Message with it's constructor and just feed it further. You should use the MessageCreator * for any message you create. The simplicity is the same.
* [IN WORK: Implementing Extensive Validity]

* Written: Radu Sion
* Version: 0.21
* Source: Message.java * @see MessageCreator * @see MessageParser */ class Message extends Object { byte data[]; /** * Constructs a null message based on a given length. * No checkings are performed on parameters. * @param len Length in bytes of the new null initialized message */ public Message(int len) { data = new byte[len]; } /** * Constructs a message based on a bytes array. * No checkings are performed on parameters. * @param oo[] Bytesarray used to initialize the message */ public Message(byte oo[]) { data = oo; } /** * Constructs a message based on a string. * No checkings are performed on parameters. * The the string is used only as ASCII so you will loose extra * EBDIC information. (for now) * @param ss String used to initialize the message */ public Message(String ss) { data = new byte[ss.length()]; ss.getBytes(0,ss.length(),data,0); } /** * Constructs a message based on another message, it is just like duplicating. * No checkings are performed on parameters. * @param msg Original message to copy */ public Message(Message msg) { data = msg.getBytes(); } /** * Reads a message from a DataInputStream. * Assumes the stream is open & running. * No parameter checkings performed. * @param dis The InputStream * @return null if error, a new Message if ok. * @exception IOException If some error occured at the stream. */ public static Message readDataInputStream(DataInputStream dis) throws IOException { int len = dis.readInt(); byte[] buff = new byte[len]; dis.readFully(buff); return new Message(buff); } /** * Writes a message to a DataOutputStream. It might block !!! * Assumes stream open & running. * No parameter checkings performed. * @padram dos The DataOutputStream * @param msg Message to send * @return true if ok, false otherwise * @exception IOException If some error occured at the stream. */ public static boolean writeDataOutputStream(DataOutputStream dos, Message msg) throws IOException { dos.writeInt(msg.length()); byte[] buff = msg.getBytes(); dos.write(buff); /** blocking ... **/ //dos.flush(); return true; } /** * Returns a integer representing two bytes starting at the given index * interpreted lo, hi as a integer. * No parameter checkings performed. * @param index Start index, index of the hi byte */ public int getInt(int index) { return (data[index+1]*128 + data[index]); } /** * Sets two bytes of the message, to represent a integer value as lo, hi. * This is the opposite of .getInt(). * No parameter checkings performed. * @param index Index of first (hi) byte */ public void setInt(int index, int value) { data[index] = (byte)(value % 128); data[index+1] = (byte)(value / 128); } /** * Returns a character at a given index in the Message. * No checkings are performed on parameters. * @param index Index in the Message */ public char charAt(int index) { return (char)data[index]; } /** * Returns a byte at a given index in the Message. * No checkings are performed on parameters. * @param index Index in the Message */ public byte byteAt(int index) { return data[index]; } /** * Sets a byte at a given index in the Message, with a specified value. * No checkings are performed on parameters. * @param index Index in the Message * @param value Value to set */ public void byteAt(int index,int value) { data[index] = (byte)value; } /** * Gives the length of the Message. * @return The length of the Message */ public int length() { return data.length; } /** * Gives a byte reprezentation of the Message. * @return A byte array reprezenting the Message. */ public byte[] getBytes() { return data; } /** * Gives a String reprezenting the Message. * It just uses getBytes() to construct a ASCII String. */ public String getString() { return new String(data,0); } } /*eoc*/