ajp13packet.java
来自「精通tomcat书籍原代码,希望大家共同学习」· Java 代码 · 共 521 行 · 第 1/2 页
JAVA
521 行
}
/**
* Copy a chunk of bytes into the packet, starting at the current
* write position. The chunk of bytes is encoded with the length
* in two bytes first, then the data itself, and finally a
* terminating \0 (which is <B>not</B> included in the encoded
* length).
*
* @param b The array from which to copy bytes.
* @param off The offset into the array at which to start copying
* @param numBytes The number of bytes to copy.
*/
public void appendBytes( byte b[], int off, int numBytes ) {
appendInt( numBytes );
if( pos + numBytes >= buff.length ) {
if (log.isDebugEnabled())
log.debug("Buffer overflow " + buff.length + " " + pos + " " + numBytes );
}
System.arraycopy( b, off, buff, pos, numBytes);
buff[pos + numBytes] = 0; // Terminating \0
pos += numBytes + 1;
}
/**
* Write a 32 bits integer at an arbitrary position in the packet, but don't
* change the write position.
*
* @param bpos The 0-indexed position within the buffer at which to
* write the integer (where 0 is the beginning of the header).
* @param val The integer to write.
*/
private void setLongInt( int bPos, int val ) {
buff[bPos] = (byte) ((val >>> 24) & 0xFF);
buff[bPos+1] = (byte) ((val >>> 16) & 0xFF);
buff[bPos+2] = (byte) ((val >>> 8) & 0xFF);
buff[bPos+3] = (byte) (val & 0xFF);
}
public void appendLongInt( int val ) {
setLongInt( pos, val );
pos += 4;
}
/**
* Copy a chunk of bytes into the packet, starting at the current
* write position. The chunk of bytes IS NOT ENCODED with ANY length
* header.
*
* @param b The array from which to copy bytes.
* @param off The offset into the array at which to start copying
* @param numBytes The number of bytes to copy.
*/
public void appendXBytes(byte[] b, int off, int numBytes) {
if( pos + numBytes > buff.length ) {
if (log.isDebugEnabled())
log.debug("appendXBytes - Buffer overflow " + buff.length + " " + pos + " " + numBytes );
}
System.arraycopy( b, off, buff, pos, numBytes);
pos += numBytes;
}
// ============ Data Reading Methods ===================
/**
* Read an integer from packet, and advance the read position past
* it. Integers are encoded as two unsigned bytes with the
* high-order byte first, and, as far as I can tell, in
* little-endian order within each byte.
*/
public int getInt() {
int result = peekInt();
pos += 2;
return result;
}
/**
* Read an integer from the packet, but don't advance the read
* position past it.
*/
public int peekInt() {
int b1 = buff[pos] & 0xFF; // No swap, Java order
int b2 = buff[pos + 1] & 0xFF;
return (b1<<8) + b2;
}
public byte getByte() {
byte res = buff[pos];
pos++;
return res;
}
public byte peekByte() {
return buff[pos];
}
public boolean getBool() {
return (getByte() == (byte) 1);
}
public void getMessageBytes(MessageBytes mb) {
int length = getInt();
if( (length == 0xFFFF) || (length == -1) ) {
mb.setString( null );
return;
}
mb.setBytes( buff, pos, length );
pos += length;
pos++; // Skip the terminating \0
}
public MessageBytes addHeader(MimeHeaders headers) {
int length = getInt();
if( (length == 0xFFFF) || (length == -1) ) {
return null;
}
MessageBytes vMB=headers.addValue( buff, pos, length );
pos += length;
pos++; // Skip the terminating \0
return vMB;
}
/**
* Read a String from the packet, and advance the read position
* past it. See appendString for details on string encoding.
**/
public String getString() throws java.io.UnsupportedEncodingException {
int length = getInt();
if( (length == 0xFFFF) || (length == -1) ) {
if (log.isDebugEnabled())
log.debug("null string " + length);
return null;
}
String s = new String(buff, pos, length, encoding);
pos += length;
pos++; // Skip the terminating \0
return s;
}
/**
* Copy a chunk of bytes from the packet into an array and advance
* the read position past the chunk. See appendBytes() for details
* on the encoding.
*
* @return The number of bytes copied.
*/
public int getBytes(byte dest[]) {
int length = getInt();
if( length > buff.length ) {
// XXX Should be if(pos + length > buff.legth)?
if (log.isDebugEnabled())
log.debug("XXX Assert failed, buff too small ");
}
if( (length == 0xFFFF) || (length == -1) ) {
if (log.isDebugEnabled())
log.debug("null string " + length);
return 0;
}
System.arraycopy( buff, pos, dest, 0, length );
pos += length;
pos++; // Skip terminating \0 XXX I believe this is wrong but harmless
return length;
}
/**
* Read a 32 bits integer from packet, and advance the read position past
* it. Integers are encoded as four unsigned bytes with the
* high-order byte first, and, as far as I can tell, in
* little-endian order within each byte.
*/
public int getLongInt() {
int result = peekLongInt();
pos += 4;
return result;
}
/**
* Copy a chunk of bytes from the packet into an array and advance
* the read position past the chunk. See appendXBytes() for details
* on the encoding.
*
* @return The number of bytes copied.
*/
public int getXBytes(byte[] dest, int length) {
if( length > buff.length ) {
// XXX Should be if(pos + length > buff.legth)?
if (log.isDebugEnabled())
log.debug("XXX Assert failed, buff too small ");
}
System.arraycopy( buff, pos, dest, 0, length );
pos += length;
return length;
}
/**
* Read a 32 bits integer from the packet, but don't advance the read
* position past it.
*/
public int peekLongInt() {
int b1 = buff[pos] & 0xFF; // No swap, Java order
b1 <<= 8;
b1 |= (buff[pos + 1] & 0xFF);
b1 <<= 8;
b1 |= (buff[pos + 2] & 0xFF);
b1 <<=8;
b1 |= (buff[pos + 3] & 0xFF);
return b1;
}
// ============== Debugging code =========================
private String hex( int x ) {
// if( x < 0) x=256 + x;
String h=Integer.toHexString( x );
if( h.length() == 1 ) h = "0" + h;
return h.substring( h.length() - 2 );
}
private void hexLine( int start ) {
int pkgEnd = len + 4;
if( pkgEnd > buff.length )
pkgEnd = buff.length;
for( int i=start; i< start+16 ; i++ ) {
if( i < pkgEnd) {
if (log.isDebugEnabled())
log.debug( hex( buff[i] ) + " ");
} else {
if (log.isDebugEnabled())
log.debug( " " );
}
}
if (log.isDebugEnabled())
log.debug(" | ");
for( int i=start; i < start+16 && i < pkgEnd; i++ ) {
if( Character.isLetterOrDigit( (char)buff[i] )) {
if (log.isDebugEnabled())
log.debug( new Character((char)buff[i]) );
} else {
if (log.isDebugEnabled())
log.debug( "." );
}
}
}
public void dump(String msg) {
if (log.isDebugEnabled())
log.debug( msg + ": " + buff + " " + pos +"/" + (len + 4));
for( int j=0; j < len + 4; j+=16 )
hexLine( j );
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?