* Attempts to convert a byte array to a UTF-8 string. * * @param {byteArray|Uint8Array} byteArray * @returns {string} * * @example * // returns "Hello" * Utils.byteArrayToUtf8([72,101,108,108,111]); * * // returns "你好" * Utils.byteArrayToUtf8([228,18
(byteArray)
| 629 | * Utils.byteArrayToUtf8([228,189,160,229,165,189]); |
| 630 | */ |
| 631 | static byteArrayToUtf8(byteArray) { |
| 632 | log.debug(`Converting byte array[${byteArray?.length}] to UTF8`); |
| 633 | if (!byteArray || !byteArray.length) return ""; |
| 634 | if (!(byteArray instanceof Uint8Array)) |
| 635 | byteArray = new Uint8Array(byteArray); |
| 636 | |
| 637 | try { |
| 638 | const str = new TextDecoder("utf-8", {fatal: true}).decode(byteArray); |
| 639 | |
| 640 | if (str.length !== byteArray.length) { |
| 641 | if (isWorkerEnvironment()) { |
| 642 | self.setOption("attemptHighlight", false); |
| 643 | } else if (isWebEnvironment()) { |
| 644 | window.app.options.attemptHighlight = false; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | return str; |
| 649 | } catch (err) { |
| 650 | // If it fails, treat it as ANSI |
| 651 | return Utils.byteArrayToChars(byteArray); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | |
| 656 | /** |
no test coverage detected