home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / pr8adpl7 / message.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  2.6 KB  |  135 lines

  1. // Message.java
  2. // A message for sending and receiving. Consists of headers (name and values)
  3. // and possibly data.
  4. import java.util.Vector;
  5. import CryptConnection;
  6. import java.io.IOException;
  7. import java.io.EOFException;
  8.  
  9. public class Message
  10. {
  11.     Vector names = new Vector();
  12.     Vector values = new Vector();
  13.     byte data[];
  14.  
  15.     // Construct and empty message
  16.     Message()
  17.     {
  18.     }
  19.  
  20.     // Construct a message from a connection
  21.     Message(LineInputStream c) throws IOException,EOFException
  22.     {
  23.     String line;
  24.     // Skip non-headers
  25.     while(true) {
  26.         line = c.gets();
  27.         int cln = line.indexOf(':'), spc = line.indexOf(' ');
  28.         if (cln != -1 && (spc == -1 || spc > cln))
  29.             break;    // this is valid
  30.         }
  31.  
  32.     // Read headers
  33.     do {
  34.         int cln = line.indexOf(':');
  35.         if (cln < 0 || cln == line.length()-1)
  36.             throw new IOException("Bogus header");
  37.         add(line.substring(0, cln), line.substring(cln+1).trim());
  38.         line = c.gets();
  39.         } while(line.length() > 0);
  40.  
  41.     // Read data (if there is any)
  42.     String cl = find("Content-length");
  43.     if (cl != null) {
  44.         data = new byte[Integer.parseInt(cl)];
  45.         c.readdata(data);
  46.         }
  47.     }
  48.  
  49.     // find
  50.     // Get the value for a named header, or null. 
  51.     public String find(String name)
  52.     {
  53.     for(int i=0; i<names.size(); i++)
  54.         if (((String)names.elementAt(i)).equalsIgnoreCase(name))
  55.             return (String)values.elementAt(i);
  56.     return null;
  57.     }
  58.  
  59.     // name
  60.     // Get the name of header i
  61.     public String name(int i)
  62.     {
  63.     return (String)names.elementAt(i);
  64.     }
  65.  
  66.     // value
  67.     // Get the value of header i
  68.     public String value(int i)
  69.     {
  70.     return (String)values.elementAt(i);
  71.     }
  72.  
  73.     // size
  74.     // Return the number of headers
  75.     public int size()
  76.     {
  77.     return names.size();
  78.     }
  79.  
  80.     // send
  81.     // Transmit the message
  82.     public void send(LineOutputStream c) 
  83.     {
  84.     try {
  85.         for(int i=0; i<names.size(); i++) {
  86.             String n = (String)names.elementAt(i);
  87.             String v = (String)values.elementAt(i);
  88.             c.puts(n+": "+v);
  89.             }
  90.         c.puts("");
  91.         if (data != null)
  92.             c.write(data);
  93.         }
  94.     catch(IOException e);
  95.     }
  96.  
  97.     // add
  98.     // Add a header-value pair, overwriting any existing pair with
  99.     // the same name.
  100.     public void add(String name, String val)
  101.     {
  102.     delete(name);
  103.     names.addElement(name);
  104.     values.addElement(val);
  105.     }
  106.  
  107.     // delete
  108.     // Remove the header with the given name (if it exists)
  109.     void delete(String h)
  110.     {
  111.     String v = find(h);
  112.     if (v != null) {
  113.         int idx = values.indexOf(v);
  114.         names.removeElementAt(idx);
  115.         values.removeElementAt(idx);
  116.         }
  117.     }
  118.  
  119.     // setdata
  120.     // Set the data to be sent
  121.     public void setdata(byte b[])
  122.     {
  123.     data = b;
  124.     if (b != null) add("Content-length",String.valueOf(b.length));
  125.     }
  126.  
  127.     // getdata
  128.     // Get the data received
  129.     public byte []getdata()
  130.     {
  131.     return data;
  132.     }
  133. }
  134.  
  135.