public static void encodeMulticastRequest(MulticastRequest request,DatagramBufferFactory bufs) throws IOException {
  try {
    LinkedList groups=new LinkedList();
    groups.addAll(Arrays.asList(request.getGroups()));
    do {
      ByteBuffer buf=bufs.newBuffer();
      putUtf(buf,request.getHost());
      buf.putShort(intToUshort(request.getPort()));
      int ngroups=0;
      int ngroupsPos=buf.position();
      buf.putShort((short)0);
      if (!groups.isEmpty()) {
        putUtf(buf,(String)groups.removeFirst());
        ngroups++;
      }
      ServiceID[] ids=request.getServiceIDs();
      int maxIds=Math.min(ids.length,MAX_USHORT);
      int maxIdsLen=SHORT_LEN + maxIds * SERVICE_ID_LEN;
      if (buf.remaining() > maxIdsLen && !groups.isEmpty()) {
        int slim=buf.limit();
        buf.limit(slim - maxIdsLen);
        try {
          do {
            putUtf(buf,(String)groups.getFirst());
            groups.removeFirst();
            ngroups++;
          }
 while (!groups.isEmpty() && ngroups < MAX_USHORT);
        }
 catch (        BufferOverflowException e) {
        }
        buf.limit(slim);
      }
      buf.putShort(ngroupsPos,intToUshort(ngroups));
      int nids=Math.min(maxIds,(buf.remaining() - SHORT_LEN) / SERVICE_ID_LEN);
      buf.putShort(intToUshort(nids));
      for (int i=0; i < nids; i++) {
        ServiceID id=ids[i];
        buf.putLong(id.getMostSignificantBits());
        buf.putLong(id.getLeastSignificantBits());
      }
    }
 while (!groups.isEmpty());
  }
 catch (  RuntimeException e) {
    throw new DiscoveryProtocolException(null,e);
  }
}

public static void encodeMulticastRequest ( MulticastRequest request , DatagramBufferFactory bufs ) throws IOException { try { LinkedList groups = new LinkedList ( ) ; groups . addAll ( Arrays . asList ( request . getGroups ( ) ) ) ; do { ByteBuffer buf = bufs . newBuffer ( ) ; putUtf ( buf , request . getHost ( ) ) ; buf . putShort ( intToUshort ( request . getPort ( ) ) ) ; int ngroups = 0 ; int ngroupsPos = buf . position ( ) ; buf . putShort ( ( short ) 0 ) ; if ( ! groups . isEmpty ( ) ) { putUtf ( buf , ( String ) groups . removeFirst ( ) ) ; ngroups ++ ; } ServiceID [ ] ids = request . getServiceIDs ( ) ; int maxIds = Math . min ( ids . length , MAX_USHORT ) ; int maxIdsLen = SHORT_LEN + maxIds * SERVICE_ID_LEN ; if ( buf . remaining ( ) > maxIdsLen && ! groups . isEmpty ( ) ) { int slim = buf . limit ( ) ; buf . limit ( slim - maxIdsLen ) ; try { do { putUtf ( buf , ( String ) groups . getFirst ( ) ) ; groups . removeFirst ( ) ; ngroups ++ ; } while ( ! groups . isEmpty ( ) && ngroups < MAX_USHORT ) ; } catch ( BufferOverflowException e ) { } buf . limit ( slim ) ; } buf . putShort ( ngroupsPos , intToUshort ( ngroups ) ) ; int nids = Math . min ( maxIds , ( buf . remaining ( ) - SHORT_LEN ) / SERVICE_ID_LEN ) ; buf . putShort ( intToUshort ( nids ) ) ; for ( int i = 0 ; i < nids ; i ++ ) { ServiceID id = ids [ i ] ; buf . putLong ( id . getMostSignificantBits ( ) ) ; buf . putLong ( id . getLeastSignificantBits ( ) ) ; } } while ( ! groups . isEmpty ( ) ) ; } catch ( RuntimeException e ) { throw new DiscoveryProtocolException ( null , e ) ; } }
encodes multicast request according to the net . jini . discovery . plaintext format .
