📄 bytechunk.java
字号:
System.arraycopy(buff, start, src, off, n);
start += n;
return n;
}
/** Send the buffer to the sink. Called by append() when the limit is reached.
* You can also call it explicitely to force the data to be written.
*
* @throws IOException
*/
public void flushBuffer()
throws IOException
{
//assert out!=null
if( out==null ) {
throw new IOException( "Buffer overflow, no sink " + limit + " " +
buff.length );
}
out.realWriteBytes( buff, start, end-start );
end=start;
}
/** Make space for len chars. If len is small, allocate
* a reserve space too. Never grow bigger than limit.
*/
private void makeSpace(int count)
{
byte[] tmp = null;
int newSize;
int desiredSize=end + count;
// Can't grow above the limit
if( limit > 0 &&
desiredSize > limit) {
desiredSize=limit;
}
if( buff==null ) {
if( desiredSize < 256 ) desiredSize=256; // take a minimum
buff=new byte[desiredSize];
}
// limit < buf.length ( the buffer is already big )
// or we already have space XXX
if( desiredSize <= buff.length ) {
return;
}
// grow in larger chunks
if( desiredSize < 2 * buff.length ) {
newSize= buff.length * 2;
if( limit >0 &&
newSize > limit ) newSize=limit;
tmp=new byte[newSize];
} else {
newSize= buff.length * 2 + count ;
if( limit > 0 &&
newSize > limit ) newSize=limit;
tmp=new byte[newSize];
}
System.arraycopy(buff, start, tmp, 0, end-start);
buff = tmp;
tmp = null;
end=end-start;
start=0;
}
// -------------------- Conversion and getters --------------------
public String toString() {
if (null == buff) {
return null;
} else if (end-start == 0) {
return "";
}
return StringCache.toString(this);
}
public String toStringInternal() {
String strValue=null;
try {
if( enc==null ) enc=DEFAULT_CHARACTER_ENCODING;
strValue = new String( buff, start, end-start, enc );
/*
Does not improve the speed too much on most systems,
it's safer to use the "clasical" new String().
Most overhead is in creating char[] and copying,
the internal implementation of new String() is very close to
what we do. The decoder is nice for large buffers and if
we don't go to String ( so we can take advantage of reduced GC)
// Method is commented out, in:
return B2CConverter.decodeString( enc );
*/
} catch (java.io.UnsupportedEncodingException e) {
// Use the platform encoding in that case; the usage of a bad
// encoding will have been logged elsewhere already
strValue = new String(buff, start, end-start);
}
return strValue;
}
public int getInt()
{
return Ascii.parseInt(buff, start,end-start);
}
public long getLong() {
return Ascii.parseLong(buff, start,end-start);
}
// -------------------- equals --------------------
/**
* Compares the message bytes to the specified String object.
* @param s the String to compare
* @return true if the comparison succeeded, false otherwise
*/
public boolean equals(String s) {
// XXX ENCODING - this only works if encoding is UTF8-compat
// ( ok for tomcat, where we compare ascii - header names, etc )!!!
byte[] b = buff;
int blen = end-start;
if (b == null || blen != s.length()) {
return false;
}
int boff = start;
for (int i = 0; i < blen; i++) {
if (b[boff++] != s.charAt(i)) {
return false;
}
}
return true;
}
/**
* Compares the message bytes to the specified String object.
* @param s the String to compare
* @return true if the comparison succeeded, false otherwise
*/
public boolean equalsIgnoreCase(String s) {
byte[] b = buff;
int blen = end-start;
if (b == null || blen != s.length()) {
return false;
}
int boff = start;
for (int i = 0; i < blen; i++) {
if (Ascii.toLower(b[boff++]) != Ascii.toLower(s.charAt(i))) {
return false;
}
}
return true;
}
public boolean equals( ByteChunk bb ) {
return equals( bb.getBytes(), bb.getStart(), bb.getLength());
}
public boolean equals( byte b2[], int off2, int len2) {
byte b1[]=buff;
if( b1==null && b2==null ) return true;
int len=end-start;
if ( len2 != len || b1==null || b2==null )
return false;
int off1 = start;
while ( len-- > 0) {
if (b1[off1++] != b2[off2++]) {
return false;
}
}
return true;
}
public boolean equals( CharChunk cc ) {
return equals( cc.getChars(), cc.getStart(), cc.getLength());
}
public boolean equals( char c2[], int off2, int len2) {
// XXX works only for enc compatible with ASCII/UTF !!!
byte b1[]=buff;
if( c2==null && b1==null ) return true;
if (b1== null || c2==null || end-start != len2 ) {
return false;
}
int off1 = start;
int len=end-start;
while ( len-- > 0) {
if ( (char)b1[off1++] != c2[off2++]) {
return false;
}
}
return true;
}
/**
* Returns true if the message bytes starts with the specified string.
* @param s the string
*/
public boolean startsWith(String s) {
// Works only if enc==UTF
byte[] b = buff;
int blen = s.length();
if (b == null || blen > end-start) {
return false;
}
int boff = start;
for (int i = 0; i < blen; i++) {
if (b[boff++] != s.charAt(i)) {
return false;
}
}
return true;
}
/* Returns true if the message bytes start with the specified byte array */
public boolean startsWith(byte[] b2) {
byte[] b1 = buff;
if (b1 == null && b2 == null) {
return true;
}
int len = end - start;
if (b1 == null || b2 == null || b2.length > len) {
return false;
}
for (int i = start, j = 0; i < end && j < b2.length; ) {
if (b1[i++] != b2[j++])
return false;
}
return true;
}
/**
* Returns true if the message bytes starts with the specified string.
* @param s the string
* @param pos The position
*/
public boolean startsWithIgnoreCase(String s, int pos) {
byte[] b = buff;
int len = s.length();
if (b == null || len+pos > end-start) {
return false;
}
int off = start+pos;
for (int i = 0; i < len; i++) {
if (Ascii.toLower( b[off++] ) != Ascii.toLower( s.charAt(i))) {
return false;
}
}
return true;
}
public int indexOf( String src, int srcOff, int srcLen, int myOff ) {
char first=src.charAt( srcOff );
// Look for first char
int srcEnd = srcOff + srcLen;
for( int i=myOff+start; i <= (end - srcLen); i++ ) {
if( buff[i] != first ) continue;
// found first char, now look for a match
int myPos=i+1;
for( int srcPos=srcOff + 1; srcPos< srcEnd; ) {
if( buff[myPos++] != src.charAt( srcPos++ ))
break;
if( srcPos==srcEnd ) return i-start; // found it
}
}
return -1;
}
// -------------------- Hash code --------------------
// normal hash.
public int hash() {
return hashBytes( buff, start, end-start);
}
// hash ignoring case
public int hashIgnoreCase() {
return hashBytesIC( buff, start, end-start );
}
private static int hashBytes( byte buff[], int start, int bytesLen ) {
int max=start+bytesLen;
byte bb[]=buff;
int code=0;
for (int i = start; i < max ; i++) {
code = code * 37 + bb[i];
}
return code;
}
private static int hashBytesIC( byte bytes[], int start,
int bytesLen )
{
int max=start+bytesLen;
byte bb[]=bytes;
int code=0;
for (int i = start; i < max ; i++) {
code = code * 37 + Ascii.toLower(bb[i]);
}
return code;
}
/**
* Returns true if the message bytes starts with the specified string.
* @param c the character
* @param starting The start position
*/
public int indexOf(char c, int starting) {
int ret = indexOf( buff, start+starting, end, c);
return (ret >= start) ? ret - start : -1;
}
public static int indexOf( byte bytes[], int off, int end, char qq )
{
// Works only for UTF
while( off < end ) {
byte b=bytes[off];
if( b==qq )
return off;
off++;
}
return -1;
}
/** Find a character, no side effects.
* @return index of char if found, -1 if not
*/
public static int findChar( byte buf[], int start, int end, char c ) {
byte b=(byte)c;
int offset = start;
while (offset < end) {
if (buf[offset] == b) {
return offset;
}
offset++;
}
return -1;
}
/** Find a character, no side effects.
* @return index of char if found, -1 if not
*/
public static int findChars( byte buf[], int start, int end, byte c[] ) {
int clen=c.length;
int offset = start;
while (offset < end) {
for( int i=0; i<clen; i++ )
if (buf[offset] == c[i]) {
return offset;
}
offset++;
}
return -1;
}
/** Find the first character != c
* @return index of char if found, -1 if not
*/
public static int findNotChars( byte buf[], int start, int end, byte c[] )
{
int clen=c.length;
int offset = start;
boolean found;
while (offset < end) {
found=true;
for( int i=0; i<clen; i++ ) {
if (buf[offset] == c[i]) {
found=false;
break;
}
}
if( found ) { // buf[offset] != c[0..len]
return offset;
}
offset++;
}
return -1;
}
/**
* Convert specified String to a byte array. This ONLY WORKS for ascii, UTF chars will be truncated.
*
* @param value to convert to byte array
* @return the byte array value
*/
public static final byte[] convertToBytes(String value) {
byte[] result = new byte[value.length()];
for (int i = 0; i < value.length(); i++) {
result[i] = (byte) value.charAt(i);
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -