From 7947e244edb5e74ecb8bbeb9989ae78cbaf816c7 Mon Sep 17 00:00:00 2001 From: wtfsck Date: Sat, 2 Jul 2022 21:51:40 +0200 Subject: [PATCH] Add `FileUtils.readAllLines()` --- .../github/icedland/iced/x86/FileUtils.java | 34 +++++++++++++++++++ .../icedland/iced/x86/SectionFileReader.java | 14 ++------ .../iced/x86/dec/CodeValueReader.java | 17 +++------- .../iced/x86/dec/DecoderTestParser.java | 14 ++------ .../iced/x86/dec/MemoryDecoderTestParser.java | 14 ++------ .../x86/info/InstructionInfoTestReader.java | 14 ++------ .../x86/info/MemorySizeInfoTestReader.java | 15 ++------ .../x86/info/OpCodeInfoTestCasesReader.java | 14 ++------ .../iced/x86/info/RegisterInfoTestReader.java | 15 ++------ .../iced/x86/instr/VATestCaseReader.java | 14 ++------ 10 files changed, 56 insertions(+), 109 deletions(-) create mode 100644 src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/FileUtils.java diff --git a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/FileUtils.java b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/FileUtils.java new file mode 100644 index 000000000..7f9ca02e7 --- /dev/null +++ b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/FileUtils.java @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: MIT +// Copyright (C) 2018-present iced project and contributors + +package com.github.icedland.iced.x86; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +public final class FileUtils { + public static ArrayList readAllLines(String filename) { + return readAllLines(filename, false); + } + + public static ArrayList readAllLines(String filename, boolean ignoreEmptyAndCommentLines) { + ArrayList result = new ArrayList(); + List lines; + try { + lines = Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8); + } + catch (IOException ex) { + throw new UnsupportedOperationException(String.format("Couldn't read `%s`", filename)); + } + for (String line : lines) { + if (ignoreEmptyAndCommentLines && (line.length() == 0 || line.charAt(0) == '#')) + continue; + result.add(line); + } + return result; + } +} diff --git a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/SectionFileReader.java b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/SectionFileReader.java index f151597d2..8679c8f1c 100644 --- a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/SectionFileReader.java +++ b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/SectionFileReader.java @@ -3,23 +3,13 @@ package com.github.icedland.iced.x86; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.List; +import java.util.ArrayList; import java.util.function.BiConsumer; public final class SectionFileReader { public static void read(String filename, SectionInfo[] sectionInfos) { SectionInfo currentSectionInfo = null; - List lines; - try { - lines = Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8); - } - catch (IOException ex) { - throw new UnsupportedOperationException(String.format("Couldn't read `%s`", filename)); - } + ArrayList lines = FileUtils.readAllLines(filename); int lineNo = 0; for (String line : lines) { lineNo++; diff --git a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/dec/CodeValueReader.java b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/dec/CodeValueReader.java index 7586015a5..287f6ec68 100644 --- a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/dec/CodeValueReader.java +++ b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/dec/CodeValueReader.java @@ -3,29 +3,20 @@ package com.github.icedland.iced.x86.dec; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; +import java.util.ArrayList; import java.util.HashSet; -import java.util.List; import com.github.icedland.iced.x86.CodeUtils; +import com.github.icedland.iced.x86.FileUtils; import com.github.icedland.iced.x86.PathUtils; import com.github.icedland.iced.x86.ToCode; final class CodeValueReader { public static HashSet read(String name) { String filename = PathUtils.getTestTextFilename("Decoder", name); - int lineNumber = 0; - List lines; - try { - lines = Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8); - } - catch (IOException ex) { - throw new UnsupportedOperationException(String.format("Couldn't read `%s`", filename)); - } + ArrayList lines = FileUtils.readAllLines(filename); HashSet hash = new HashSet(); + int lineNumber = 0; for (String line : lines) { lineNumber++; if (line.length() == 0 || line.charAt(0) == '#') diff --git a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/dec/DecoderTestParser.java b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/dec/DecoderTestParser.java index 0c20f36e8..a23f60b24 100644 --- a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/dec/DecoderTestParser.java +++ b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/dec/DecoderTestParser.java @@ -3,17 +3,13 @@ package com.github.icedland.iced.x86.dec; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.ArrayList; -import java.util.List; import com.github.icedland.iced.x86.Code; import com.github.icedland.iced.x86.CodeUtils; import com.github.icedland.iced.x86.ConstantOffsets; import com.github.icedland.iced.x86.DecoderConstants; +import com.github.icedland.iced.x86.FileUtils; import com.github.icedland.iced.x86.HexUtils; import com.github.icedland.iced.x86.MvexRegMemConv; import com.github.icedland.iced.x86.NumberConverter; @@ -30,13 +26,7 @@ import com.github.icedland.iced.x86.ToRegister; final class DecoderTestParser { public static DecoderTestCase[] readFile(int bitness, String filename) { - List lines; - try { - lines = Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8); - } - catch (IOException ex) { - throw new UnsupportedOperationException(String.format("Couldn't read `%s`", filename)); - } + ArrayList lines = FileUtils.readAllLines(filename); ArrayList result = new ArrayList(lines.size()); int lineNumber = 0; for (String line : lines) { diff --git a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/dec/MemoryDecoderTestParser.java b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/dec/MemoryDecoderTestParser.java index 4aa3bab0c..824ea0b57 100644 --- a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/dec/MemoryDecoderTestParser.java +++ b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/dec/MemoryDecoderTestParser.java @@ -3,29 +3,19 @@ package com.github.icedland.iced.x86.dec; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.ArrayList; -import java.util.List; import com.github.icedland.iced.x86.CodeUtils; import com.github.icedland.iced.x86.ConstantOffsets; import com.github.icedland.iced.x86.DecoderConstants; +import com.github.icedland.iced.x86.FileUtils; import com.github.icedland.iced.x86.NumberConverter; import com.github.icedland.iced.x86.ToCode; import com.github.icedland.iced.x86.ToRegister; final class MemoryDecoderTestParser { public static DecoderMemoryTestCase[] readFile(int bitness, String filename) { - List lines; - try { - lines = Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8); - } - catch (IOException ex) { - throw new UnsupportedOperationException(String.format("Couldn't read `%s`", filename)); - } + ArrayList lines = FileUtils.readAllLines(filename); ArrayList result = new ArrayList(lines.size()); int lineNumber = 0; for (String line : lines) { diff --git a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/InstructionInfoTestReader.java b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/InstructionInfoTestReader.java index aa19dc089..57ba07747 100644 --- a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/InstructionInfoTestReader.java +++ b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/InstructionInfoTestReader.java @@ -3,19 +3,15 @@ package com.github.icedland.iced.x86.info; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Locale; import com.github.icedland.iced.x86.CodeSize; import com.github.icedland.iced.x86.CodeUtils; import com.github.icedland.iced.x86.DecoderConstants; import com.github.icedland.iced.x86.EncodingKind; +import com.github.icedland.iced.x86.FileUtils; import com.github.icedland.iced.x86.HexUtils; import com.github.icedland.iced.x86.NumberConverter; import com.github.icedland.iced.x86.PathUtils; @@ -55,13 +51,7 @@ final class InstructionInfoTestReader { toRegister.put(MiscInstrInfoTestConstants.VMM_PREFIX + Integer.toString(i), IcedConstants.VMM_FIRST + i); String filename = PathUtils.getTestTextFilename("InstructionInfo", String.format("InstructionInfoTest_%d.txt", bitness)); - List lines; - try { - lines = Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8); - } - catch (IOException ex) { - throw new UnsupportedOperationException(String.format("Couldn't read `%s`", filename)); - } + ArrayList lines = FileUtils.readAllLines(filename); ArrayList result = new ArrayList(lines.size()); int lineNo = 0; for (String line : lines) { diff --git a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/MemorySizeInfoTestReader.java b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/MemorySizeInfoTestReader.java index e0a00c68a..445cde59b 100644 --- a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/MemorySizeInfoTestReader.java +++ b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/MemorySizeInfoTestReader.java @@ -3,12 +3,9 @@ package com.github.icedland.iced.x86.info; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.List; +import java.util.ArrayList; +import com.github.icedland.iced.x86.FileUtils; import com.github.icedland.iced.x86.NumberConverter; import com.github.icedland.iced.x86.PathUtils; import com.github.icedland.iced.x86.ToMemorySize; @@ -17,13 +14,7 @@ import com.github.icedland.iced.x86.internal.IcedConstants; final class MemorySizeInfoTestReader { public static MemorySizeInfoTestCase[] getTestCases() { String filename = PathUtils.getTestTextFilename("InstructionInfo", "MemorySizeInfo.txt"); - List lines; - try { - lines = Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8); - } - catch (IOException ex) { - throw new UnsupportedOperationException(String.format("Couldn't read `%s`", filename)); - } + ArrayList lines = FileUtils.readAllLines(filename); MemorySizeInfoTestCase[] result = new MemorySizeInfoTestCase[IcedConstants.MEMORY_SIZE_ENUM_COUNT]; int lineNo = 0; for (String line : lines) { diff --git a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/OpCodeInfoTestCasesReader.java b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/OpCodeInfoTestCasesReader.java index 663a44c92..f0435ef33 100644 --- a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/OpCodeInfoTestCasesReader.java +++ b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/OpCodeInfoTestCasesReader.java @@ -3,15 +3,11 @@ package com.github.icedland.iced.x86.info; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.ArrayList; -import java.util.List; import com.github.icedland.iced.x86.CodeUtils; import com.github.icedland.iced.x86.EncodingKind; +import com.github.icedland.iced.x86.FileUtils; import com.github.icedland.iced.x86.MvexEHBit; import com.github.icedland.iced.x86.NumberConverter; import com.github.icedland.iced.x86.ToCode; @@ -26,13 +22,7 @@ import com.github.icedland.iced.x86.internal.IcedConstants; final class OpCodeInfoTestCasesReader { static OpCodeInfoTestCase[] readFile(String filename) { - List lines; - try { - lines = Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8); - } - catch (IOException ex) { - throw new UnsupportedOperationException(String.format("Couldn't read `%s`", filename)); - } + ArrayList lines = FileUtils.readAllLines(filename); ArrayList result = new ArrayList(lines.size()); int lineNo = 0; for (String line : lines) { diff --git a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/RegisterInfoTestReader.java b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/RegisterInfoTestReader.java index d703ea1d9..ed4f53da6 100644 --- a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/RegisterInfoTestReader.java +++ b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/info/RegisterInfoTestReader.java @@ -3,12 +3,9 @@ package com.github.icedland.iced.x86.info; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.List; +import java.util.ArrayList; +import com.github.icedland.iced.x86.FileUtils; import com.github.icedland.iced.x86.NumberConverter; import com.github.icedland.iced.x86.PathUtils; import com.github.icedland.iced.x86.ToRegister; @@ -17,13 +14,7 @@ import com.github.icedland.iced.x86.internal.IcedConstants; final class RegisterInfoTestReader { public static RegisterInfoTestCase[] getTestCases() { String filename = PathUtils.getTestTextFilename("InstructionInfo", "RegisterInfo.txt"); - List lines; - try { - lines = Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8); - } - catch (IOException ex) { - throw new UnsupportedOperationException(String.format("Couldn't read `%s`", filename)); - } + ArrayList lines = FileUtils.readAllLines(filename); RegisterInfoTestCase[] result = new RegisterInfoTestCase[IcedConstants.REGISTER_ENUM_COUNT]; int lineNo = 0; for (String line : lines) { diff --git a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/instr/VATestCaseReader.java b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/instr/VATestCaseReader.java index 04c562488..177ba03d6 100644 --- a/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/instr/VATestCaseReader.java +++ b/src/java/iced-x86/src/test/java/com/github/icedland/iced/x86/instr/VATestCaseReader.java @@ -3,14 +3,10 @@ package com.github.icedland.iced.x86.instr; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.ArrayList; -import java.util.List; import com.github.icedland.iced.x86.CodeUtils; +import com.github.icedland.iced.x86.FileUtils; import com.github.icedland.iced.x86.NumberConverter; import com.github.icedland.iced.x86.ToDecoderOptions; import com.github.icedland.iced.x86.ToRegister; @@ -18,13 +14,7 @@ import com.github.icedland.iced.x86.dec.DecoderOptions; final class VATestCaseReader { public static VirtualAddressTestCase[] readFile(String filename) { - List lines; - try { - lines = Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8); - } - catch (IOException ex) { - throw new UnsupportedOperationException(String.format("Couldn't read `%s`", filename)); - } + ArrayList lines = FileUtils.readAllLines(filename); ArrayList result = new ArrayList(lines.size()); int lineNo = 0; for (String line : lines) {