📄 binaryformatwriter.java
字号:
package com.sri.oaa2.simplefac;
import com.sri.oaa2.icl.*;
import java.io.*;
import org.apache.log4j.Logger;
import java.util.*;
public class BinaryFormatWriter implements FormatTypes, FormatWriter, BinaryStreamTypes
{
// Logger for this class
static Logger logger = Logger.getLogger(BinaryFormatWriter.class.getName());
/**
* This is computed as the md5sum of the text of this source file, using "" as the VERSIONID.
* It is not always computed for any change, but when the binary format changes.
*/
public final static String VERSIONID = "";
private DataOutputStream outWriter;
private OutputStream ostream;
private int maxSendSize = 8;
public BinaryFormatWriter(OutputStream os, int bufSize)
{
// Giving the buffer output stream a buffer size of 10k speeds up
// performance when writing small terms. Initial tests show that
setOstream(new BufferedOutputStream(os, 10240));
setMaxSendSize(bufSize);
initOutWriter(getOstream());
}
public int getMaxSendSize()
{
return this.maxSendSize;
}
public void setMaxSendSize(int m)
{
this.maxSendSize = m;
}
/**
* Set format to use
*/
public void setFormat(int f)
{
}
/**
* Get format
*/
public int getFormat()
{
return BINARY;
}
/**
* Set output stream
*/
public void setOstream(OutputStream o)
{
ostream = o;
}
/**
* Get output stream
*/
public OutputStream getOstream()
{
return ostream;
}
/**
* Initialize output stream writer
*/
protected void initOutWriter(OutputStream os)
{
outWriter = new DataOutputStream(os);
}
/**
* Do depth first traversal through tree of terms, favouring last children in
* each set of siblings.
*/
public void write(IclTerm t)
{
if(logger.isDebugEnabled()) {
logger.debug("BinaryFormatWriter.write() writing term [" + t.toString() + "]");
}
try {
ArrayList termRecordList = new ArrayList();
termRecordList.add(new TermRecord(t));
TermRecord currentRecord;
EXAMINING_RECORDS:
while(termRecordList.size() > 0) {
// Get term and increment visit number
currentRecord = (TermRecord)termRecordList.remove(termRecordList.size() - 1);
++currentRecord.numTimesVisited;
if(currentRecord.numTimesVisited == 2) {
// this is only true for compound terms
switch(currentRecord.t.getType()) {
case OaaPrologVocabTokenTypes.STRUCT:
if(logger.isDebugEnabled()) {
logger.debug("BinaryFormatWriter.write() writing STRUCT");
}
String functor;
functor = ToFunctor.getInstance().from(currentRecord.t);
outWriter.writeInt(BinaryStreamTypes.ICLSTRUCT);
outWriter.writeInt(currentRecord.t.size());
outWriter.writeInt(functor.length());
outWriter.writeChars(functor);
break;
case OaaPrologVocabTokenTypes.LIST:
if(logger.isDebugEnabled()) {
logger.debug("BinaryFormatWriter.write() writing LIST");
}
if(((IclList)currentRecord.t).hasEmptyTail()) {
outWriter.writeInt(BinaryStreamTypes.ICLLIST);
}
else {
outWriter.writeInt(BinaryStreamTypes.ICLHEADTAILLIST);
}
outWriter.writeInt(currentRecord.t.size());
break;
case OaaPrologVocabTokenTypes.GROUP:
if(logger.isDebugEnabled()) {
logger.debug("BinaryFormatWriter.write() writing GROUP");
}
outWriter.writeInt(BinaryStreamTypes.ICLGROUP);
outWriter.writeInt(currentRecord.t.size());
outWriter.writeChar(ToStarter.getInstance().from(currentRecord.t));
break;
default:
logger.error("BinaryFormatWriter.write() Unexpected type visited twice: " + currentRecord.t.getType() + "; " + currentRecord.t.toString());
throw new RuntimeException("BinaryFormatWriter.write() Unexpected type visited twice: " + currentRecord.t.getType() + "; " + currentRecord.t.toString());
}
continue EXAMINING_RECORDS;
}
else {
switch(currentRecord.t.getType()) {
case OaaPrologVocabTokenTypes.INT:
if(logger.isDebugEnabled()) {
logger.debug("BinaryFormatWriter.write() writing INT");
}
outWriter.writeInt(BinaryStreamTypes.ICLINT);
outWriter.writeLong(ToLong.getInstance().from(currentRecord.t));
break;
case OaaPrologVocabTokenTypes.FLOAT:
if(logger.isDebugEnabled()) {
logger.debug("BinaryFormatWriter.write() writing FLOAT");
}
outWriter.writeInt(BinaryStreamTypes.ICLFLOAT);
outWriter.writeDouble(ToDouble.getInstance().from(currentRecord.t));
break;
case OaaPrologVocabTokenTypes.VAR:
if(logger.isDebugEnabled()) {
logger.debug("BinaryFormatWriter.write() writing VAR");
}
String name = currentRecord.t.toString();
outWriter.writeInt(BinaryStreamTypes.ICLVAR);
outWriter.writeInt(name.length());
outWriter.writeChars(name);
break;
case OaaPrologVocabTokenTypes.STR:
if(logger.isDebugEnabled()) {
logger.debug("BinaryFormatWriter.write() writing STR");
}
String value = currentRecord.t.toString();
outWriter.writeInt(BinaryStreamTypes.ICLSTR);
outWriter.writeInt(value.length());
outWriter.writeChars(value);
break;
case OaaPrologVocabTokenTypes.ICLDATAQ:
if(logger.isDebugEnabled()) {
logger.debug("BinaryFormatWriter.write() writing ICLDATAQ");
}
outWriter.writeInt(BinaryStreamTypes.ICLDATAQ);
int rawLen = ((IclDataQ)(currentRecord.t)).getRawLen();
outWriter.writeInt(rawLen);
byte[] data = ((IclDataQ)(currentRecord.t)).getData();
int remaining = rawLen;
int offset = 0;
int len = 0;
while(remaining > 0) {
len = (remaining < getMaxSendSize()) ? remaining : getMaxSendSize();
outWriter.write(data, offset, len);
remaining -= len;
offset += len;
}
break;
case OaaPrologVocabTokenTypes.STRUCT:
case OaaPrologVocabTokenTypes.LIST:
case OaaPrologVocabTokenTypes.GROUP:
termRecordList.add(currentRecord);
Iterator i = currentRecord.t.iterator();
while(i.hasNext()) {
termRecordList.add(new TermRecord((IclTerm)i.next()));
}
continue EXAMINING_RECORDS;
default:
logger.error("BinaryFormatWriter.write() Unexpected type visited < 2 times: " + currentRecord.t.getType() + "; " + currentRecord.t.toString());
throw new RuntimeException("BinaryFormatWriter.write() Unexpected type visited < 2 times: " + currentRecord.t.getType() + "; " + currentRecord.t.toString());
}
}
}
outWriter.writeInt(BinaryStreamTypes.SENTINEL);
outWriter.flush();
}
catch(IOException ioe) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ioe.printStackTrace(pw);
pw.flush();
logger.error("BinaryFormatWriter.write() Could not send message exception is: " + sw.toString());
logger.error("BinaryFormatWriter.write() Could not send binary message: " + t.toString());
}
}
}
class TermRecord
{
public IclTerm t = null;
int numTimesVisited = 0;
public TermRecord()
{
}
public TermRecord(IclTerm term)
{
t = term;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -