@@ -1044,11 +1044,54 @@ void _benchmark () {
10441044 });
10451045 }
10461046
1047+ @ Test
1048+ void _benchmarkJDK () {
1049+ IntStream .range (0 , 2 ).forEach (__ ->{
1050+ long t = System .nanoTime ();
1051+ for (int loop = 1 ; loop < 500_000 ; loop ++){
1052+ val baos = new BAOS ();
1053+ var os = new DataOutputStream (baos );
1054+ val is = new BAIS (baos .array ());
1055+
1056+ try {
1057+ for (int i = 0 ; i < 133 ; i ++){
1058+ os .write (0xAB );
1059+ os .writeShort (0xCD_EF );
1060+ os .writeInt (0x1234_5678 );
1061+ os .writeLong (0x91929394_95969798L );
1062+ os .writeDouble (Math .PI );
1063+ os .writeLong (uuid .getMostSignificantBits ());
1064+ os .writeLong (uuid .getLeastSignificantBits ());
1065+ os .writeBytes ("Answer42!" );
1066+ }
1067+ } catch (IOException e ){
1068+ throw new AssertionError (e );
1069+ }
1070+ assertEquals (6384 , os .size ());
1071+
1072+ is .array (baos .array (), 0 , os .size ());
1073+
1074+ for (int i = 0 ; i < 133 ; i ++){
1075+ assertEquals (0xAB_CD_EF , is .readMedium ());
1076+ assertEquals (0x1234_5678 , is .readInt ());
1077+ assertEquals (0x91929394_95969798L , is .readLong ());
1078+ assertEquals (Math .PI , is .readDouble ());
1079+ assertEquals (uuid , is .readUUID ());
1080+ assertEquals ("Answer42!" , is .readLatin1String (9 ));
1081+ }
1082+ assertEquals (9222 , is .array ().length );
1083+ assertEquals (6384 , is .length ());
1084+ assertEquals (6384 , is .limit ());
1085+ assertEquals (6384 , is .position ());
1086+ assertEquals (6384 , is .readerIndex ());
1087+ }//f
1088+ System .out .println (benchToStr (t , System .nanoTime (), 500_000 ));
1089+ });
1090+ }
1091+
10471092 public static String benchToStr (long start , long end , long totalOperations ) {
10481093 assert start <= end : "start ≤ end, but " + start + " > " + end ;
1049-
10501094 long elapsed = end - start ;
1051-
10521095 return String .format (Locale .ENGLISH , "%.3f, op/s=%.2f" , elapsed /1000 /1000.0 , totalOperations * 1_000_000_000.0 / elapsed );
10531096 }
10541097
@@ -1126,4 +1169,19 @@ void _appendable () {
11261169 assertEquals (" test !" , is .readUTF16String (7 ));
11271170 assertEquals (s3 , is .readUTF16String (s3 .length ()));
11281171 }
1172+
1173+ @ Test
1174+ void _baos_as_StringBuilder () {
1175+ val w = new BAOS (5000 *2 );
1176+ val b = new StringBuilder (5000 );
1177+
1178+ for (int i = 0 ; i <=255 ; i ++){
1179+ w .append (Integer .toString (i )).append (Integer .toHexString (i )).append ('\n' );
1180+ b .append (i ).append (Integer .toHexString (i )).append ('\n' );
1181+ }
1182+
1183+ assertEquals (w .toString (UTF_16BE ), b .toString ());
1184+ assertArrayEquals (w .toByteArray (), b .toString ().getBytes (UTF_16BE ));
1185+ assertEquals (w .length ()/2 , b .length ());
1186+ }
11291187}
0 commit comments