Skip to content

Commit 8f91a6a

Browse files
committed
Wrap stream passed to createParser in try-with-resources (#28897)
* Wrap stream passed to createParser in try-with-resources This wraps the stream (`.streamInput()`) that is passed to many of the `createParser` instances in the enclosing (or a new) try-with-resources block. This ensures the `BytesReference.streamInput()` is closed. Relates to #28504 * Use try-with-resources instead of closing in a finally block
1 parent b066463 commit 8f91a6a

File tree

15 files changed

+86
-50
lines changed

15 files changed

+86
-50
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JsonProcessor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.elasticsearch.ingest.Processor;
3939

4040
import java.io.IOException;
41+
import java.io.InputStream;
4142
import java.util.Map;
4243

4344
import static org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException;
@@ -77,8 +78,9 @@ boolean isAddToRoot() {
7778
public void execute(IngestDocument document) throws Exception {
7879
Object fieldValue = document.getFieldValue(field, Object.class);
7980
BytesReference bytesRef = (fieldValue == null) ? new BytesArray("null") : new BytesArray(fieldValue.toString());
80-
try (XContentParser parser = JsonXContent.jsonXContent
81-
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytesRef.streamInput())) {
81+
try (InputStream stream = bytesRef.streamInput();
82+
XContentParser parser = JsonXContent.jsonXContent
83+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream)) {
8284
XContentParser.Token token = parser.nextToken();
8385
Object value = null;
8486
if (token == XContentParser.Token.VALUE_NULL) {

modules/reindex/src/main/java/org/elasticsearch/index/reindex/RestReindexAction.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.elasticsearch.script.Script;
4242

4343
import java.io.IOException;
44+
import java.io.InputStream;
4445
import java.util.List;
4546
import java.util.Map;
4647
import java.util.regex.Matcher;
@@ -74,8 +75,9 @@ public class RestReindexAction extends AbstractBaseReindexRestHandler<ReindexReq
7475
request.setRemoteInfo(buildRemoteInfo(source));
7576
XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType());
7677
builder.map(source);
77-
try (XContentParser innerParser = parser.contentType().xContent()
78-
.createParser(parser.getXContentRegistry(), parser.getDeprecationHandler(), builder.bytes().streamInput())) {
78+
try (InputStream stream = builder.bytes().streamInput();
79+
XContentParser innerParser = parser.contentType().xContent()
80+
.createParser(parser.getXContentRegistry(), parser.getDeprecationHandler(), stream)) {
7981
request.getSearchRequest().source().parseXContent(innerParser);
8082
}
8183
};

modules/reindex/src/main/java/org/elasticsearch/index/reindex/TransportReindexAction.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import org.elasticsearch.transport.TransportService;
7373

7474
import java.io.IOException;
75+
import java.io.InputStream;
7576
import java.io.UncheckedIOException;
7677
import java.util.ArrayList;
7778
import java.util.List;
@@ -338,9 +339,9 @@ protected RequestWrapper<IndexRequest> buildRequest(ScrollableHitSource.Hit doc)
338339
final XContentType mainRequestXContentType = mainRequest.getDestination().getContentType();
339340
if (mainRequestXContentType != null && doc.getXContentType() != mainRequestXContentType) {
340341
// we need to convert
341-
try (XContentParser parser = sourceXContentType.xContent()
342-
.createParser(NamedXContentRegistry.EMPTY,
343-
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, doc.getSource().streamInput());
342+
try (InputStream stream = doc.getSource().streamInput();
343+
XContentParser parser = sourceXContentType.xContent()
344+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream);
344345
XContentBuilder builder = XContentBuilder.builder(mainRequestXContentType.xContent())) {
345346
parser.nextToken();
346347
builder.copyCurrentStructure(parser);

server/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.elasticsearch.index.Index;
4141

4242
import java.io.IOException;
43+
import java.io.InputStream;
4344
import java.io.UncheckedIOException;
4445
import java.util.Arrays;
4546
import java.util.Map;
@@ -335,7 +336,9 @@ public void writeTo(StreamOutput out) throws IOException {
335336
@Override
336337
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
337338
if (source != null) {
338-
builder.rawValue(new BytesArray(source).streamInput(), XContentType.JSON);
339+
try (InputStream stream = new BytesArray(source).streamInput()) {
340+
builder.rawValue(stream, XContentType.JSON);
341+
}
339342
} else {
340343
builder.startObject().endObject();
341344
}

server/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
5050

5151
import java.io.IOException;
52+
import java.io.InputStream;
5253
import java.util.ArrayList;
5354
import java.util.HashSet;
5455
import java.util.List;
@@ -305,9 +306,9 @@ public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Null
305306

306307
// now parse the action
307308
// EMPTY is safe here because we never call namedObject
308-
try (XContentParser parser = xContent
309-
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE,
310-
data.slice(from, nextMarker - from).streamInput())) {
309+
try (InputStream stream = data.slice(from, nextMarker - from).streamInput();
310+
XContentParser parser = xContent
311+
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
311312
// move pointers
312313
from = nextMarker + 1;
313314

@@ -431,8 +432,9 @@ public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Null
431432
.routing(routing)
432433
.parent(parent);
433434
// EMPTY is safe here because we never call namedObject
434-
try (XContentParser sliceParser = xContent.createParser(NamedXContentRegistry.EMPTY,
435-
LoggingDeprecationHandler.INSTANCE, sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType).streamInput())) {
435+
try (InputStream dataStream = sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType).streamInput();
436+
XContentParser sliceParser = xContent.createParser(NamedXContentRegistry.EMPTY,
437+
LoggingDeprecationHandler.INSTANCE, dataStream)) {
436438
updateRequest.fromXContent(sliceParser);
437439
}
438440
if (fetchSourceContext != null) {

server/src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import java.io.ByteArrayOutputStream;
3838
import java.io.IOException;
39+
import java.io.InputStream;
3940
import java.util.ArrayList;
4041
import java.util.List;
4142
import java.util.Locale;
@@ -208,9 +209,8 @@ public static void readMultiLineFormat(BytesReference data,
208209
IndicesOptions defaultOptions = SearchRequest.DEFAULT_INDICES_OPTIONS;
209210
// now parse the action
210211
if (nextMarker - from > 0) {
211-
try (XContentParser parser = xContent
212-
.createParser(registry, LoggingDeprecationHandler.INSTANCE,
213-
data.slice(from, nextMarker - from).streamInput())) {
212+
try (InputStream stream = data.slice(from, nextMarker - from).streamInput();
213+
XContentParser parser = xContent.createParser(registry, LoggingDeprecationHandler.INSTANCE, stream)) {
214214
Map<String, Object> source = parser.map();
215215
for (Map.Entry<String, Object> entry : source.entrySet()) {
216216
Object value = entry.getValue();
@@ -246,7 +246,8 @@ public static void readMultiLineFormat(BytesReference data,
246246
break;
247247
}
248248
BytesReference bytes = data.slice(from, nextMarker - from);
249-
try (XContentParser parser = xContent.createParser(registry, LoggingDeprecationHandler.INSTANCE, bytes.streamInput())) {
249+
try (InputStream stream = bytes.streamInput();
250+
XContentParser parser = xContent.createParser(registry, LoggingDeprecationHandler.INSTANCE, stream)) {
250251
consumer.accept(searchRequest, parser);
251252
}
252253
// move pointers

server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsFields.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,21 @@ public final class TermVectorsFields extends Fields {
130130
* @param termVectors Stores the actual term vectors as a {@link BytesRef}.
131131
*/
132132
public TermVectorsFields(BytesReference headerRef, BytesReference termVectors) throws IOException {
133-
StreamInput header = headerRef.streamInput();
134-
fieldMap = new ObjectLongHashMap<>();
135-
// here we read the header to fill the field offset map
136-
String headerString = header.readString();
137-
assert headerString.equals("TV");
138-
int version = header.readInt();
139-
assert version == -1;
140-
hasTermStatistic = header.readBoolean();
141-
hasFieldStatistic = header.readBoolean();
142-
hasScores = header.readBoolean();
143-
final int numFields = header.readVInt();
144-
for (int i = 0; i < numFields; i++) {
145-
fieldMap.put((header.readString()), header.readVLong());
133+
try (StreamInput header = headerRef.streamInput()) {
134+
fieldMap = new ObjectLongHashMap<>();
135+
// here we read the header to fill the field offset map
136+
String headerString = header.readString();
137+
assert headerString.equals("TV");
138+
int version = header.readInt();
139+
assert version == -1;
140+
hasTermStatistic = header.readBoolean();
141+
hasFieldStatistic = header.readBoolean();
142+
hasScores = header.readBoolean();
143+
final int numFields = header.readVInt();
144+
for (int i = 0; i < numFields; i++) {
145+
fieldMap.put((header.readString()), header.readVLong());
146+
}
146147
}
147-
header.close();
148148
// reference to the term vector data
149149
this.termVectors = termVectors;
150150
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,15 +987,19 @@ public XContentBuilder rawField(String name, InputStream value, XContentType con
987987
*/
988988
@Deprecated
989989
public XContentBuilder rawField(String name, BytesReference value) throws IOException {
990-
generator.writeRawField(name, value.streamInput());
990+
try (InputStream stream = value.streamInput()) {
991+
generator.writeRawField(name, stream);
992+
}
991993
return this;
992994
}
993995

994996
/**
995997
* Writes a raw field with the given bytes as the value
996998
*/
997999
public XContentBuilder rawField(String name, BytesReference value, XContentType contentType) throws IOException {
998-
generator.writeRawField(name, value.streamInput(), contentType);
1000+
try (InputStream stream = value.streamInput()) {
1001+
generator.writeRawField(name, stream, contentType);
1002+
}
9991003
return this;
10001004
}
10011005

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ public static Tuple<XContentType, Map<String, Object>> convertToMap(BytesReferen
106106
input = bytes.streamInput();
107107
}
108108
contentType = xContentType != null ? xContentType : XContentFactory.xContentType(input);
109-
return new Tuple<>(Objects.requireNonNull(contentType), convertToMap(XContentFactory.xContent(contentType), input, ordered));
109+
try (InputStream stream = input) {
110+
return new Tuple<>(Objects.requireNonNull(contentType), convertToMap(XContentFactory.xContent(contentType), stream, ordered));
111+
}
110112
} catch (IOException e) {
111113
throw new ElasticsearchParseException("Failed to parse content to map", e);
112114
}
@@ -163,8 +165,9 @@ public static String convertToJson(BytesReference bytes, boolean reformatJson, b
163165
}
164166

165167
// It is safe to use EMPTY here because this never uses namedObject
166-
try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(NamedXContentRegistry.EMPTY,
167-
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput())) {
168+
try (InputStream stream = bytes.streamInput();
169+
XContentParser parser = XContentFactory.xContent(xContentType).createParser(NamedXContentRegistry.EMPTY,
170+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream)) {
168171
parser.nextToken();
169172
XContentBuilder builder = XContentFactory.jsonBuilder();
170173
if (prettyPrint) {
@@ -376,8 +379,9 @@ public static void copyCurrentEvent(XContentGenerator generator, XContentParser
376379
public static void writeRawField(String field, BytesReference source, XContentBuilder builder, ToXContent.Params params) throws IOException {
377380
Compressor compressor = CompressorFactory.compressor(source);
378381
if (compressor != null) {
379-
InputStream compressedStreamInput = compressor.streamInput(source.streamInput());
380-
builder.rawField(field, compressedStreamInput);
382+
try (InputStream compressedStreamInput = compressor.streamInput(source.streamInput())) {
383+
builder.rawField(field, compressedStreamInput);
384+
}
381385
} else {
382386
builder.rawField(field, source);
383387
}
@@ -392,8 +396,9 @@ public static void writeRawField(String field, BytesReference source, XContentTy
392396
Objects.requireNonNull(xContentType);
393397
Compressor compressor = CompressorFactory.compressor(source);
394398
if (compressor != null) {
395-
InputStream compressedStreamInput = compressor.streamInput(source.streamInput());
396-
builder.rawField(field, compressedStreamInput, xContentType);
399+
try (InputStream compressedStreamInput = compressor.streamInput(source.streamInput())) {
400+
builder.rawField(field, compressedStreamInput, xContentType);
401+
}
397402
} else {
398403
builder.rawField(field, source, xContentType);
399404
}

server/src/main/java/org/elasticsearch/index/query/functionscore/DecayFunctionBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.elasticsearch.search.MultiValueMode;
5454

5555
import java.io.IOException;
56+
import java.io.InputStream;
5657
import java.util.Objects;
5758

5859
public abstract class DecayFunctionBuilder<DFB extends DecayFunctionBuilder<DFB>> extends ScoreFunctionBuilder<DFB> {
@@ -182,8 +183,9 @@ protected int doHashCode() {
182183
protected ScoreFunction doToFunction(QueryShardContext context) throws IOException {
183184
AbstractDistanceScoreFunction scoreFunction;
184185
// EMPTY is safe because parseVariable doesn't use namedObject
185-
try (XContentParser parser = XContentFactory.xContent(functionBytes)
186-
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, functionBytes.streamInput())) {
186+
try (InputStream stream = functionBytes.streamInput();
187+
XContentParser parser = XContentFactory.xContent(functionBytes)
188+
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
187189
scoreFunction = parseVariable(fieldName, parser, context, multiValueMode);
188190
}
189191
return scoreFunction;

server/src/main/java/org/elasticsearch/rest/RestRequest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.elasticsearch.common.xcontent.XContentType;
3838

3939
import java.io.IOException;
40+
import java.io.InputStream;
4041
import java.net.SocketAddress;
4142
import java.util.Collections;
4243
import java.util.HashMap;
@@ -385,8 +386,9 @@ public final void withContentOrSourceParamParserOrNull(CheckedConsumer<XContentP
385386
Tuple<XContentType, BytesReference> tuple = contentOrSourceParam();
386387
BytesReference content = tuple.v2();
387388
XContentType xContentType = tuple.v1();
388-
try (XContentParser parser = xContentType.xContent()
389-
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, content.streamInput())) {
389+
try (InputStream stream = content.streamInput();
390+
XContentParser parser = xContentType.xContent()
391+
.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream)) {
390392
withParser.accept(parser);
391393
}
392394
} else {

server/src/main/java/org/elasticsearch/rest/action/document/RestGetSourceAction.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
3737

3838
import java.io.IOException;
39+
import java.io.InputStream;
3940

4041
import static org.elasticsearch.rest.RestRequest.Method.GET;
4142
import static org.elasticsearch.rest.RestRequest.Method.HEAD;
@@ -84,7 +85,9 @@ public RestResponse buildResponse(final GetResponse response) throws Exception {
8485
return new BytesRestResponse(NOT_FOUND, builder);
8586
} else {
8687
final BytesReference source = response.getSourceInternal();
87-
builder.rawValue(source.streamInput(), XContentFactory.xContentType(source));
88+
try (InputStream stream = source.streamInput()) {
89+
builder.rawValue(stream, XContentFactory.xContentType(source));
90+
}
8891
return new BytesRestResponse(OK, builder);
8992
}
9093
}

server/src/main/java/org/elasticsearch/script/Script.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.elasticsearch.common.xcontent.json.JsonXContent;
4141

4242
import java.io.IOException;
43+
import java.io.InputStream;
4344
import java.io.UncheckedIOException;
4445
import java.util.Collections;
4546
import java.util.HashMap;
@@ -282,8 +283,11 @@ public static Script parse(Settings settings) {
282283
builder.startObject();
283284
settings.toXContent(builder, ToXContent.EMPTY_PARAMS);
284285
builder.endObject();
285-
return parse(JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
286-
LoggingDeprecationHandler.INSTANCE, builder.bytes().streamInput()));
286+
try (InputStream stream = builder.bytes().streamInput();
287+
XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
288+
LoggingDeprecationHandler.INSTANCE, stream)) {
289+
return parse(parser);
290+
}
287291
} catch (IOException e) {
288292
// it should not happen since we are not actually reading from a stream but an in-memory byte[]
289293
throw new IllegalStateException(e);

server/src/main/java/org/elasticsearch/script/StoredScriptSource.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.elasticsearch.common.xcontent.XContentType;
4444

4545
import java.io.IOException;
46+
import java.io.InputStream;
4647
import java.io.UncheckedIOException;
4748
import java.util.Collections;
4849
import java.util.HashMap;
@@ -243,8 +244,9 @@ private StoredScriptSource build() {
243244
* @return The parsed {@link StoredScriptSource}.
244245
*/
245246
public static StoredScriptSource parse(BytesReference content, XContentType xContentType) {
246-
try (XContentParser parser = xContentType.xContent()
247-
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, content.streamInput())) {
247+
try (InputStream stream = content.streamInput();
248+
XContentParser parser = xContentType.xContent()
249+
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
248250
Token token = parser.nextToken();
249251

250252
if (token != Token.START_OBJECT) {

server/src/main/java/org/elasticsearch/tasks/RawTaskStatus.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.common.xcontent.XContentFactory;
2828

2929
import java.io.IOException;
30+
import java.io.InputStream;
3031
import java.util.Map;
3132

3233
import static java.util.Objects.requireNonNull;
@@ -58,7 +59,9 @@ public void writeTo(StreamOutput out) throws IOException {
5859

5960
@Override
6061
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
61-
return builder.rawValue(status.streamInput(), XContentFactory.xContentType(status));
62+
try (InputStream stream = status.streamInput()) {
63+
return builder.rawValue(stream, XContentFactory.xContentType(status));
64+
}
6265
}
6366

6467
@Override

0 commit comments

Comments
 (0)