Uploaded image for project: 'JGroups'
  1. JGroups
  2. JGRP-2122

Streamable class might implement SizeStreamable

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Done
    • Major
    • 4.0
    • None

    Description

      Many classes implement Streamable, but also provide a serializedSize() method which returns the number of bytes needed for the serialized form.

      Methods like Util.streamableToBuffer() could benefit from this: instead of allocating a 512 byte buffer, which is either too big or needs to be resized later, we can allocate a buffer of the exact size.

      We should therefore make all classes implementing Streamable instead implement SizeStreamable.

      Code that makes use of this:

      Util.streamableToBuffer()
      public static Buffer streamableToBuffer(Streamable obj) {
              int expected_size=obj instanceof SizeStreamable? 
                     ((SizeStreamable)obj).serializedSize() +1 : 512;
              final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(expected_size);
              try {
                  Util.writeStreamable(obj,out);
                  return out.getBuffer();
              }
              catch(Exception ex) {
                  return null;
              }
          }
      

      Attachments

        Activity

          People

            rhn-engineering-bban Bela Ban
            rhn-engineering-bban Bela Ban
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: