privateint hashCode = 0; // did we compute the hashcode ? privateboolean hasHashCode = false;
// Internal objects to represent array + offset, and specific methods privatefinal ByteChunk byteC = new ByteChunk(); privatefinal CharChunk charC = new CharChunk();
/** *Setsthecontenttothespecifiedsubarrayofbytes. * *@parambthebytes *@paramoffthestartoffsetofthebytes *@paramlenthelengthofthebytes
*/ publicvoid setBytes(byte[] b, int off, int len) {
byteC.setBytes(b, off, len);
type = T_BYTES;
hasHashCode = false;
hasLongValue = false;
}
/** *Setsthecontenttobeachar[] * *@paramcthechars *@paramoffthestartoffsetofthechars *@paramlenthelengthofthechars
*/ publicvoid setChars(char[] c, int off, int len) {
charC.setChars(c, off, len);
type = T_CHARS;
hasHashCode = false;
hasLongValue = false;
}
/** *Setthecontenttobeastring * *@paramsThestring
*/ publicvoid setString(String s) {
strValue = s;
hasHashCode = false;
hasLongValue = false; if (s == null) {
type = T_NULL;
} else {
type = T_STR;
}
}
// -------------------- Conversion and getters --------------------
/** *Computethestringvalue. * *@returnthestring
*/
@Override public String toString() { switch (type) { case T_NULL: case T_STR: // No conversion required break; case T_BYTES:
strValue = byteC.toString(); break; case T_CHARS:
strValue = charC.toString(); break;
}
return strValue;
}
/** *ConverttoString(ifnotalreadyoftheStringtype)andthenreturntheStringvalue. * *@returnThecurrentvalueasaString
*/ public String toStringType() { switch (type) { case T_NULL: case T_STR: // No conversion required break; case T_BYTES:
setString(byteC.toString()); break; case T_CHARS:
setString(charC.toString()); break;
}
// normal hash. privateint hash() { int code = 0; switch (type) { case T_STR: // We need to use the same hash function for (int i = 0; i < strValue.length(); i++) {
code = code * 37 + strValue.charAt(i);
} return code; case T_CHARS: return charC.hash(); case T_BYTES: return byteC.hash(); default: return0;
}
}
// Inefficient initial implementation. Will be replaced on the next // round of tune-up publicint indexOf(String s, int starting) {
toString(); return strValue.indexOf(s, starting);
}
// Inefficient initial implementation. Will be replaced on the next // round of tune-up publicint indexOf(String s) { return indexOf(s, 0);
}
publicint indexOfIgnoreCase(String s, int starting) {
toString();
String upper = strValue.toUpperCase(Locale.ENGLISH);
String sU = s.toUpperCase(Locale.ENGLISH); return upper.indexOf(sU, starting);
}
/** *CopythesrcintothisMessageBytes,allocatingmorespaceifneeded. * *@paramsrcThesource * *@throwsIOExceptionWritingoverflowdatatotheoutputchannelfailed
*/ publicvoid duplicate(MessageBytes src) throws IOException { switch (src.getType()) { case T_BYTES:
type = T_BYTES;
ByteChunk bc = src.getByteChunk();
byteC.allocate(2 * bc.getLength(), -1);
byteC.append(bc); break; case T_CHARS:
type = T_CHARS;
CharChunk cc = src.getCharChunk();
charC.allocate(2 * cc.getLength(), -1);
charC.append(cc); break; case T_STR:
type = T_STR;
String sc = src.getString(); this.setString(sc); break;
}
setCharset(src.getCharset());
}
// efficient long privatelong longValue; privateboolean hasLongValue = false;
/** *Setthebuffertotherepresentationofalong. * *@paramlThelong
*/ publicvoid setLong(long l) {
byteC.allocate(32, 64); long current = l; byte[] buf = byteC.getBuffer(); int start = 0; int end = 0; if (l == 0) {
buf[end++] = (byte) '0';
} if (l < 0) {
current = -l;
buf[end++] = (byte) '-';
} while (current > 0) { int digit = (int) (current % 10);
current = current / 10;
buf[end++] = HexUtils.getHex(digit);
}
byteC.setOffset(0);
byteC.setEnd(end); // Inverting buffer
end--; if (l < 0) {
start++;
} while (end > start) { byte temp = buf[start];
buf[start] = buf[end];
buf[end] = temp;
start++;
end--;
}
longValue = l;
hasHashCode = false;
hasLongValue = true;
type = T_BYTES;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.