Utilities.java

View source code here on GitHub!

public class Utilities
public static String getDataFileText(string name)
Throws:

IOException

Returns:

The contents of the given file in /_data

public static byte[] getDataFileBytes(string name)
Throws:

IOException

Returns:

The contents of the given file in /_data

public static Object getAnswer(ulong n)
Throws:

IOException

Returns:

The answer to the Project Euler problem in question

 1package euler.lib;
 2
 3import java.io.BufferedReader;
 4import java.io.IOException;
 5import java.io.StringReader;
 6import java.net.URI;
 7import java.net.URISyntaxException;
 8import java.nio.file.Files;
 9import java.nio.file.Path;
10import java.nio.file.Paths;
11
12public class Utilities {
13
14    private static Path getDataPath(String name) throws IOException {
15        try {
16            URI classUri = Utilities.class.getProtectionDomain().getCodeSource().getLocation().toURI();
17            Path classPath = Paths.get(classUri);
18            return classPath.getParent().getParent().getParent().resolve("_data").resolve(name);
19        } catch (URISyntaxException e) {
20            throw new IOException("Invalid syntax in class path");
21        }
22    }
23
24    public static byte[] getDataFileBytes(String name) throws IOException {
25        return Files.readAllBytes(getDataPath(name));
26    }
27
28    public static String getDataFileText(String name) throws IOException {
29        return new String(getDataFileBytes(name));
30    }
31
32    public static Object getAnswer(long n) throws IOException {
33        String csvContent = getDataFileText("answers.tsv");
34        try (BufferedReader reader = new BufferedReader(new StringReader(csvContent))) {
35            String line;
36            reader.readLine();
37            while ((line = reader.readLine()) != null) {
38                String[] arr = line.split("\t");
39                long key = Long.parseLong(arr[0]);
40                if (key != n)
41                    continue;
42
43                String type = arr[1];
44                int bitLength = Integer.parseInt(arr[2]);
45                String value = arr[3];
46
47                switch (type) {
48                case "str":
49                    return value;
50                case "int":
51                    switch (bitLength) {
52                    case 8:
53                        return (byte) Integer.parseInt(value);
54                    case 16:
55                        return (short) Integer.parseInt(value);
56                    case 32:
57                        return Integer.parseInt(value);
58                    case 64:
59                        return Long.parseLong(value);
60                    default:
61                        // no return, just continue
62                    }
63                    break;
64                case "uint":
65                    switch (bitLength) {
66                    case 8:
67                        return (byte) Integer.parseUnsignedInt(value);
68                    case 16:
69                        return (short) Integer.parseUnsignedInt(value);
70                    case 32:
71                        return Integer.parseUnsignedInt(value);
72                    case 64:
73                        return Long.parseUnsignedLong(value);
74                    default:
75                        // no return, just continue
76                    }
77                default:
78                    // no return, just continue
79                }
80
81                // If no valid type or bit length was found
82                throw new IOException("Unsupported type/length: " + type + ", " + bitLength);
83            }
84        }
85        throw new IOException("Answer not found.");
86    }
87}

Tags: file-io