From 0f3417eec4eabf442c29a6e83a53c1cfa2772e89 Mon Sep 17 00:00:00 2001 From: Luke Imhoff Date: Sun, 6 Aug 2017 22:54:50 -0500 Subject: [PATCH] AtU8 UTF8 atoms chunk Check for old Atom, if it doesn't exist, check for AtU8 chunk. --- src/org/elixir_lang/beam/Beam.java | 10 +++++++++- src/org/elixir_lang/beam/chunk/Atoms.java | 21 ++++++++------------- src/org/elixir_lang/beam/chunk/Chunk.java | 1 + 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/org/elixir_lang/beam/Beam.java b/src/org/elixir_lang/beam/Beam.java index 90018cf50..0a7f8d867 100644 --- a/src/org/elixir_lang/beam/Beam.java +++ b/src/org/elixir_lang/beam/Beam.java @@ -14,12 +14,14 @@ import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import static org.elixir_lang.beam.chunk.Chunk.TypeID.ATOM; +import static org.elixir_lang.beam.chunk.Chunk.TypeID.ATU8; import static org.elixir_lang.beam.chunk.Chunk.TypeID.EXPT; import static org.elixir_lang.beam.chunk.Chunk.length; import static org.elixir_lang.beam.chunk.Chunk.typeID; @@ -166,7 +168,13 @@ public Atoms atoms() { Chunk chunk = chunk(ATOM); if (chunk != null) { - atoms = Atoms.from(chunk); + atoms = Atoms.from(chunk, ATOM, Charset.forName("LATIN1")); + } else { + chunk = chunk(ATU8); + + if (chunk != null) { + atoms = Atoms.from(chunk, ATU8, Charset.forName("UTF-8")); + } } return atoms; diff --git a/src/org/elixir_lang/beam/chunk/Atoms.java b/src/org/elixir_lang/beam/chunk/Atoms.java index ed6ee11c5..b48f88a45 100644 --- a/src/org/elixir_lang/beam/chunk/Atoms.java +++ b/src/org/elixir_lang/beam/chunk/Atoms.java @@ -4,11 +4,11 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import static org.elixir_lang.beam.chunk.Chunk.TypeID.ATOM; import static org.elixir_lang.beam.chunk.Chunk.unsignedByte; import static org.elixir_lang.beam.chunk.Chunk.unsignedInt; @@ -18,13 +18,9 @@ public class Atoms { */ @NotNull - private List atomList; + private final List atomList; - /* - * Constructors - */ - - public Atoms(@NotNull List atomList) { + private Atoms(@NotNull List atomList) { this.atomList = Collections.unmodifiableList(atomList); } @@ -33,23 +29,23 @@ public Atoms(@NotNull List atomList) { */ @Nullable - public static Atoms from(@NotNull Chunk chunk) { + public static Atoms from(@NotNull Chunk chunk, @NotNull Chunk.TypeID typeID, @NotNull Charset charset) { Atoms atoms = null; - if (chunk.typeID.equals(ATOM.toString()) && chunk.data.length >= 4) { + if (chunk.typeID.equals(typeID.toString()) && chunk.data.length >= 4) { int offset = 0; Pair atomCountByteCount = unsignedInt(chunk.data, offset); long atomCount = atomCountByteCount.first; offset += atomCountByteCount.second; - List atomList = new ArrayList(); + List atomList = new ArrayList<>(); for (long i = 0; i < atomCount; i++) { Pair atomLengthByteCount = unsignedByte(chunk.data[offset]); int atomLength = atomLengthByteCount.first; offset += atomLengthByteCount.second; - String entry = new String(chunk.data, offset, atomLength); + String entry = new String(chunk.data, offset, atomLength, charset); offset += atomLength; atomList.add(entry); } @@ -65,9 +61,8 @@ public static Atoms from(@NotNull Chunk chunk) { */ /** - * * @param index 1-based index. 1 is reserved for {#link moduleName} - * @return atom if + * @return atom if it exists */ @Nullable public String get(int index) { diff --git a/src/org/elixir_lang/beam/chunk/Chunk.java b/src/org/elixir_lang/beam/chunk/Chunk.java index 66d3a13c8..b328d95d4 100644 --- a/src/org/elixir_lang/beam/chunk/Chunk.java +++ b/src/org/elixir_lang/beam/chunk/Chunk.java @@ -101,6 +101,7 @@ static Pair unsignedInt(@NotNull byte[] bytes, int offset) { public enum TypeID { ATOM("Atom"), + ATU8("AtU8"), EXPT("ExpT"); private final String typeID;