From ff42341f8369479ebb4ac1c9a10b21941cf68fb5 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 11 Jan 2023 18:29:14 +0100 Subject: [PATCH] Simplify YangLibProvider Use a CharSource with UTF-8 encoding instead of manually constructing strings. Change-Id: Ib87a30321ccebb4c22920966fec86cf872ba2173 Signed-off-by: Robert Varga --- .../yanglib/impl/YangLibProvider.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/netconf/yanglib/src/main/java/org/opendaylight/yanglib/impl/YangLibProvider.java b/netconf/yanglib/src/main/java/org/opendaylight/yanglib/impl/YangLibProvider.java index f306467533..5235d7df51 100644 --- a/netconf/yanglib/src/main/java/org/opendaylight/yanglib/impl/YangLibProvider.java +++ b/netconf/yanglib/src/main/java/org/opendaylight/yanglib/impl/YangLibProvider.java @@ -13,13 +13,12 @@ import static java.util.Objects.requireNonNull; import com.google.common.base.Predicate; import com.google.common.base.Strings; import com.google.common.collect.Iterables; -import com.google.common.io.ByteStreams; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import java.io.File; import java.io.IOException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -180,12 +179,18 @@ public class YangLibProvider implements AutoCloseable, SchemaSourceListener, Yan final ListenableFuture sourceFuture = schemaRepository.getSchemaSource(sourceId, YangTextSchemaSource.class); + final YangTextSchemaSource source; try { - final YangTextSchemaSource source = sourceFuture.get(); - return new String(ByteStreams.toByteArray(source.openStream()), Charset.defaultCharset()); - } catch (InterruptedException | ExecutionException | IOException e) { + source = sourceFuture.get(); + } catch (InterruptedException | ExecutionException e) { throw new IllegalStateException("Unable to get schema " + sourceId, e); } + + try { + return source.asCharSource(StandardCharsets.UTF_8).read(); + } catch (IOException e) { + throw new IllegalStateException("Unable to read schema " + sourceId, e); + } } private Uri getUrlForModule(final SourceIdentifier sourceIdentifier) { -- 2.36.6