Skip to content

Commit

Permalink
AtU8 UTF8 atoms chunk
Browse files Browse the repository at this point in the history
Check for old Atom, if it doesn't exist, check for AtU8 chunk.
  • Loading branch information
KronicDeth committed Aug 7, 2017
1 parent 8008d24 commit 0f3417e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
10 changes: 9 additions & 1 deletion src/org/elixir_lang/beam/Beam.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 8 additions & 13 deletions src/org/elixir_lang/beam/chunk/Atoms.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,13 +18,9 @@ public class Atoms {
*/

@NotNull
private List<String> atomList;
private final List<String> atomList;

/*
* Constructors
*/

public Atoms(@NotNull List<String> atomList) {
private Atoms(@NotNull List<String> atomList) {
this.atomList = Collections.unmodifiableList(atomList);
}

Expand All @@ -33,23 +29,23 @@ public Atoms(@NotNull List<String> 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<Long, Integer> atomCountByteCount = unsignedInt(chunk.data, offset);
long atomCount = atomCountByteCount.first;
offset += atomCountByteCount.second;

List<String> atomList = new ArrayList<String>();
List<String> atomList = new ArrayList<>();

for (long i = 0; i < atomCount; i++) {
Pair<Integer, Integer> 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);
}
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/org/elixir_lang/beam/chunk/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ static Pair<Long, Integer> unsignedInt(@NotNull byte[] bytes, int offset) {

public enum TypeID {
ATOM("Atom"),
ATU8("AtU8"),
EXPT("ExpT");

private final String typeID;
Expand Down

0 comments on commit 0f3417e

Please sign in to comment.