Skip to content
Snippets Groups Projects
Commit d6c8fa44 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

more java decoder pragma skeleton

parent 9bfa7c14
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@ package se.lth.control.labcomm;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.EOFException;
......@@ -11,11 +12,55 @@ public class DecoderChannel implements Decoder {
private DataInputStream in;
private DecoderRegistry registry;
public DecoderChannel(InputStream in) throws IOException {
public DecoderChannel(InputStream in, DecoderRegistry reg) throws IOException {
this.in = new DataInputStream(in);
registry = new DecoderRegistry();
this.registry = reg;
}
public DecoderChannel(InputStream in) throws IOException {
this(in, new DecoderRegistry());
}
private void processSampleDef() throws IOException {
int index = decodePacked32();
String name = decodeString();
int signature_length = decodePacked32();
byte[] signature = new byte[signature_length];
ReadBytes(signature, signature_length);
registry.add(index, name, signature);
}
private void processPragma(int len) throws IOException {
// String pt = decodeString();
// System.out.println("Pragma["+pt+"]: skipping "+plen+" bytes");
// for(int i=0; i<plen; i++) {
// decodeByte();
// }
String type = decodeString();
int plen = len - (type.length()+1); // XXX HERE BE DRAGONS: +1?
System.out.println("[ begin pragma ("+type+") ]");
//System.out.println("metadata : "+len + " bytes, ref: " + Integer.toHexString(typeRefId)+".");
byte buf[] = new byte[plen];
for(int i=0; i<plen; i++) {
buf[i]=decodeByte();
}
ByteArrayInputStream bis = new ByteArrayInputStream(buf);
DecoderChannel pd = new DecoderChannel(bis, registry);
try{
pd.run();
} catch(java.io.EOFException ex) {
// We're done.
} catch(Throwable e) {
// Something unexpected happened
// (an unregistered handler is OK, but still report it.
// TODO: implement exception handling here. Aborting is fine,
// that only means skipping the rest of the metadata packet.)
System.out.println("Exception while decoding pragma: "+e);
}
System.out.println("[ end pragma ]");
}
public void runOne() throws Exception {
boolean done = false;
while (!done) {
......@@ -30,23 +75,21 @@ public class DecoderChannel implements Decoder {
}
} break;
case Constant.SAMPLE_DEF: {
int index = decodePacked32();
String name = decodeString();
int signature_length = decodePacked32();
byte[] signature = new byte[signature_length];
ReadBytes(signature, signature_length);
registry.add(index, name, signature);
processSampleDef();
} break;
case Constant.PRAGMA: {
System.out.println("Pragma: skipping "+length+" bytes");
for(int i=0; i<length; i++) {
decodeByte();
}
processPragma(length);
// String pt = decodeString();
// int plen = length - pt.length()-1;
// System.out.println("Pragma["+pt+"]: skipping "+plen+" bytes");
// for(int i=0; i<plen; i++) {
// decodeByte();
// }
} break;
default: {
DecoderRegistry.Entry e = registry.get(tag);
if (e == null) {
throw new IOException("Unhandled tag " + tag);
throw new IOException("Unhandled tag 0x" + Integer.toHexString(tag));
}
SampleDispatcher d = e.getDispatcher();
if (d == null) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment