From 2e8e3a65bdd396a2624295db8f96266b747247bf Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 1 Jun 2017 13:03:26 +0300 Subject: [PATCH] Add IO bufferization --- .../teavm/classlib/impl/currency/CountriesGenerator.java | 9 +++------ .../classlib/impl/currency/CurrenciesGenerator.java | 7 ++----- .../org/teavm/classlib/impl/tz/TimeZoneGenerator.java | 9 +++------ .../java/org/teavm/classlib/impl/unicode/CLDRReader.java | 9 +++------ .../classlib/java/lang/ClassLoaderNativeGenerator.java | 3 ++- .../src/main/java/org/teavm/model/ClassHolderSource.java | 4 ---- .../org/teavm/parsing/ClasspathResourceProvider.java | 8 +++----- .../teavm/parsing/resource/ClasspathResourceReader.java | 8 +++----- core/src/main/java/org/teavm/vm/TeaVMPluginLoader.java | 3 ++- .../metaprogramming/impl/MetaprogrammingClassLoader.java | 9 ++++++--- .../core/src/main/java/org/teavm/tooling/TeaVMTool.java | 7 ++++--- .../java/org/teavm/idea/jps/RenamingClassLoader.java | 3 ++- 12 files changed, 33 insertions(+), 46 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/impl/currency/CountriesGenerator.java b/classlib/src/main/java/org/teavm/classlib/impl/currency/CountriesGenerator.java index dcb712422..522c8cae1 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/currency/CountriesGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/currency/CountriesGenerator.java @@ -15,6 +15,7 @@ */ package org.teavm.classlib.impl.currency; +import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -24,15 +25,11 @@ import java.util.List; import org.teavm.model.MethodReference; import org.teavm.platform.metadata.*; -/** - * - * @author Alexey Andreev - */ public class CountriesGenerator implements MetadataGenerator { @Override public Resource generateMetadata(MetadataGeneratorContext context, MethodReference method) { - try (InputStream input = context.getClassLoader().getResourceAsStream( - "org/teavm/classlib/impl/currency/iso3166.csv")) { + try (InputStream input = new BufferedInputStream(context.getClassLoader().getResourceAsStream( + "org/teavm/classlib/impl/currency/iso3166.csv"))) { if (input == null) { throw new AssertionError("ISO 3166 table was not found"); } diff --git a/classlib/src/main/java/org/teavm/classlib/impl/currency/CurrenciesGenerator.java b/classlib/src/main/java/org/teavm/classlib/impl/currency/CurrenciesGenerator.java index f399d7181..e03c4fb62 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/currency/CurrenciesGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/currency/CurrenciesGenerator.java @@ -15,6 +15,7 @@ */ package org.teavm.classlib.impl.currency; +import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; @@ -29,10 +30,6 @@ import org.teavm.platform.metadata.ResourceArray; import org.w3c.dom.*; import org.xml.sax.SAXException; -/** - * - * @author Alexey Andreev - */ public class CurrenciesGenerator implements MetadataGenerator { @Override public Resource generateMetadata(MetadataGeneratorContext context, MethodReference method) { @@ -41,7 +38,7 @@ public class CurrenciesGenerator implements MetadataGenerator { "org/teavm/classlib/impl/currency/iso4217.xml")) { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); - doc = builder.parse(input); + doc = builder.parse(new BufferedInputStream(input)); } catch (IOException | ParserConfigurationException | SAXException e) { throw new RuntimeException("Error reading ISO 4217 medata from file"); } diff --git a/classlib/src/main/java/org/teavm/classlib/impl/tz/TimeZoneGenerator.java b/classlib/src/main/java/org/teavm/classlib/impl/tz/TimeZoneGenerator.java index 86e4d0d66..416b42184 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/tz/TimeZoneGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/tz/TimeZoneGenerator.java @@ -15,6 +15,7 @@ */ package org.teavm.classlib.impl.tz; +import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -27,17 +28,13 @@ import org.teavm.platform.metadata.MetadataGenerator; import org.teavm.platform.metadata.MetadataGeneratorContext; import org.teavm.platform.metadata.ResourceMap; -/** - * - * @author Alexey Andreev - */ public class TimeZoneGenerator implements MetadataGenerator { public static final String TIMEZONE_DB_VERSION = "2015d"; public static final String TIMEZONE_DB_PATH = "org/teavm/classlib/impl/tz/tzdata" + TIMEZONE_DB_VERSION + ".zip"; public static void compile(ZoneInfoCompiler compiler, ClassLoader classLoader) { try (InputStream input = classLoader.getResourceAsStream(TIMEZONE_DB_PATH)) { - try (ZipInputStream zip = new ZipInputStream(input)) { + try (ZipInputStream zip = new ZipInputStream(new BufferedInputStream(input))) { while (true) { ZipEntry entry = zip.getNextEntry(); if (entry == null) { @@ -76,7 +73,7 @@ public class TimeZoneGenerator implements MetadataGenerator { try (InputStream input = context.getClassLoader().getResourceAsStream("org/teavm/classlib/impl/tz/cache")) { if (input != null) { TimeZoneCache cache = new TimeZoneCache(); - zones = cache.read(input).values(); + zones = cache.read(new BufferedInputStream(input)).values(); } else { compile(compiler, context.getClassLoader()); zones = compiler.compile().values(); diff --git a/classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java b/classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java index 9c453341e..daa58847b 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/unicode/CLDRReader.java @@ -18,6 +18,7 @@ package org.teavm.classlib.impl.unicode; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -33,10 +34,6 @@ import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -/** - * - * @author Alexey Andreev - */ public class CLDRReader { private static String[] weekdayKeys = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" }; private Map knownLocales = new LinkedHashMap<>(); @@ -82,8 +79,8 @@ public class CLDRReader { } private void readCLDR(ClassLoader classLoader) { - try (ZipInputStream input = new ZipInputStream(classLoader.getResourceAsStream( - "org/teavm/classlib/impl/unicode/cldr-json.zip"))) { + try (ZipInputStream input = new ZipInputStream(new BufferedInputStream(classLoader.getResourceAsStream( + "org/teavm/classlib/impl/unicode/cldr-json.zip")))) { while (true) { ZipEntry entry = input.getNextEntry(); if (entry == null) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/ClassLoaderNativeGenerator.java b/classlib/src/main/java/org/teavm/classlib/java/lang/ClassLoaderNativeGenerator.java index ba5cf5f0e..b2c10caa4 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/ClassLoaderNativeGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/ClassLoaderNativeGenerator.java @@ -15,6 +15,7 @@ */ package org.teavm.classlib.java.lang; +import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; @@ -68,7 +69,7 @@ public class ClassLoaderNativeGenerator implements Injector { } first = false; writer.newLine(); - String data = Base64.getEncoder().encodeToString(IOUtils.toByteArray(input)); + String data = Base64.getEncoder().encodeToString(IOUtils.toByteArray(new BufferedInputStream(input))); writer.append("\"").append(RenderingUtil.escapeString(resource)).append("\""); writer.ws().append(':').ws(); writer.append("\"").append(data).append("\""); diff --git a/core/src/main/java/org/teavm/model/ClassHolderSource.java b/core/src/main/java/org/teavm/model/ClassHolderSource.java index 8274a3c1e..15c079c2b 100644 --- a/core/src/main/java/org/teavm/model/ClassHolderSource.java +++ b/core/src/main/java/org/teavm/model/ClassHolderSource.java @@ -17,10 +17,6 @@ package org.teavm.model; import java.util.stream.Stream; -/** - * - * @author Alexey Andreev - */ public interface ClassHolderSource extends ClassReaderSource { @Override ClassHolder get(String name); diff --git a/core/src/main/java/org/teavm/parsing/ClasspathResourceProvider.java b/core/src/main/java/org/teavm/parsing/ClasspathResourceProvider.java index b8b5f6d1a..480c07b41 100644 --- a/core/src/main/java/org/teavm/parsing/ClasspathResourceProvider.java +++ b/core/src/main/java/org/teavm/parsing/ClasspathResourceProvider.java @@ -15,12 +15,9 @@ */ package org.teavm.parsing; +import java.io.BufferedInputStream; import java.io.InputStream; -/** - * - * @author Alexey Andreev - */ public class ClasspathResourceProvider implements ResourceProvider { private ClassLoader classLoader; @@ -35,6 +32,7 @@ public class ClasspathResourceProvider implements ResourceProvider { @Override public InputStream openResource(String name) { - return classLoader.getResourceAsStream(name); + InputStream result = classLoader.getResourceAsStream(name); + return result != null ? new BufferedInputStream(result) : null; } } diff --git a/core/src/main/java/org/teavm/parsing/resource/ClasspathResourceReader.java b/core/src/main/java/org/teavm/parsing/resource/ClasspathResourceReader.java index ba20ffa45..4c065f275 100644 --- a/core/src/main/java/org/teavm/parsing/resource/ClasspathResourceReader.java +++ b/core/src/main/java/org/teavm/parsing/resource/ClasspathResourceReader.java @@ -15,13 +15,10 @@ */ package org.teavm.parsing.resource; +import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; -/** - * - * @author Alexey Andreev - */ public class ClasspathResourceReader implements ResourceReader { private ClassLoader classLoader; @@ -51,6 +48,7 @@ public class ClasspathResourceReader implements ResourceReader { @Override public InputStream openResource(String name) throws IOException { - return classLoader.getResourceAsStream(name); + InputStream result = classLoader.getResourceAsStream(name); + return result != null ? new BufferedInputStream(result) : null; } } diff --git a/core/src/main/java/org/teavm/vm/TeaVMPluginLoader.java b/core/src/main/java/org/teavm/vm/TeaVMPluginLoader.java index b4db270ed..16a9f5a1a 100644 --- a/core/src/main/java/org/teavm/vm/TeaVMPluginLoader.java +++ b/core/src/main/java/org/teavm/vm/TeaVMPluginLoader.java @@ -15,6 +15,7 @@ */ package org.teavm.vm; +import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -120,7 +121,7 @@ public final class TeaVMPluginLoader { if (input == null) { return false; } - ClassReader reader = new ClassReader(input); + ClassReader reader = new ClassReader(new BufferedInputStream(input)); PluginDescriptorFiller filler = new PluginDescriptorFiller(descriptor); reader.accept(filler, 0); return true; diff --git a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/MetaprogrammingClassLoader.java b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/MetaprogrammingClassLoader.java index 6b00fd20c..39162f340 100644 --- a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/MetaprogrammingClassLoader.java +++ b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/MetaprogrammingClassLoader.java @@ -15,6 +15,7 @@ */ package org.teavm.metaprogramming.impl; +import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; @@ -42,7 +43,7 @@ public class MetaprogrammingClassLoader extends ClassLoader { return super.loadClass(name, resolve); } else { try (InputStream input = getResourceAsStream(name.replace('.', '/') + ".class")) { - byte[] array = instrumentation.instrument(IOUtils.toByteArray(input)); + byte[] array = instrumentation.instrument(IOUtils.toByteArray(new BufferedInputStream(input))); return defineClass(name, array, 0, array.length); } catch (IOException e) { throw new ClassNotFoundException("Error reading bytecode of class " + name, e); @@ -84,7 +85,8 @@ public class MetaprogrammingClassLoader extends ClassLoader { if (input == null) { return false; } - new ClassReader(input).accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG); + new ClassReader(new BufferedInputStream(input)) + .accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG); } catch (IOException e) { return false; } @@ -109,7 +111,8 @@ public class MetaprogrammingClassLoader extends ClassLoader { if (input == null) { return false; } - new ClassReader(input).accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG); + new ClassReader(new BufferedInputStream(input)) + .accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG); } catch (IOException e) { return false; } diff --git a/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java b/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java index 953b980cc..ed1dde646 100644 --- a/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java +++ b/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java @@ -15,6 +15,7 @@ */ package org.teavm.tooling; +import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; @@ -558,8 +559,8 @@ public class TeaVMTool implements BaseTeaVMTool { private void resourceToFile(String resource, String fileName) throws IOException { try (InputStream input = TeaVMTool.class.getClassLoader().getResourceAsStream(resource)) { File outputFile = new File(targetDirectory, fileName); - try (OutputStream output = new FileOutputStream(outputFile)) { - IOUtils.copy(input, output); + try (OutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile))) { + IOUtils.copy(new BufferedInputStream(input), output); } generatedFiles.add(outputFile); } @@ -567,7 +568,7 @@ public class TeaVMTool implements BaseTeaVMTool { private void resourceToWriter(String resource, Writer writer) throws IOException { try (InputStream input = TeaVMTool.class.getClassLoader().getResourceAsStream(resource)) { - IOUtils.copy(input, writer, "UTF-8"); + IOUtils.copy(new BufferedInputStream(input), writer, "UTF-8"); } } } diff --git a/tools/idea/jps-plugin/src/main/java/org/teavm/idea/jps/RenamingClassLoader.java b/tools/idea/jps-plugin/src/main/java/org/teavm/idea/jps/RenamingClassLoader.java index b4931fb49..484229829 100644 --- a/tools/idea/jps-plugin/src/main/java/org/teavm/idea/jps/RenamingClassLoader.java +++ b/tools/idea/jps-plugin/src/main/java/org/teavm/idea/jps/RenamingClassLoader.java @@ -15,6 +15,7 @@ */ package org.teavm.idea.jps; +import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -44,7 +45,7 @@ public class RenamingClassLoader extends URLClassLoader { } try (InputStream input = getResourceAsStream(name.replace('.', '/') + ".class")) { - ClassReader classReader = new ClassReader(input); + ClassReader classReader = new ClassReader(new BufferedInputStream(input)); ClassWriter writer = new ClassWriter(0); RenamingVisitor visitor = new RenamingVisitor(writer); for (Rename rename : renameList) {