SocketUtils.java

  1. package emissary.util.io;

  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;

  5. public class SocketUtils {

  6.     public static final String RCS_ID = "$Id$";

  7.     public static void sendString(String str, DataOutputStream os) throws IOException {
  8.         os.writeInt(str.length());
  9.         os.write(str.getBytes(), 0, str.length());
  10.     }

  11.     public static String readString(DataInputStream is) throws IOException {
  12.         int contentSize = is.readInt();
  13.         byte[] theContent = new byte[contentSize];
  14.         is.readFully(theContent);
  15.         return new String(theContent);
  16.     }

  17.     public static void sendByteArray(byte[] bb, DataOutputStream os) throws IOException {
  18.         os.writeInt(bb.length);
  19.         os.write(bb, 0, bb.length);
  20.     }

  21.     public static byte[] readByteArray(DataInputStream theStream) throws IOException {
  22.         int contentSize = theStream.readInt();
  23.         byte[] theContent = new byte[contentSize];
  24.         theStream.readFully(theContent);
  25.         return theContent;
  26.     }

  27.     /** This class is not meant to be instantiated. */
  28.     private SocketUtils() {}
  29. }