21
21
22
22
import org .elasticsearch .common .Strings ;
23
23
import org .elasticsearch .common .Table ;
24
- import org .elasticsearch .common .io .UTF8StreamWriter ;
24
+ import org .elasticsearch .common .io .Streams ;
25
25
import org .elasticsearch .common .io .stream .BytesStream ;
26
26
import org .elasticsearch .common .regex .Regex ;
27
27
import org .elasticsearch .common .unit .ByteSizeValue ;
36
36
import org .elasticsearch .xcontent .XContentType ;
37
37
38
38
import java .io .IOException ;
39
+ import java .io .OutputStreamWriter ;
40
+ import java .nio .charset .StandardCharsets ;
39
41
import java .util .ArrayList ;
40
42
import java .util .LinkedHashSet ;
41
43
import java .util .List ;
@@ -57,11 +59,11 @@ public static RestResponse buildResponse(Table table, RestChannel channel) throw
57
59
}
58
60
59
61
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 )) {
62
64
out .append (text );
63
- return new RestResponse (RestStatus .OK , RestResponse .TEXT_CONTENT_TYPE , bytesOut .bytes ());
64
65
}
66
+ return new RestResponse (RestStatus .OK , RestResponse .TEXT_CONTENT_TYPE , bytesOut .bytes ());
65
67
}
66
68
67
69
public static RestResponse response (RestChannel channel , Map <String , Object > map ) throws IOException {
@@ -96,7 +98,8 @@ public static RestResponse buildTextPlainResponse(Table table, RestChannel chann
96
98
List <DisplayHeader > headers = buildDisplayHeaders (table , request );
97
99
int [] width = buildWidths (table , request , verbose , headers );
98
100
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 )) {
100
103
if (verbose ) {
101
104
for (int col = 0 ; col < headers .size (); col ++) {
102
105
DisplayHeader header = headers .get (col );
@@ -113,8 +116,8 @@ public static RestResponse buildTextPlainResponse(Table table, RestChannel chann
113
116
}
114
117
out .append ("\n " );
115
118
}
116
- return new RestResponse (RestStatus .OK , RestResponse .TEXT_CONTENT_TYPE , bytesOut .bytes ());
117
119
}
120
+ return new RestResponse (RestStatus .OK , RestResponse .TEXT_CONTENT_TYPE , bytesOut .bytes ());
118
121
}
119
122
120
123
private static List <DisplayHeader > buildDisplayHeaders (Table table , RestRequest request ) {
@@ -258,7 +261,7 @@ private static int[] buildWidths(Table table, RestRequest request, boolean verbo
258
261
return width ;
259
262
}
260
263
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 {
262
265
String sValue = renderValue (request , cell .value );
263
266
int length = sValue == null ? 0 : sValue .length ();
264
267
byte leftOver = (byte ) (width - length );
0 commit comments