Skip to content

Commit 9ab5ded

Browse files
authored
Switch XContentBuilder from BytesStreamOutput to ByteArrayOutputStream (elastic#28945)
This switches the underlying byte output representation used by default in `XContentBuilder` from `BytesStreamOutput` to a `ByteArrayOutputStream` (an `OutputStream` can still be specified manually) This is groundwork to allow us to decouple `XContent*` from the rest of the ES core code so that it may be factored into a separate jar. Since `BytesStreamOutput` was not using the recycling instance of `BigArrays`, this should not affect the circuit breaking capabilities elsewhere in the system. Relates to elastic#28504
1 parent 9683282 commit 9ab5ded

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

server/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
package org.elasticsearch.common.xcontent;
2121

2222
import org.apache.lucene.util.BytesRef;
23+
import org.elasticsearch.common.bytes.BytesArray;
2324
import org.elasticsearch.common.bytes.BytesReference;
2425
import org.elasticsearch.common.geo.GeoPoint;
2526
import org.elasticsearch.common.io.stream.BytesStream;
26-
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2727
import org.elasticsearch.common.lease.Releasable;
2828
import org.elasticsearch.common.text.Text;
2929
import org.elasticsearch.common.unit.ByteSizeValue;
@@ -34,6 +34,7 @@
3434
import org.joda.time.format.DateTimeFormatter;
3535
import org.joda.time.format.ISODateTimeFormat;
3636

37+
import java.io.ByteArrayOutputStream;
3738
import java.io.Flushable;
3839
import java.io.IOException;
3940
import java.io.InputStream;
@@ -58,21 +59,21 @@ public final class XContentBuilder implements Releasable, Flushable {
5859
/**
5960
* Create a new {@link XContentBuilder} using the given {@link XContent} content.
6061
* <p>
61-
* The builder uses an internal {@link BytesStreamOutput} output stream to build the content.
62+
* The builder uses an internal {@link ByteArrayOutputStream} output stream to build the content.
6263
* </p>
6364
*
6465
* @param xContent the {@link XContent}
6566
* @return a new {@link XContentBuilder}
6667
* @throws IOException if an {@link IOException} occurs while building the content
6768
*/
6869
public static XContentBuilder builder(XContent xContent) throws IOException {
69-
return new XContentBuilder(xContent, new BytesStreamOutput());
70+
return new XContentBuilder(xContent, new ByteArrayOutputStream());
7071
}
7172

7273
/**
7374
* Create a new {@link XContentBuilder} using the given {@link XContent} content and some inclusive and/or exclusive filters.
7475
* <p>
75-
* The builder uses an internal {@link BytesStreamOutput} output stream to build the content. When both exclusive and
76+
* The builder uses an internal {@link ByteArrayOutputStream} output stream to build the content. When both exclusive and
7677
* inclusive filters are provided, the underlying builder will first use exclusion filters to remove fields and then will check the
7778
* remaining fields against the inclusive filters.
7879
* <p>
@@ -83,7 +84,7 @@ public static XContentBuilder builder(XContent xContent) throws IOException {
8384
* @throws IOException if an {@link IOException} occurs while building the content
8485
*/
8586
public static XContentBuilder builder(XContent xContent, Set<String> includes, Set<String> excludes) throws IOException {
86-
return new XContentBuilder(xContent, new BytesStreamOutput(), includes, excludes);
87+
return new XContentBuilder(xContent, new ByteArrayOutputStream(), includes, excludes);
8788
}
8889

8990
public static final DateTimeFormatter DEFAULT_DATE_PRINTER = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);
@@ -1036,7 +1037,11 @@ public XContentGenerator generator() {
10361037

10371038
public BytesReference bytes() {
10381039
close();
1039-
return ((BytesStream) bos).bytes();
1040+
if (bos instanceof ByteArrayOutputStream) {
1041+
return new BytesArray(((ByteArrayOutputStream) bos).toByteArray());
1042+
} else {
1043+
return ((BytesStream) bos).bytes();
1044+
}
10401045
}
10411046

10421047
/**

0 commit comments

Comments
 (0)