Add IO bufferization

This commit is contained in:
Alexey Andreev 2017-06-01 13:03:26 +03:00
parent ad14176277
commit 2e8e3a65bd
12 changed files with 33 additions and 46 deletions

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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();

View File

@ -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<String, CLDRLocale> 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) {

View File

@ -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("\"");

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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");
}
}
}

View File

@ -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) {