Skip to content

Commit

Permalink
use tryWithResource
Browse files Browse the repository at this point in the history
  • Loading branch information
panbingkun committed Oct 11, 2024
1 parent 088b614 commit 53d596c
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions common/utils/src/main/scala/org/apache/spark/util/JsonUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,11 @@ private[spark] trait JsonUtils {
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

def toJsonString(block: JsonGenerator => Unit): String = {
var baos: ByteArrayOutputStream = null
var generator: JsonGenerator = null
try {
baos = new ByteArrayOutputStream()
generator = mapper.createGenerator(baos, JsonEncoding.UTF8)
block(generator)
new String(baos.toByteArray, StandardCharsets.UTF_8)
} finally {
if (generator != null) {
generator.close()
}
if (baos != null) {
baos.close()
val baos: ByteArrayOutputStream = new ByteArrayOutputStream()
SparkErrorUtils.tryWithResource(mapper.createGenerator(baos, JsonEncoding.UTF8)) {
generator => {
block(generator)
new String(baos.toByteArray, StandardCharsets.UTF_8)
}
}
}
Expand Down

0 comments on commit 53d596c

Please sign in to comment.