public static void encodeMulticastAnnouncement(MulticastAnnouncement announcement,DatagramBufferFactory bufs) throws IOException {
  try {
    LinkedList groups=new LinkedList();
    groups.addAll(Arrays.asList(announcement.getGroups()));
    do {
      ByteBuffer buf=bufs.newBuffer();
      int slim=buf.limit();
      buf.limit(slim - SERVICE_ID_LEN);
      buf.putLong(announcement.getSequenceNumber());
      putUtf(buf,announcement.getHost());
      buf.putShort(intToUshort(announcement.getPort()));
      int ngroups=0;
      int ngroupsPos=buf.position();
      buf.putShort((short)0);
      try {
        while (!groups.isEmpty() && ngroups < MAX_USHORT) {
          putUtf(buf,(String)groups.getFirst());
          groups.removeFirst();
          ngroups++;
        }
      }
 catch (      BufferOverflowException e) {
        if (ngroups == 0) {
          throw e;
        }
      }
      buf.putShort(ngroupsPos,intToUshort(ngroups));
      ServiceID id=announcement.getServiceID();
      buf.limit(slim);
      buf.putLong(id.getMostSignificantBits());
      buf.putLong(id.getLeastSignificantBits());
    }
 while (!groups.isEmpty());
  }
 catch (  RuntimeException e) {
    throw new DiscoveryProtocolException(null,e);
  }
}

public static void encodeMulticastAnnouncement ( MulticastAnnouncement announcement , DatagramBufferFactory bufs ) throws IOException { try { LinkedList groups = new LinkedList ( ) ; groups . addAll ( Arrays . asList ( announcement . getGroups ( ) ) ) ; do { ByteBuffer buf = bufs . newBuffer ( ) ; int slim = buf . limit ( ) ; buf . limit ( slim - SERVICE_ID_LEN ) ; buf . putLong ( announcement . getSequenceNumber ( ) ) ; putUtf ( buf , announcement . getHost ( ) ) ; buf . putShort ( intToUshort ( announcement . getPort ( ) ) ) ; int ngroups = 0 ; int ngroupsPos = buf . position ( ) ; buf . putShort ( ( short ) 0 ) ; try { while ( ! groups . isEmpty ( ) && ngroups < MAX_USHORT ) { putUtf ( buf , ( String ) groups . getFirst ( ) ) ; groups . removeFirst ( ) ; ngroups ++ ; } } catch ( BufferOverflowException e ) { if ( ngroups == 0 ) { throw e ; } } buf . putShort ( ngroupsPos , intToUshort ( ngroups ) ) ; ServiceID id = announcement . getServiceID ( ) ; buf . limit ( slim ) ; buf . putLong ( id . getMostSignificantBits ( ) ) ; buf . putLong ( id . getLeastSignificantBits ( ) ) ; } while ( ! groups . isEmpty ( ) ) ; } catch ( RuntimeException e ) { throw new DiscoveryProtocolException ( null , e ) ; } }
encodes multicast announcement according to the net . jini . discovery . plaintext format .
