1818package org .apache .spark .util
1919
2020import java .io ._
21+ import java .nio .charset .Charset
2122
2223import scala .collection .mutable .HashSet
2324import scala .reflect ._
2425
25- import org .apache .commons .io .{FileUtils , IOUtils }
26- import org .apache .spark .{Logging , SparkConf }
2726import org .scalatest .{BeforeAndAfter , FunSuite }
27+
28+ import org .apache .spark .{Logging , SparkConf }
2829import org .apache .spark .util .logging .{RollingFileAppender , SizeBasedRollingPolicy , TimeBasedRollingPolicy , FileAppender }
2930
31+ import com .google .common .io .Files
32+
3033class FileAppenderSuite extends FunSuite with BeforeAndAfter with Logging {
3134
3235 val testFile = new File (" FileAppenderSuite-test-" + System .currentTimeMillis).getAbsoluteFile
@@ -41,11 +44,11 @@ class FileAppenderSuite extends FunSuite with BeforeAndAfter with Logging {
4144
4245 test(" basic file appender" ) {
4346 val testString = (1 to 1000 ).mkString(" , " )
44- val inputStream = IOUtils .toInputStream (testString)
47+ val inputStream = new ByteArrayInputStream (testString.getBytes( Charset .forName( " UTF-8 " )) )
4548 val appender = new FileAppender (inputStream, testFile)
4649 inputStream.close()
4750 appender.awaitTermination()
48- assert(FileUtils .readFileToString (testFile) === testString)
51+ assert(Files .toString (testFile, Charset .forName( " UTF-8 " ) ) === testString)
4952 }
5053
5154 test(" rolling file appender - time-based rolling" ) {
@@ -93,7 +96,7 @@ class FileAppenderSuite extends FunSuite with BeforeAndAfter with Logging {
9396 val allGeneratedFiles = new HashSet [String ]()
9497 val items = (1 to 10 ).map { _.toString * 10000 }
9598 for (i <- 0 until items.size) {
96- testOutputStream.write(items(i).getBytes(" UTF8 " ))
99+ testOutputStream.write(items(i).getBytes(Charset .forName( " UTF-8 " ) ))
97100 testOutputStream.flush()
98101 allGeneratedFiles ++= RollingFileAppender .getSortedRolledOverFiles(
99102 testFile.getParentFile.toString, testFile.getName).map(_.toString)
@@ -197,7 +200,7 @@ class FileAppenderSuite extends FunSuite with BeforeAndAfter with Logging {
197200 // send data to appender through the input stream, and wait for the data to be written
198201 val expectedText = textToAppend.mkString(" " )
199202 for (i <- 0 until textToAppend.size) {
200- outputStream.write(textToAppend(i).getBytes(" UTF8 " ))
203+ outputStream.write(textToAppend(i).getBytes(Charset .forName( " UTF-8 " ) ))
201204 outputStream.flush()
202205 Thread .sleep(sleepTimeBetweenTexts)
203206 }
@@ -212,7 +215,7 @@ class FileAppenderSuite extends FunSuite with BeforeAndAfter with Logging {
212215 logInfo(" Filtered files: \n " + generatedFiles.mkString(" \n " ))
213216 assert(generatedFiles.size > 1 )
214217 val allText = generatedFiles.map { file =>
215- FileUtils .readFileToString (file)
218+ Files .toString (file, Charset .forName( " UTF-8 " ) )
216219 }.mkString(" " )
217220 assert(allText === expectedText)
218221 generatedFiles
0 commit comments