Skip to content

Commit

Permalink
AVRO-1715. Java: Close files opened by the Schema parser. Contributed…
Browse files Browse the repository at this point in the history
… by Pavel Safrata.

This also clarifies the InputStream parse method, which does not own the
stream that is passed in and does not close it. The tools have been
updated to correctly close streams passed to the parser.

git-svn-id: https://svn.apache.org/repos/asf/avro/trunk@1707389 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
rdblue committed Oct 7, 2015
1 parent 0f5758b commit 4707d9a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ Avro 1.8.0 (10 August 2014)
AVRO-1700. C++: Fix avro_BufferStreambuf_hh__ header guard.
(Liu Yanbo via blue)

AVRO-1715. Java: Close files opened by the Schema parser.
(Pavel Safrata via blue)

Avro 1.7.7 (23 July 2014)

NEW FEATURES
Expand Down
7 changes: 5 additions & 2 deletions lang/java/avro/src/main/java/org/apache/avro/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -997,9 +997,11 @@ public Schema parse(File file) throws IOException {
}

/** Parse a schema from the provided stream.
* If named, the schema is added to the names known to this parser. */
* If named, the schema is added to the names known to this parser.
* The input stream stays open after the parsing. */
public Schema parse(InputStream in) throws IOException {
return parse(FACTORY.createJsonParser(in));
return parse(FACTORY.createJsonParser(in).disable(
JsonParser.Feature.AUTO_CLOSE_SOURCE));
}

/** Read a schema from one or more json strings */
Expand Down Expand Up @@ -1030,6 +1032,7 @@ private Schema parse(JsonParser parser) throws IOException {
} catch (JsonParseException e) {
throw new SchemaParseException(e);
} finally {
parser.close();
validateNames.set(saved);
VALIDATE_DEFAULTS.set(savedValidateDefaults);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public int run(InputStream stdin, PrintStream out, PrintStream err,
schema = new Schema.Parser().parse(nargs.get(0));
inputFile = nargs.get(1);
} else {
schema = new Schema.Parser().parse(Util.openFromFS(schemaFile));
schema = Util.parseSchemaFromFS(schemaFile);
inputFile = nargs.get(0);
}
InputStream input = Util.fileOrStdin(inputFile, stdin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public int run(InputStream stdin, PrintStream out, PrintStream err,
return 1;
}
Schema schema = (schemafile != null)
? new Schema.Parser().parse(Util.openFromFS(schemafile))
? Util.parseSchemaFromFS(schemafile)
: new Schema.Parser().parse(schemastr);

DataFileWriter<Object> writer =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public int run(InputStream stdin, PrintStream out, PrintStream err,
return 1;
}
Schema schema = (schemafile != null)
? new Schema.Parser().parse(Util.openFromFS(schemafile))
? Util.parseSchemaFromFS(schemafile)
: new Schema.Parser().parse(schemastr);

DatumReader<Object> reader = new GenericDatumReader<Object>(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public int run(InputStream stdin, PrintStream out, PrintStream err,
schema = new Schema.Parser().parse(nargs.get(0));
inputFile = nargs.get(1);
} else {
schema = new Schema.Parser().parse(Util.openFromFS(schemaFile));
schema = Util.parseSchemaFromFS(schemaFile);
inputFile = nargs.get(0);
}
InputStream input = Util.fileOrStdin(inputFile, stdin);
Expand Down
15 changes: 15 additions & 0 deletions lang/java/tools/src/main/java/org/apache/avro/tool/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@ static void close(OutputStream out) {
}
}

/**
* Parses a schema from the specified file.
* @param filename The file name to parse
* @return The parsed schema
* @throws IOException
*/
static Schema parseSchemaFromFS(String filename) throws IOException {
InputStream stream = openFromFS(filename);
try {
return new Schema.Parser().parse(stream);
} finally {
close(stream);
}
}

/**If pathname is a file, this method returns a list with a single absolute Path to that file,
* if pathname is a directory, this method returns a list of Pathes to all the files within
* this directory.
Expand Down

0 comments on commit 4707d9a

Please sign in to comment.