@@ -126,26 +126,20 @@ private static byte[] convertDottedQuadToHex(byte[] ipUtf8, int offset, int leng
126126 if (quad == null ) {
127127 return null ;
128128 }
129- byte [] penultimate = toHexBytes (((quad [0 ] & 0xff ) << 8 ) | (quad [1 ] & 0xff ));
130- byte [] ultimate = toHexBytes (((quad [2 ] & 0xff ) << 8 ) | (quad [3 ] & 0xff ));
131- // initialPart + penultimate + ":" + ultimate
132- byte [] result = new byte [quadOffset + penultimate .length + 1 + ultimate .length ];
129+ // initialPart(quadOffset) + penultimate(4) + ":"(1) + ultimate(4)
130+ byte [] result = new byte [quadOffset + 9 ];
133131 System .arraycopy (ipUtf8 , offset , result , 0 , quadOffset );
134- System . arraycopy ( penultimate , 0 , result , quadOffset , penultimate . length );
135- result [quadOffset + penultimate . length ] = ':' ;
136- System . arraycopy ( ultimate , 0 , result , quadOffset + penultimate . length + 1 , ultimate . length );
132+ appendHexBytes ( result , quadOffset , quad [ 0 ], quad [ 1 ]); // penultimate part
133+ result [quadOffset + 4 ] = ':' ;
134+ appendHexBytes ( result , quadOffset + 5 , quad [ 2 ], quad [ 3 ]); // ultimate part
137135 return result ;
138136 }
139137
140- static byte [] toHexBytes (int val ) {
141- int mag = Integer .SIZE - Integer .numberOfLeadingZeros (val );
142- int length = Math .max (((mag + 3 ) / 4 ), 1 );
143- byte [] result = new byte [length ];
144- for (int i = length - 1 ; i >= 0 ; i --) {
145- result [i ] = (byte ) HEX_DIGITS [val & 0xf ];
146- val >>>= 4 ;
147- }
148- return result ;
138+ static void appendHexBytes (byte [] result , int offset , byte b1 , byte b2 ) {
139+ result [offset ] = (byte ) HEX_DIGITS [((b1 & 0xf0 ) >> 4 )];
140+ result [offset + 1 ] = (byte ) HEX_DIGITS [(b1 & 0x0f )];
141+ result [offset + 2 ] = (byte ) HEX_DIGITS [((b2 & 0xf0 ) >> 4 )];
142+ result [offset + 3 ] = (byte ) HEX_DIGITS [(b2 & 0x0f )];
149143 }
150144
151145 private static byte [] textToNumericFormatV4 (byte [] ipUtf8 , int offset , int length , boolean asIpv6 ) {
0 commit comments