RFR [9]8134424: BlockDataInputStream.readUTFBody: examine sizing local StringBuffer with the given length

Roger Riggs Roger.Riggs at Oracle.com
Thu Feb 11 16:42:25 UTC 2016


Thanks, looks good.

On 2/11/2016 11:28 AM, Chris Hegarty wrote:
> On 11 Feb 2016, at 15:59, Roger Riggs <Roger.Riggs at Oracle.com> wrote:
>
>> Hi Chris,
>>
>> ok, but can I suggest using 65535 instead of 16384, it will match the length
>> of the short encoded strings in writeObjectStream.writeString.
> That sounds reasonable. So the final change is:
>
> diff --git a/src/java.base/share/classes/java/io/ObjectInputStream.java b/src/java.base/share/classes/java/io/ObjectInputStream.java
> --- a/src/java.base/share/classes/java/io/ObjectInputStream.java
> +++ b/src/java.base/share/classes/java/io/ObjectInputStream.java
> @@ -3144,7 +3144,14 @@
>            * utflen bytes.
>            */
>           private String readUTFBody(long utflen) throws IOException {
> -            StringBuilder sbuf = new StringBuilder();
> +            StringBuilder sbuf ;
> +            if (utflen > 0 && utflen < Integer.MAX_VALUE) {
> +                // a reasonable initial capacity based on the UTF length
> +                int initialCapacity = Math.min((int)utflen, 65536);
> +                sbuf = new StringBuilder(initialCapacity);
> +            } else {
> +                sbuf = new StringBuilder();
> +            }
>               if (!blkmode) {
>                   end = pos = 0;
>               }
> -Chris.




More information about the core-libs-dev mailing list