Skip to content

Commit 4e32a10

Browse files
Dry Up XContent Parser Construction (#75114) (#75130)
Cleanup duplication in how we parse byte arrrays directly.
1 parent 54459eb commit 4e32a10

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

libs/x-content/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.common.xcontent.XContentParser;
2222
import org.elasticsearch.common.xcontent.XContentType;
2323

24+
import java.io.ByteArrayInputStream;
2425
import java.io.IOException;
2526
import java.io.InputStream;
2627
import java.io.OutputStream;
@@ -81,13 +82,14 @@ public XContentParser createParser(NamedXContentRegistry xContentRegistry,
8182
@Override
8283
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
8384
DeprecationHandler deprecationHandler, byte[] data) throws IOException {
84-
return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(data));
85+
return createParser(xContentRegistry, deprecationHandler, data, 0, data.length);
8586
}
8687

8788
@Override
8889
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
8990
DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException {
90-
return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(data, offset, length));
91+
return new CborXContentParser(xContentRegistry, deprecationHandler,
92+
cborFactory.createParser(new ByteArrayInputStream(data, offset, length)));
9193
}
9294

9395
@Override

libs/x-content/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.elasticsearch.common.xcontent.XContentParser;
2121
import org.elasticsearch.common.xcontent.XContentType;
2222

23+
import java.io.ByteArrayInputStream;
2324
import java.io.IOException;
2425
import java.io.InputStream;
2526
import java.io.OutputStream;
@@ -82,13 +83,14 @@ public XContentParser createParser(NamedXContentRegistry xContentRegistry,
8283
@Override
8384
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
8485
DeprecationHandler deprecationHandler, byte[] data) throws IOException {
85-
return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(data));
86+
return createParser(xContentRegistry, deprecationHandler, data, 0, data.length);
8687
}
8788

8889
@Override
8990
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
9091
DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException {
91-
return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(data, offset, length));
92+
return new JsonXContentParser(xContentRegistry, deprecationHandler,
93+
jsonFactory.createParser(new ByteArrayInputStream(data, offset, length)));
9294
}
9395

9496
@Override

libs/x-content/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.common.xcontent.XContentParser;
2222
import org.elasticsearch.common.xcontent.XContentType;
2323

24+
import java.io.ByteArrayInputStream;
2425
import java.io.IOException;
2526
import java.io.InputStream;
2627
import java.io.OutputStream;
@@ -83,13 +84,14 @@ public XContentParser createParser(NamedXContentRegistry xContentRegistry,
8384
@Override
8485
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
8586
DeprecationHandler deprecationHandler, byte[] data) throws IOException {
86-
return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(data));
87+
return createParser(xContentRegistry, deprecationHandler, data, 0, data.length);
8788
}
8889

8990
@Override
9091
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
9192
DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException {
92-
return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(data, offset, length));
93+
return new SmileXContentParser(xContentRegistry, deprecationHandler,
94+
smileFactory.createParser(new ByteArrayInputStream(data, offset, length)));
9395
}
9496

9597
@Override

libs/x-content/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.common.xcontent.XContentParser;
2020
import org.elasticsearch.common.xcontent.XContentType;
2121

22+
import java.io.ByteArrayInputStream;
2223
import java.io.IOException;
2324
import java.io.InputStream;
2425
import java.io.OutputStream;
@@ -76,13 +77,14 @@ public XContentParser createParser(NamedXContentRegistry xContentRegistry,
7677
@Override
7778
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
7879
DeprecationHandler deprecationHandler, byte[] data) throws IOException {
79-
return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(data));
80+
return createParser(xContentRegistry, deprecationHandler, data, 0, data.length);
8081
}
8182

8283
@Override
8384
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
8485
DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException {
85-
return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(data, offset, length));
86+
return new YamlXContentParser(xContentRegistry, deprecationHandler,
87+
yamlFactory.createParser(new ByteArrayInputStream(data, offset, length)));
8688
}
8789

8890
@Override

0 commit comments

Comments
 (0)