📄 ajp13packet.java
字号:
// // XXX i don't have OutputBuffer in tc4... ks. // fix this later... // byte[] bytes = str.getBytes(encoding); appendBytes(bytes, 0, bytes.length); /* XXX XXX XXX XXX Try to add it back. int strStart=pos; // This replaces the old ( buggy and slow ) str.length() // and str.getBytes(). str.length() is chars, may be != bytes // and getBytes is _very_ slow. // XXX setEncoding !!! ob.setByteOff( pos+2 ); try { ob.write( str ); ob.flushChars(); } catch( IOException ex ) { ex.printStackTrace(); } int strEnd=ob.getByteOff(); buff[strEnd]=0; // The \0 terminator int strLen=strEnd-strStart; setInt( pos, strEnd - strStart ); pos += strLen + 3; */ } /** * 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 len The number of bytes to copy. */ public void appendBytes( byte b[], int off, int numBytes ) { appendInt( numBytes ); if( pos + numBytes >= buff.length ) { System.out.println("Buffer overflow " + buff.length + " " + pos + " " + numBytes ); // XXX Log } 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 len The number of bytes to copy. */ public void appendXBytes(byte[] b, int off, int numBytes) { if( pos + numBytes > buff.length ) { System.out.println("appendXBytes - Buffer overflow " + buff.length + " " + pos + " " + numBytes ); // XXX Log } 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) ) { // System.out.println("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)? System.out.println("XXX Assert failed, buff too small "); } if( (length == 0xFFFF) || (length == -1) ) { System.out.println("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)? System.out.println("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) System.out.print( hex( buff[i] ) + " "); else System.out.print( " " ); } System.out.print(" | "); for( int i=start; i < start+16 && i < pkgEnd; i++ ) { if( Character.isLetterOrDigit( (char)buff[i] )) System.out.print( new Character((char)buff[i]) ); else System.out.print( "." ); } System.out.println(); } public void dump(String msg) { System.out.println( msg + ": " + buff + " " + pos +"/" + (len + 4)); for( int j=0; j < len + 4; j+=16 ) hexLine( j ); System.out.println(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -