Simplify YangLibProvider 92/103992/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 11 Jan 2023 17:29:14 +0000 (18:29 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 11 Jan 2023 17:29:58 +0000 (18:29 +0100)
Use a CharSource with UTF-8 encoding instead of manually constructing
strings.

Change-Id: Ib87a30321ccebb4c22920966fec86cf872ba2173
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/yanglib/src/main/java/org/opendaylight/yanglib/impl/YangLibProvider.java

index f3064675331c8274a1c2b6002b495ef5876035a2..5235d7df51281a0a424020e446b274f0557c3d37 100644 (file)
@@ -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<YangTextSchemaSource> 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) {