3 Replies Latest reply on Sep 10, 2010 6:02 AM by timfox

    Issues with determining String size for encoding

    jmesnil

      Yesterday, clebert and I fixed https://jira.jboss.org/browse/HORNETQ-503 but there is a more general problem with determining String size to encode it in the buffer.


      Everywhere we use BufferHelper.sizeOfNullableSimpleString(String) or BufferHelper.sizeOfSimpleString(String) to determine the encoded size of a String, we may have issue is the String is not encoded as a SimpleString.

      In ChannelBufferWrapper.writeStringInternal(String), the way the String is encoded depends on its size. It can be encoded as shorts, as a SimpleString or as a UTF-8 String. Only when the String is encoded as a SimpleString, the size determined by BufferHelper.sizeOfSimpleString(String) would be correct. In all other cases, the size is not correct and could result in data loss.


      To fix this we need to change the sizeOfXXXString() to return the correct size depending on the String length and not assume that every String will be encoded as a SimpleString.

       

      After a quick look, this affect only persistence of JMS resources in the journal.

        • 1. Re: Issues with determining String size for encoding
          timfox

          Not sure why this is an issue.

           

          If you use ChannelBufferWrapper.writeString() and ChannelBufferWrapper.readString() it should handle all this for you.

           

          Can you be more specific about where this problem occurs?

          • 2. Re: Issues with determining String size for encoding
            jmesnil

            On second look, it is fine to use BufferHelper.sizeOfNullableSimpleString(String) to determine the size of the string *as long as we also use BufferHelper.writeAsSimpleString(Buffer, String) and not HornetQBuffer.writeString(String) directly*... not very intuitive...

             

            PersistedDestination was the only class where it was not the case (hence the issue fixed yesterday).

            • 3. Re: Issues with determining String size for encoding
              timfox

              Sounds pretty intuitive to me. If you encoded it as a SimpleString you can use sizeOfNullableSimpleString(), if you didn't encode it as a SimpleString you can't use sizeOfNullableSimpleString since it's... not encoded as a SimpleString.