Skip to content

Commit

Permalink
Ignore any content present after the EndMetafile
Browse files Browse the repository at this point in the history
  • Loading branch information
DIAA committed Nov 15, 2024
1 parent 1bd92ed commit 71af0b7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/net/sf/jcgm/core/CGM.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
public class CGM implements Cloneable {
private List<Command> commands;

/**
* Acc. to ISO/IEC 8632-1:19999(E) §6.1:<br>
* "The [...] END METAFILE elements [...] occur exactly once in a complete metafile [...]".
*/
private boolean endMetafileReached = false;

private final ArrayList<Message> messages = new ArrayList<>();
private int colourIndexPrecision = 8;
private int colourPrecision = 8;
Expand Down Expand Up @@ -103,7 +109,7 @@ public CGM(File cgmFile) throws IOException {
public void read(DataInput in) throws IOException {
reset();
this.commands = new ArrayList<Command>(INITIAL_NUM_COMMANDS);
while (true) {
while (!this.endMetafileReached) {
Command c = Command.read(in, this);
if (c == null)
break;
Expand All @@ -116,6 +122,23 @@ public void read(DataInput in) throws IOException {
c.cleanUpArguments();
this.commands.add(c);
}

// ignore trailing data for now: it only happened once with an A320 ASM sample;
// TODO: integrate proper logger and log whenever there is unexpected trailing data
// StringBuilder trailingData = new StringBuilder();
// try {
// String line = in.readLine();
// while (line != null) {
// trailingData.append(line);
// line = in.readLine();
// }
// } catch (Exception e) {
// throw new CgmException("Failed to read the trailing data of the CGM.");
// }
// // most of (all?) CGM samples can have a trailing character that can be ignored
// if (trailingData.length() > 1) {
// throw new CgmException("The CGM contains additional data that was not processed: [" + trailingData + "].");
// }
}

/**
Expand Down Expand Up @@ -691,7 +714,10 @@ public void showCGMCommands(PrintStream stream) {
public List<Command> getCommands() {
return Collections.unmodifiableList(this.commands);
}


public void setEndMetafileReached() {
this.endMetafileReached = true;
}
}

/*
Expand Down
2 changes: 2 additions & 0 deletions src/net/sf/jcgm/core/EndMetafile.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class EndMetafile extends Command {
public EndMetafile(int ec, int eid, int l, DataInput in, CGM cgm)
throws IOException {
super(ec, eid, l, in, cgm);

cgm.setEndMetafileReached();
}

@Override
Expand Down

0 comments on commit 71af0b7

Please sign in to comment.