@@ -837,11 +837,32 @@ private void generateDataEncodeMethods(
837837 byteOrderStr ,
838838 indent );
839839
840- if (null = = characterEncoding )
840+ if (null ! = characterEncoding )
841841 {
842- return ;
842+ generateCharArrayEncodeMethods (
843+ sb ,
844+ propertyName ,
845+ sizeOfLengthField ,
846+ maxLengthValue ,
847+ lengthType ,
848+ byteOrderStr ,
849+ characterEncoding ,
850+ className ,
851+ indent );
843852 }
853+ }
844854
855+ private void generateCharArrayEncodeMethods (
856+ final StringBuilder sb ,
857+ final String propertyName ,
858+ final int sizeOfLengthField ,
859+ final int maxLengthValue ,
860+ final PrimitiveType lengthType ,
861+ final String byteOrderStr ,
862+ final String characterEncoding ,
863+ final String className ,
864+ final String indent )
865+ {
845866 if (characterEncoding .contains ("ASCII" ))
846867 {
847868 sb .append (String .format ("\n " +
@@ -864,6 +885,32 @@ private void generateDataEncodeMethods(
864885 maxLengthValue ,
865886 sizeOfLengthField ,
866887 generatePut (lengthType , "limit" , "length" , byteOrderStr )));
888+
889+ sb .append (String .format ("\n " +
890+ indent + " public %1$s %2$s(final CharSequence value)\n " +
891+ indent + " {\n " +
892+ indent + " final int length = value.length();\n " +
893+ indent + " if (length > %3$d)\n " +
894+ indent + " {\n " +
895+ indent + " throw new IllegalStateException(\" length > maxValue for type: \" + length);\n " +
896+ indent + " }\n \n " +
897+ indent + " final int headerLength = %4$d;\n " +
898+ indent + " final int limit = parentMessage.limit();\n " +
899+ indent + " parentMessage.limit(limit + headerLength + length);\n " +
900+ indent + " %5$s;\n " +
901+ indent + " for (int i = 0; i < length; ++i)\n " +
902+ indent + " {\n " +
903+ indent + " final char charValue = value.charAt(i);\n " +
904+ indent + " final byte byteValue = charValue > 127 ? (byte)'?' : (byte)charValue;\n " +
905+ indent + " buffer.putByte(limit + headerLength + i, byteValue);\n " +
906+ indent + " }\n \n " +
907+ indent + " return this;\n " +
908+ indent + " }\n " ,
909+ className ,
910+ formatPropertyName (propertyName ),
911+ maxLengthValue ,
912+ sizeOfLengthField ,
913+ generatePut (lengthType , "limit" , "length" , byteOrderStr )));
867914 }
868915 else
869916 {
@@ -1858,6 +1905,32 @@ private void generateCharArrayEncodeMethods(
18581905 propertyName ,
18591906 fieldLength ,
18601907 offset ));
1908+ sb .append (String .format ("\n " +
1909+ indent + " public %1$s %2$s(final CharSequence src)\n " +
1910+ indent + " {\n " +
1911+ indent + " final int length = %3$d;\n " +
1912+ indent + " final int srcLength = src.length();\n " +
1913+ indent + " if (srcLength > length)\n " +
1914+ indent + " {\n " +
1915+ indent + " throw new IndexOutOfBoundsException(" +
1916+ "\" CharSequence too large for copy: byte length=\" + srcLength);\n " +
1917+ indent + " }\n \n " +
1918+ indent + " for (int i = 0; i < srcLength; ++i)\n " +
1919+ indent + " {\n " +
1920+ indent + " final char charValue = src.charAt(i);\n " +
1921+ indent + " final byte byteValue = charValue > 127 ? (byte)'?' : (byte)charValue;\n " +
1922+ indent + " buffer.putByte(this.offset + %4$d + i, byteValue);\n " +
1923+ indent + " }\n \n " +
1924+ indent + " for (int i = srcLength; i < length; ++i)\n " +
1925+ indent + " {\n " +
1926+ indent + " buffer.putByte(this.offset + %4$d + i, (byte)0);\n " +
1927+ indent + " }\n \n " +
1928+ indent + " return this;\n " +
1929+ indent + " }\n " ,
1930+ formatClassName (containingClassName ),
1931+ propertyName ,
1932+ fieldLength ,
1933+ offset ));
18611934 }
18621935 else
18631936 {
0 commit comments