@@ -802,17 +802,6 @@ private void checkWritable() throws EofException
802
802
803
803
@ Override
804
804
public void write (byte [] b , int off , int len ) throws IOException
805
- {
806
- write (b , off , len , null );
807
- }
808
-
809
- @ FunctionalInterface
810
- private interface IORunnable
811
- {
812
- void run () throws IOException ;
813
- }
814
-
815
- private void write (byte [] b , int off , int len , IORunnable onComplete ) throws IOException
816
805
{
817
806
if (LOG .isDebugEnabled ())
818
807
LOG .debug ("write(array {})" , BufferUtil .toDetailString (ByteBuffer .wrap (b , off , len )));
@@ -878,8 +867,6 @@ private void write(byte[] b, int off, int len, IORunnable onComplete) throws IOE
878
867
if (LOG .isDebugEnabled ())
879
868
LOG .debug ("write(array) {} aggregated !flush {}" ,
880
869
lockedStateString (), _aggregate );
881
- if (onComplete != null )
882
- onComplete .run ();
883
870
return ;
884
871
}
885
872
@@ -895,27 +882,8 @@ private void write(byte[] b, int off, int len, IORunnable onComplete) throws IOE
895
882
896
883
if (async )
897
884
{
898
- IORunnable finalOnComplete = onComplete ;
899
885
// Do the asynchronous writing from the callback
900
- new AsyncWrite (b , off , len , last )
901
- {
902
- @ Override
903
- protected void onCompleted (Throwable causeOrNull )
904
- {
905
- if (finalOnComplete != null )
906
- {
907
- try
908
- {
909
- finalOnComplete .run ();
910
- }
911
- catch (Throwable x )
912
- {
913
- causeOrNull = ExceptionUtil .combine (causeOrNull , x );
914
- }
915
- }
916
- super .onCompleted (causeOrNull );
917
- }
918
- }.iterate ();
886
+ new AsyncWrite (b , off , len , last ).iterate ();
919
887
return ;
920
888
}
921
889
@@ -936,12 +904,6 @@ protected void onCompleted(Throwable causeOrNull)
936
904
{
937
905
BufferUtil .append (byteBuffer , b , off , len );
938
906
onWriteComplete (false , null );
939
- if (onComplete != null )
940
- {
941
- IORunnable runOnComplete = onComplete ;
942
- onComplete = null ;
943
- runOnComplete .run ();
944
- }
945
907
return ;
946
908
}
947
909
}
@@ -971,18 +933,10 @@ else if (last && !complete)
971
933
}
972
934
973
935
onWriteComplete (last , null );
974
- if (onComplete != null )
975
- {
976
- IORunnable runOnComplete = onComplete ;
977
- onComplete = null ;
978
- runOnComplete .run ();
979
- }
980
936
}
981
937
catch (Throwable t )
982
938
{
983
939
onWriteComplete (last , t );
984
- if (onComplete != null )
985
- onComplete .run ();
986
940
throw t ;
987
941
}
988
942
}
@@ -1159,12 +1113,46 @@ private void print(String s, boolean eoln) throws IOException
1159
1113
throw new IOException ("Closed" );
1160
1114
1161
1115
s = String .valueOf (s );
1162
-
1163
1116
String charset = _servletChannel .getServletContextResponse ().getCharacterEncoding (false );
1164
-
1117
+ // String.getBytes() is much faster than a CharsetEncoder.encode() loop that would also need to estimate the
1118
+ // destination buffer size, so this compensates for the rare extra copy that needs to be done when the aggregation
1119
+ // buffer is full.
1165
1120
byte [] bytes = s .getBytes (charset );
1166
- IORunnable onComplete = eoln ? () -> write (IO .CRLF_BYTES ) : null ;
1167
- write (bytes , 0 , bytes .length , onComplete );
1121
+ if (!eoln )
1122
+ {
1123
+ write (bytes );
1124
+ }
1125
+ else
1126
+ {
1127
+ int len = bytes .length + IO .CRLF_BYTES .length ;
1128
+
1129
+ // If there is enough room left in the aggregation buffer, just fill it otherwise copy into a bigger byte array.
1130
+ boolean canAggregate = false ;
1131
+ try (AutoLock ignored = _channelState .lock ())
1132
+ {
1133
+ if (len <= _commitSize )
1134
+ {
1135
+ lockedAcquireBuffer ();
1136
+ if (len <= maximizeAggregateSpace ())
1137
+ {
1138
+ BufferUtil .fill (_aggregate .getByteBuffer (), bytes , 0 , bytes .length );
1139
+ canAggregate = true ;
1140
+ }
1141
+ }
1142
+ }
1143
+
1144
+ if (canAggregate )
1145
+ {
1146
+ write (IO .CRLF_BYTES );
1147
+ }
1148
+ else
1149
+ {
1150
+ byte [] bytesWithCrLf = new byte [len ];
1151
+ System .arraycopy (bytes , 0 , bytesWithCrLf , 0 , bytes .length );
1152
+ System .arraycopy (IO .CRLF_BYTES , 0 , bytesWithCrLf , bytes .length , IO .CRLF_BYTES .length );
1153
+ write (bytesWithCrLf );
1154
+ }
1155
+ }
1168
1156
}
1169
1157
1170
1158
/**
0 commit comments