home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch18 / multi / TextMessage.java < prev    next >
Encoding:
Java Source  |  1997-02-21  |  1.5 KB  |  38 lines

  1. // A text message in the multi-user system
  2.  
  3. // Written by Bernie Roehl, December 1996
  4.  
  5. package multi;
  6.  
  7. import java.io.*;
  8.  
  9. public class TextMessage {
  10.         short type;             // 0 for public, 1 for group, 2 for private
  11.         public final static int PUBLIC = 0, GROUP = 1, PRIVATE = 2;
  12.         int ssrc;              // synchronization source
  13.     String message;        // the actual message itself
  14.         
  15.         public short getType() { return type; }
  16.         public int getTextId() { return ssrc; }
  17.     public String getText() { return message; }
  18.  
  19.         public TextMessage(byte[] buffer) throws IOException {
  20.             ByteArrayInputStream bin = new ByteArrayInputStream(buffer);
  21.             DataInputStream in = new DataInputStream(bin);
  22.             in.readByte();   // version, padding, extension, CSRC count
  23.             if (in.readByte() != 80)  // payload type, marker is zero
  24.                 throw new IOException("bad text payload type");
  25.             in.readShort();  // sequence number
  26.             in.readInt();    // timestamp
  27.             ssrc = in.readInt();  // text SSRC
  28.             type = in.readShort();
  29.             int len = in.readShort();
  30. System.out.println("Length field claims" + len + " bytes");
  31.             byte[] textbuffer = new byte[len];
  32.             in.readFully(textbuffer);
  33.             message = new String(textbuffer, 0, 0, len);
  34. System.out.println("Actual text message has length of " + message.length());
  35.         }
  36. }
  37.  
  38.