Skip to content

Commit 481219c

Browse files
committed
Use IOUtils in SmartLoader, make loadStream more concise.
1 parent b642542 commit 481219c

File tree

1 file changed

+2
-18
lines changed

1 file changed

+2
-18
lines changed

codepulse/src/main/scala/com/secdec/codepulse/util/SmartLoader.scala

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020
package com.secdec.codepulse.util
2121

2222
import java.io.ByteArrayInputStream
23-
import java.io.ByteArrayOutputStream
2423
import java.io.InputStream
2524

2625
import scala.io.Codec
27-
import scala.io.Codec.string2codec
2826
import scala.io.Source
2927

28+
import org.apache.commons.io.IOUtils
3029
import org.mozilla.universalchardet.UniversalDetector
3130

3231
/** Loader that detects the proper character set when loading the contents of a
@@ -64,22 +63,7 @@ class SmartLoader {
6463
* memory.
6564
*/
6665
def loadStream(stream: InputStream): String = {
67-
val bytes = {
68-
val bos = new ByteArrayOutputStream
69-
val buffer = new Array[Byte](BufferSize)
70-
71-
val chunks = Iterator.continually {
72-
stream.read(buffer, 0, buffer.length) -> buffer
73-
} takeWhile { _._1 > 0 }
74-
75-
for ((len, buffer) <- chunks) {
76-
bos.write(buffer, 0, len)
77-
}
78-
79-
bos.flush
80-
bos.toByteArray
81-
}
82-
66+
val bytes = IOUtils.toByteArray(stream)
8367
val charset = Option(detectCharset(new ByteArrayInputStream(bytes))).map[Codec](identity)
8468
Source.fromInputStream(new ByteArrayInputStream(bytes))(charset getOrElse Codec.UTF8).mkString
8569
}

0 commit comments

Comments
 (0)