📄 icldataq.java
字号:
package com.sri.oaa2.icl;
import java.util.zip.CRC32;
/**
* IclTerm representing binary data.
*/
public final class IclDataQ extends IclAtomic
{
/// The functor
protected byte[] data = null;
int rawLen = -1;
int numQuotes = -1;
/**
* Create an empty IclDataQ. Use setData() to set the binary data.
*/
public IclDataQ()
{
setType(OaaPrologVocabTokenTypes.ICLDATAQ);
setData(new byte[0]);
}
/**
* Construct an IclDataQ from a byte array.
* This constructor assumes that the given array does not have quotes
* escaped.
*
* @param byte[] d: the byte array data
*/
public IclDataQ(byte[] d)
{
setType(OaaPrologVocabTokenTypes.ICLDATAQ);
setData(d);
}
/**
* Doubles (escapes) double quotes in the data.
*/
public void fixQuotes()
{
byte[] d = getData();
if(getNumQuotes() == 0) {
return;
}
int realDataIndex = 0;
for(int i = 1;
i < d.length - 1;
++i, ++realDataIndex) {
if(d[i] == ((byte)'"')) {
d[realDataIndex] = '"';
++i;
}
else {
d[realDataIndex] = d[i];
}
}
byte[] newData = new byte[realDataIndex];
System.arraycopy(d, 0, newData, 0, realDataIndex);
setData(newData);
}
/**
* Doubles (escapes) double quotes in a String.
*
* @param String s: the String to escape
* @return byte[]: a byte array representing the data.
*/
public static byte[] fixQuotes(String s)
{
IclDataQ d = new IclDataQ();
byte[] b = s.getBytes();
d.setData(b);
d.fixQuotes();
return d.getData();
}
/**
* Return the data; the length of the returned array is the length of
* the data.
*/
public byte[] getData()
{
return this.data;
}
/**
* The length of this array should be the length of the raw (quotes not
* doubled) data. This data should not be escaped. This automatically
* sets the raw length.
*/
public void setData(byte[] d)
{
if(this.data == d) {
return;
}
this.data = d;
this.setRawLen(d.length);
CRC32 computedCrc = new CRC32();
computedCrc.update(d);
this.setCrc(computedCrc.getValue());
}
/**
* Raw length of the data. This is the number of bytes of unescaped data.
*/
public int getRawLen()
{
return this.rawLen;
}
/**
* Set the raw length of the data. Not needed if setData(byte[]) has been
* used.
*/
public void setRawLen(int r)
{
this.rawLen = r;
}
/**
* Get the number of double quotes in the data.
*
* @return int: -1 if unknown, or the number of quotes
*/
public int getNumQuotes()
{
return this.numQuotes;
}
/**
* Set the number of double quotes in the data.
*/
public void setNumQuotes(int n)
{
this.numQuotes = n;
}
/**
* For parser.
*/
protected IclDataQ(int id) {
super(OaaPrologVocabTokenTypes.ICLDATAQ);
}
/** Accept the visitor. **/
protected Object accept(OaaPrologVisitor visitor, Object d) {
return visitor.visit(this, d);
}
/**
* Return data as icldataq("<data>"), where <data> is the data with
* quote characters doubled
*/
public String getQuotedData()
{
StringBuffer b = new StringBuffer();
b.append("icldataq(\"");
byte[] unquoted = getData();
int start = 0;
for(int i = 0; i < getRawLen(); ++i) {
if(unquoted[i] == ((byte)'"')) {
// append all the stuff that wasn't a quote character
if((i - start) > 0) {
b.append(new String(unquoted, start, i - start));
}
b.append("\"\"");
start = i + 1;
}
}
// might not have added stuff at the end of the unquoted data
if(start < getRawLen()) {
b.append(new String(unquoted, start, getRawLen() - start));
}
b.append("\")");
return b.toString();
}
/**
* Get a String representing the data. This String consists
* of an octal representation of the String: \010\012.
*
* @return String: an octal representation of the data
*/
public String toHumanReadableString()
{
StringBuffer b = new StringBuffer("");
byte[] d = getData();
Byte aByte;
String s;
for(int i = 0; i < d.length; ++i) {
aByte = new Byte(d[i]);
s = Integer.toOctalString(aByte.intValue());
b.append("\\");
if(s.length() < 2) {
b.append("0");
}
b.append(s);
}
return b.toString();
}
/**
* Returns icldataq.
*/
public String toIdentifyingString()
{
return toHumanReadableString();
}
public boolean equals(Object o)
{
if(o == this) {
return true;
}
if(!(o instanceof IclDataQ)) {
return false;
}
IclDataQ t = (IclDataQ)o;
if(t.getData().length != this.getData().length) {
return false;
}
if(t.getCrc() != this.getCrc()) {
return false;
}
for(int i = 0; i < t.getData().length; ++i) {
if((t.getData())[i] != (this.getData())[i]) {
return false;
}
}
return true;
}
long getCrc()
{
CRC32 computedCrc = new CRC32();
computedCrc.update(this.data);
return computedCrc.getValue();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -