package com.itstherules.parser;
import java.io.Writer;
import java.util.HashMap;
import com.itstherules.io.BufferHelper;
import com.itstherules.io.FileReader;
import com.itstherules.io.IOHelper;
import com.itstherules.metadata.EmbeddedData;
import com.itstherules.tags.FlvTag;
import com.itstherules.tags.MetaTag;
public class MetaDataParser {
private IOHelper ioHelper;
private FileReader fh;
private BufferHelper bh;
private MetaTag metaTag;
private boolean found;
private boolean debug;
public MetaDataParser(IOHelper ioHelper) {
this.ioHelper = ioHelper;
found = false;
metaTag = null;
fh = ioHelper.getFileReader();
fh.setDebug(ioHelper.isDebug());
bh = ioHelper.getBufferHelper();
debug = ioHelper.isDebug();
}
public HashMap getMetaData() {
return metaTag != null ? (HashMap) metaTag.getMetaData() : null;
}
public void findMetaTag() {
int tagType = 0;
do {
byte mbb[] = fh.readByteArray(5);
if (mbb == null) break;
tagType = bh.readUint(mbb, 4, 1);
if (tagType == 18) {
metaTag = new MetaTag(ioHelper);
if (!metaTag.getEvent().equals("onMetaData")) continue;
found = true;
break;
}
new FlvTag(ioHelper);
} while (true);
}
public void writeMetaData(Writer writer) throws Exception {
if (!found) {
writer.write("no metadata has been embedded");
writer.write("\n");
} else {
EmbeddedData embeddedData = new EmbeddedData();
try {
embeddedData.setData((HashMap) metaTag.getMetaData());
writer.write(embeddedData.printMetaData());
writer.write("\n");
} catch (Exception e) {
writer.write("An error occurred while parsing metadata ... try re-embedding");
if (debug) writer.write(e.getMessage());
}
}
}
}