Add `FileUtils.readAllLines()`

This commit is contained in:
wtfsck 2022-07-02 21:51:40 +02:00
parent 95914d09aa
commit 7947e244ed
10 changed files with 56 additions and 109 deletions

View File

@ -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<String> readAllLines(String filename) {
return readAllLines(filename, false);
}
public static ArrayList<String> readAllLines(String filename, boolean ignoreEmptyAndCommentLines) {
ArrayList<String> result = new ArrayList<String>();
List<String> 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;
}
}

View File

@ -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<String> 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<String> lines = FileUtils.readAllLines(filename);
int lineNo = 0;
for (String line : lines) {
lineNo++;

View File

@ -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<Integer> read(String name) {
String filename = PathUtils.getTestTextFilename("Decoder", name);
int lineNumber = 0;
List<String> 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<String> lines = FileUtils.readAllLines(filename);
HashSet<Integer> hash = new HashSet<Integer>();
int lineNumber = 0;
for (String line : lines) {
lineNumber++;
if (line.length() == 0 || line.charAt(0) == '#')

View File

@ -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<String> 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<String> lines = FileUtils.readAllLines(filename);
ArrayList<DecoderTestCase> result = new ArrayList<DecoderTestCase>(lines.size());
int lineNumber = 0;
for (String line : lines) {

View File

@ -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<String> 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<String> lines = FileUtils.readAllLines(filename);
ArrayList<DecoderMemoryTestCase> result = new ArrayList<DecoderMemoryTestCase>(lines.size());
int lineNumber = 0;
for (String line : lines) {

View File

@ -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<String> 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<String> lines = FileUtils.readAllLines(filename);
ArrayList<InstructionInfoTestCase> result = new ArrayList<InstructionInfoTestCase>(lines.size());
int lineNo = 0;
for (String line : lines) {

View File

@ -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<String> 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<String> lines = FileUtils.readAllLines(filename);
MemorySizeInfoTestCase[] result = new MemorySizeInfoTestCase[IcedConstants.MEMORY_SIZE_ENUM_COUNT];
int lineNo = 0;
for (String line : lines) {

View File

@ -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<String> 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<String> lines = FileUtils.readAllLines(filename);
ArrayList<OpCodeInfoTestCase> result = new ArrayList<OpCodeInfoTestCase>(lines.size());
int lineNo = 0;
for (String line : lines) {

View File

@ -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<String> 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<String> lines = FileUtils.readAllLines(filename);
RegisterInfoTestCase[] result = new RegisterInfoTestCase[IcedConstants.REGISTER_ENUM_COUNT];
int lineNo = 0;
for (String line : lines) {

View File

@ -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<String> 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<String> lines = FileUtils.readAllLines(filename);
ArrayList<VirtualAddressTestCase> result = new ArrayList<VirtualAddressTestCase>(lines.size());
int lineNo = 0;
for (String line : lines) {