Make TeaVMStorageUtils.read work instead of hanging

This commit is contained in:
Riccardo Azzolini 2019-08-09 17:41:30 +02:00
parent fef30042ce
commit e5b64ce218

View File

@ -11,12 +11,12 @@ import java.io.Reader;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.teavm.jso.browser.Window; import org.teavm.jso.browser.Window;
import it.cavallium.warppi.Platform.StorageUtils; import it.cavallium.warppi.Platform.StorageUtils;
@ -133,9 +133,21 @@ public class TeaVMStorageUtils implements StorageUtils {
return list; return list;
} }
// Reading an InputStream as a String is implemented this way
// because, on TeaVM, more convenient methods (such as IOUtils.toString) hang.
@Override @Override
public String read(final InputStream input) throws IOException { public String read(final InputStream input) throws IOException {
return IOUtils.toString(input, "UTF-8"); final int BUFFER_SIZE = 1024;
final StringBuilder builder = new StringBuilder();
try (final Reader reader = new InputStreamReader(input, StandardCharsets.UTF_8)) {
final char[] buffer = new char[BUFFER_SIZE];
int readCount;
while ((readCount = reader.read(buffer)) != -1) {
builder.append(buffer, 0, readCount);
}
}
return builder.toString();
} }
@Override @Override