-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#139: improves file encoding support
- Loading branch information
1 parent
a0ba8ed
commit f2d61f9
Showing
9 changed files
with
106 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package app.logorrr.io | ||
|
||
import java.nio.file.{Files, Path} | ||
|
||
object FEncoding { | ||
|
||
def apply(path: Path): FEncoding = { | ||
val is = Files.newInputStream(path) | ||
try { | ||
val bom = Array.fill[Byte](3)(0) | ||
is.read(bom) | ||
if (bom.startsWith(Array(0xFF.toByte, 0xFE.toByte))) { | ||
UTF16LE | ||
} else if (bom.startsWith(Array(0xFE.toByte, 0xFF.toByte))) { | ||
UTF16BE | ||
} else if (bom.startsWith(Array(0xEF.toByte, 0xBB.toByte, 0xBF.toByte))) { | ||
UTF8 | ||
} else { | ||
Unknown | ||
} | ||
} finally { | ||
is.close() | ||
} | ||
} | ||
} | ||
|
||
class FEncoding(val asString: String) | ||
|
||
case object UTF8 extends FEncoding("UTF-8") | ||
|
||
case object UTF16LE extends FEncoding("UTF-16LE") | ||
|
||
case object UTF16BE extends FEncoding("UTF-16BE") | ||
|
||
case object Unknown extends FEncoding("Unknown") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package app.logorrr.io | ||
|
||
import app.logorrr.util.CanLog | ||
|
||
import java.io.{BufferedReader, FileInputStream, InputStreamReader} | ||
import java.nio.file.{Files, Path} | ||
|
||
|
||
object FileManager extends CanLog { | ||
|
||
private def openFileWithDetectedEncoding(path: Path): BufferedReader = { | ||
val encoding = FEncoding(path) | ||
if (encoding == Unknown) { | ||
logWarn(encoding.asString + " encoding - fallback to UTF-8") | ||
new BufferedReader(new InputStreamReader(new FileInputStream(path.toFile), UTF8.asString)) | ||
} else { | ||
new BufferedReader(new InputStreamReader(new FileInputStream(path.toFile), encoding.asString)) | ||
} | ||
} | ||
|
||
def fromPath(path: Path): Seq[String] = { | ||
require(Files.exists(path)) | ||
val reader = openFileWithDetectedEncoding(path) | ||
try { | ||
(for (line <- Iterator.continually(reader.readLine()).takeWhile(_ != null)) yield line).toSeq | ||
} finally { | ||
reader.close() | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,28 @@ | ||
package app.logorrr.model | ||
|
||
import app.logorrr.OsxBridge | ||
import app.logorrr.io.FileManager | ||
import app.logorrr.util.{CanLog, OsUtil} | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.{Files, Path} | ||
import java.util | ||
import scala.util.{Failure, Success, Try} | ||
import java.nio.file.Path | ||
|
||
object LogFileReader extends CanLog { | ||
|
||
def readFromFile(logFile: Path): util.List[String] = { | ||
Try { | ||
if (OsUtil.isMac) { | ||
logInfo("Registering security bookmark for " + logFile.toAbsolutePath.toString) | ||
OsxBridge.registerPath(logFile.toAbsolutePath.toString) | ||
} | ||
|
||
val lines = Files.readAllLines(logFile) | ||
|
||
logEmptyLogFile(logFile, lines) | ||
lines | ||
} match { | ||
case Failure(exception) => | ||
val msg = s"Failed to read ${logFile.toAbsolutePath.toString}, exception: ${exception.getMessage}, retrying ISO_8859_1 ..." | ||
logException(msg, exception) | ||
Try { | ||
val lines = Files.readAllLines(logFile, StandardCharsets.ISO_8859_1) | ||
logEmptyLogFile(logFile, lines) | ||
lines | ||
} match { | ||
case Failure(exception) => | ||
val msg = s"Could not read file ${logFile.toAbsolutePath.toString} properly. Reason: ${exception.getMessage}." | ||
logException(msg, exception) | ||
util.Arrays.asList(msg) | ||
case Success(value) => | ||
value | ||
} | ||
case Success(lines) => lines | ||
def readFromFile(logFile: Path): Seq[String] = { | ||
if (OsUtil.isMac && !OsUtil.inTest) { | ||
logInfo("Registering security bookmark for " + logFile.toAbsolutePath.toString) | ||
OsxBridge.registerPath(logFile.toAbsolutePath.toString) | ||
} | ||
val lines = FileManager.fromPath(logFile) | ||
logEmptyLogFile(logFile, lines) | ||
lines | ||
} | ||
|
||
|
||
private def logEmptyLogFile(logFile: Path, lines: util.List[String]): Unit = { | ||
private def logEmptyLogFile(logFile: Path, lines: Seq[String]): Unit = { | ||
if (lines.isEmpty) { | ||
logWarn(s"${logFile.toAbsolutePath.toString} was empty.") | ||
} else { | ||
logTrace(s"${logFile.toAbsolutePath.toString} has ${lines.size()} lines.") | ||
logTrace(s"${logFile.toAbsolutePath.toString} has ${lines.size} lines.") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package app.logorrr | ||
|
||
import app.logorrr.io.FileManager | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import java.nio.file.{Files, Paths} | ||
|
||
|
||
|
||
class Issue139Spec extends AnyWordSpec { | ||
|
||
"Logfile" when { | ||
"encodedInUtf16" should { | ||
val p = Paths.get("src/test/resources/app/logorrr/issue-139.log") | ||
//val p = Paths.get("src/test/resources/app/logorrr/util/orig.log") | ||
"exist" in assert(Files.exists(p)) | ||
"can read file" in assert(FileManager.fromPath(p).nonEmpty) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters