diff options
author | Simon Rettberg | 2019-09-13 16:02:52 +0200 |
---|---|---|
committer | Simon Rettberg | 2019-09-13 16:02:52 +0200 |
commit | 74e3323555ff9037bfbf6d245f966efb9919f266 (patch) | |
tree | 37c86e7a6960c0eb2ff347acb45a7a4c43155835 | |
parent | [DownloadText] Increase max size to 1M (diff) | |
download | tmlite-bwlp-74e3323555ff9037bfbf6d245f966efb9919f266.tar.gz tmlite-bwlp-74e3323555ff9037bfbf6d245f966efb9919f266.tar.xz tmlite-bwlp-74e3323555ff9037bfbf6d245f966efb9919f266.zip |
[DownloadText] Buffer everything before converting to UTF-8
As we read the incoming data in chunks, if a multi-byte UTF-8 char were
at such a chunk boundary, it would break during conversion, so buffer
everything first and convert later in one go.
-rw-r--r-- | src/main/java/org/openslx/taskmanager/tasks/DownloadText.java | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java b/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java index b62dc12..db0b126 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java +++ b/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java @@ -1,6 +1,7 @@ package org.openslx.taskmanager.tasks; import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URL; import java.net.URLConnection; @@ -49,19 +50,19 @@ public class DownloadText extends AbstractTask status.error = "Remote file too large: " + status.size + " bytes!"; return false; } - StringBuilder sb = new StringBuilder( Math.max( 8, status.size ) ); + final ByteArrayOutputStream baos = new ByteArrayOutputStream( Math.max( 512, status.size ) ); final byte data[] = new byte[ 9000 ]; int count; while ( ( count = in.read( data, 0, data.length ) ) != -1 ) { - sb.append( new String( data, 0, count, StandardCharsets.UTF_8 ) ); + baos.write( data, 0, count ); status.complete += count; if ( status.complete > MAX_SIZE ) { status.error = "Remote file too large: > " + status.complete + " bytes!"; return false; } } - status.content = sb.toString(); + status.content = new String( baos.toByteArray(), StandardCharsets.UTF_8 ); return true; } catch ( IOException e ) { status.error = "Download error: " + e.toString(); |