Skip to content

Commit

Permalink
Dry Up XContent Parser Construction (#75114) (#75130)
Browse files Browse the repository at this point in the history
Cleanup duplication in how we parse byte arrrays directly.
  • Loading branch information
original-brownbear committed Jul 8, 2021
1 parent 54459eb commit 4e32a10
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -81,13 +82,14 @@ public XContentParser createParser(NamedXContentRegistry xContentRegistry,
@Override
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, byte[] data) throws IOException {
return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(data));
return createParser(xContentRegistry, deprecationHandler, data, 0, data.length);
}

@Override
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException {
return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(data, offset, length));
return new CborXContentParser(xContentRegistry, deprecationHandler,
cborFactory.createParser(new ByteArrayInputStream(data, offset, length)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -82,13 +83,14 @@ public XContentParser createParser(NamedXContentRegistry xContentRegistry,
@Override
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, byte[] data) throws IOException {
return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(data));
return createParser(xContentRegistry, deprecationHandler, data, 0, data.length);
}

@Override
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException {
return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(data, offset, length));
return new JsonXContentParser(xContentRegistry, deprecationHandler,
jsonFactory.createParser(new ByteArrayInputStream(data, offset, length)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -83,13 +84,14 @@ public XContentParser createParser(NamedXContentRegistry xContentRegistry,
@Override
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, byte[] data) throws IOException {
return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(data));
return createParser(xContentRegistry, deprecationHandler, data, 0, data.length);
}

@Override
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException {
return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(data, offset, length));
return new SmileXContentParser(xContentRegistry, deprecationHandler,
smileFactory.createParser(new ByteArrayInputStream(data, offset, length)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -76,13 +77,14 @@ public XContentParser createParser(NamedXContentRegistry xContentRegistry,
@Override
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, byte[] data) throws IOException {
return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(data));
return createParser(xContentRegistry, deprecationHandler, data, 0, data.length);
}

@Override
public XContentParser createParser(NamedXContentRegistry xContentRegistry,
DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException {
return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(data, offset, length));
return new YamlXContentParser(xContentRegistry, deprecationHandler,
yamlFactory.createParser(new ByteArrayInputStream(data, offset, length)));
}

@Override
Expand Down

0 comments on commit 4e32a10

Please sign in to comment.