2121
2222import org .elasticsearch .common .Strings ;
2323import org .elasticsearch .common .Table ;
24- import org .elasticsearch .common .io .UTF8StreamWriter ;
24+ import org .elasticsearch .common .io .Streams ;
2525import org .elasticsearch .common .io .stream .BytesStream ;
2626import org .elasticsearch .common .regex .Regex ;
2727import org .elasticsearch .common .unit .ByteSizeValue ;
3636import org .elasticsearch .xcontent .XContentType ;
3737
3838import java .io .IOException ;
39+ import java .io .OutputStreamWriter ;
40+ import java .nio .charset .StandardCharsets ;
3941import java .util .ArrayList ;
4042import java .util .LinkedHashSet ;
4143import java .util .List ;
@@ -57,11 +59,11 @@ public static RestResponse buildResponse(Table table, RestChannel channel) throw
5759 }
5860
5961 public static RestResponse response (RestChannel channel , String text ) throws IOException {
60- try ( UTF8StreamWriter out = new UTF8StreamWriter (); BytesStream bytesOut = channel .bytesOutput ()) {
61- out . setOutput (bytesOut );
62+ BytesStream bytesOut = Streams . flushOnCloseStream ( channel .bytesOutput ());
63+ try ( OutputStreamWriter out = new OutputStreamWriter (bytesOut , StandardCharsets . UTF_8 )) {
6264 out .append (text );
63- return new RestResponse (RestStatus .OK , RestResponse .TEXT_CONTENT_TYPE , bytesOut .bytes ());
6465 }
66+ return new RestResponse (RestStatus .OK , RestResponse .TEXT_CONTENT_TYPE , bytesOut .bytes ());
6567 }
6668
6769 public static RestResponse response (RestChannel channel , Map <String , Object > map ) throws IOException {
@@ -96,7 +98,8 @@ public static RestResponse buildTextPlainResponse(Table table, RestChannel chann
9698 List <DisplayHeader > headers = buildDisplayHeaders (table , request );
9799 int [] width = buildWidths (table , request , verbose , headers );
98100
99- try (BytesStream bytesOut = channel .bytesOutput (); UTF8StreamWriter out = new UTF8StreamWriter ().setOutput (bytesOut )) {
101+ BytesStream bytesOut = Streams .flushOnCloseStream (channel .bytesOutput ());
102+ try (OutputStreamWriter out = new OutputStreamWriter (bytesOut , StandardCharsets .UTF_8 )) {
100103 if (verbose ) {
101104 for (int col = 0 ; col < headers .size (); col ++) {
102105 DisplayHeader header = headers .get (col );
@@ -113,8 +116,8 @@ public static RestResponse buildTextPlainResponse(Table table, RestChannel chann
113116 }
114117 out .append ("\n " );
115118 }
116- return new RestResponse (RestStatus .OK , RestResponse .TEXT_CONTENT_TYPE , bytesOut .bytes ());
117119 }
120+ return new RestResponse (RestStatus .OK , RestResponse .TEXT_CONTENT_TYPE , bytesOut .bytes ());
118121 }
119122
120123 private static List <DisplayHeader > buildDisplayHeaders (Table table , RestRequest request ) {
@@ -258,7 +261,7 @@ private static int[] buildWidths(Table table, RestRequest request, boolean verbo
258261 return width ;
259262 }
260263
261- public static void pad (Table .Cell cell , int width , RestRequest request , UTF8StreamWriter out ) throws IOException {
264+ public static void pad (Table .Cell cell , int width , RestRequest request , OutputStreamWriter out ) throws IOException {
262265 String sValue = renderValue (request , cell .value );
263266 int length = sValue == null ? 0 : sValue .length ();
264267 byte leftOver = (byte ) (width - length );
0 commit comments