📄 messagebytes.java
字号:
return false; } } /** * 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) { switch (type) { case T_STR: if( strValue==null && s!=null) return false; return strValue.equalsIgnoreCase( s ); case T_CHARS: return charC.equalsIgnoreCase( s ); case T_BYTES: return byteC.equalsIgnoreCase( s ); default: return false; } } public boolean equals(MessageBytes mb) { switch (type) { case T_STR: return mb.equals( strValue ); } if( mb.type != T_CHARS && mb.type!= T_BYTES ) { // it's a string or int/date string value return equals( mb.toString() ); } // mb is either CHARS or BYTES. // this is either CHARS or BYTES // Deal with the 4 cases ( in fact 3, one is simetric) if( mb.type == T_CHARS && type==T_CHARS ) { return charC.equals( mb.charC ); } if( mb.type==T_BYTES && type== T_BYTES ) { return byteC.equals( mb.byteC ); } if( mb.type== T_CHARS && type== T_BYTES ) { return byteC.equals( mb.charC ); } if( mb.type== T_BYTES && type== T_CHARS ) { return mb.byteC.equals( charC ); } // can't happen return true; } /** * Returns true if the message bytes starts with the specified string. * @param s the string */ public boolean startsWith(String s) { switch (type) { case T_STR: return strValue.startsWith( s ); case T_CHARS: return charC.startsWith( s ); case T_BYTES: return byteC.startsWith( s ); default: return false; } } /** * Returns true if the message bytes starts with the specified string. * @param s the string */ public boolean startsWithIgnoreCase(String s, int pos) { switch (type) { case T_STR: if( strValue==null ) return false; if( strValue.length() < pos + s.length() ) return false; for( int i=0; i<s.length(); i++ ) { if( Ascii.toLower( s.charAt( i ) ) != Ascii.toLower( strValue.charAt( pos + i ))) { return false; } } return true; case T_CHARS: return charC.startsWithIgnoreCase( s, pos ); case T_BYTES: return byteC.startsWithIgnoreCase( s, pos ); default: return false; } } // -------------------- Hash code -------------------- public int hashCode() { if( hasHashCode ) return hashCode; int code = 0; if( caseSensitive ) code=hash(); else code=hashIgnoreCase(); hashCode=code; hasHashCode=true; return code; } // normal hash. private int 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: return 0; } } // hash ignoring case private int hashIgnoreCase() { int code=0; switch (type) { case T_STR: for (int i = 0; i < strValue.length(); i++) { code = code * 37 + Ascii.toLower(strValue.charAt( i )); } return code; case T_CHARS: return charC.hashIgnoreCase(); case T_BYTES: return byteC.hashIgnoreCase(); default: return 0; } } public int indexOf(char c) { return indexOf( c, 0); } // Inefficient initial implementation. Will be replaced on the next // round of tune-up public int indexOf(String s, int starting) { toString(); return strValue.indexOf( s, starting ); } // Inefficient initial implementation. Will be replaced on the next // round of tune-up public int indexOf(String s) { return indexOf( s, 0 ); } public int indexOfIgnoreCase(String s, int starting) { toString(); String upper=strValue.toUpperCase(); String sU=s.toUpperCase(); return upper.indexOf( sU, starting ); } /** * Returns true if the message bytes starts with the specified string. * @param s the string */ public int indexOf(char c, int starting) { switch (type) { case T_STR: return strValue.indexOf( c, starting ); case T_CHARS: return charC.indexOf( c, starting); case T_BYTES: return byteC.indexOf( c, starting ); default: return -1; } } /** Copy the src into this MessageBytes, allocating more space if * needed */ public void duplicate( MessageBytes src ) throws IOException { switch( src.getType() ) { case MessageBytes.T_BYTES: type=T_BYTES; ByteChunk bc=src.getByteChunk(); byteC.allocate( bc.getLength(), -1 ); byteC.append( bc ); break; case MessageBytes.T_CHARS: type=T_CHARS; CharChunk cc=src.getCharChunk(); charC.allocate( cc.getLength(), -1 ); charC.append( cc ); break; case MessageBytes.T_STR: type=T_STR; String sc=src.getString(); this.setString( sc ); break; } } // -------------------- Deprecated code -------------------- // efficient int and date // XXX used only for headers - shouldn't be // stored here. private int intValue; private boolean hasIntValue=false; private Date dateValue; private boolean hasDateValue=false; /** * @deprecated The buffer are general purpose, caching for headers should * be done in headers. The second parameter allows us to pass a date format * instance to avoid synchronization problems. */ public void setTime(long t, DateFormat df) { // XXX replace it with a byte[] tool recycle(); if( dateValue==null) dateValue=new Date(t); else dateValue.setTime(t); if( df==null ) strValue=DateTool.format1123(dateValue); else strValue=DateTool.format1123(dateValue,df); hasStrValue=true; hasDateValue=true; type=T_STR; } public void setTime(long t) { setTime( t, null ); } /** Set the buffer to the representation of an int * @deprecated The buffer are general purpose, caching for headers should * be done in headers */ public void setInt(int i) { // XXX replace it with a byte[] tool recycle(); strValue=String.valueOf( i ); intValue=i; hasIntValue=true; hasStrValue=true; type=T_STR; } /** * @deprecated The buffer are general purpose, caching for headers should * be done in headers */ public long getTime() { if( hasDateValue ) { if( dateValue==null) return -1; return dateValue.getTime(); } long l=DateTool.parseDate( this ); if( dateValue==null) dateValue=new Date(l); else dateValue.setTime(l); hasDateValue=true; return l; } // Used for headers conversion /** Convert the buffer to an int, cache the value * @deprecated The buffer are general purpose, caching for headers should * be done in headers */ public int getInt() { if( hasIntValue ) return intValue; switch (type) { case T_BYTES: intValue=byteC.getInt(); break; default: intValue=Integer.parseInt(toString()); } hasIntValue=true; return intValue; } // -------------------- Future may be different -------------------- private static MessageBytesFactory factory=new MessageBytesFactory(); public static void setFactory( MessageBytesFactory mbf ) { factory=mbf; } public static class MessageBytesFactory { protected MessageBytesFactory() { } public MessageBytes newInstance() { return new MessageBytes(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -