📄 charchunk.java
字号:
// if we don't have limit: makeSpace can grow as it wants if( limit < 0 ) { // assert: makeSpace made enough space sb.getChars(0, len, buff, end ); end+=len; return; } int off=0; int sbOff = off; int sbEnd = off + len; while (sbOff < sbEnd) { int d = min(limit - end, sbEnd - sbOff); sb.getChars( sbOff, sbOff+d, buff, end); sbOff += d; end += d; if (end >= limit) flushBuffer(); } } /** Append a string to the buffer */ public void append(String s, int off, int len) throws IOException { if (s==null) return; // will grow, up to limit makeSpace( len ); // if we don't have limit: makeSpace can grow as it wants if( limit < 0 ) { // assert: makeSpace made enough space s.getChars(off, off+len, buff, end ); end+=len; return; } int sOff = off; int sEnd = off + len; while (sOff < sEnd) { int d = min(limit - end, sEnd - sOff); s.getChars( sOff, sOff+d, buff, end); sOff += d; end += d; if (end >= limit) flushBuffer(); } } // -------------------- Removing data from the buffer -------------------- public int substract() throws IOException { if ((end - start) == 0) { if (in == null) return -1; int n = in.realReadChars(buff, 0, buff.length); if (n < 0) return -1; } return (buff[start++]); } public int substract(CharChunk src) throws IOException { if ((end - start) == 0) { if (in == null) return -1; int n = in.realReadChars( buff, 0, buff.length ); if (n < 0) return -1; } int len = getLength(); src.append(buff, start, len); start = end; return len; } public int substract( char src[], int off, int len ) throws IOException { if ((end - start) == 0) { if (in == null) return -1; int n = in.realReadChars( buff, 0, buff.length ); if (n < 0) return -1; } int n = len; if (len > getLength()) { n = getLength(); } System.arraycopy(buff, start, src, off, n); start += n; return n; } public void flushBuffer() throws IOException { //assert out!=null if( out==null ) { throw new IOException( "Buffer overflow, no sink " + limit + " " + buff.length ); } out.realWriteChars( 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) { char[] tmp = null; int newSize; int desiredSize=end + count; // Can't grow above the limit if( limit > 0 && desiredSize > limit -start ) { desiredSize=limit-start; } if( buff==null ) { if( desiredSize < 256 ) desiredSize=256; // take a minimum buff=new char[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 char[newSize]; } else { newSize= buff.length * 2 + count ; if( limit > 0 && newSize > limit ) newSize=limit; tmp=new char[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( buff==null ) return null; return new String( buff, start, end-start); } public int getInt() { return Ascii.parseInt(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) { char[] c = buff; int len = end-start; if (c == null || len != s.length()) { return false; } int off = start; for (int i = 0; i < len; i++) { if (c[off++] != 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) { char[] c = buff; int len = end-start; if (c == null || len != s.length()) { return false; } int off = start; for (int i = 0; i < len; i++) { if (Ascii.toLower( c[off++] ) != Ascii.toLower( s.charAt(i))) { return false; } } return true; } public boolean equals(CharChunk cc) { return equals( cc.getChars(), cc.getOffset(), cc.getLength()); } public boolean equals(char b2[], int off2, int len2) { char b1[]=buff; if( b1==null && b2==null ) return true; if (b1== null || b2==null || end-start != len2) { return false; } int off1 = start; int len=end-start; while ( len-- > 0) { if (b1[off1++] != b2[off2++]) { return false; } } return true; } public boolean equals(byte b2[], int off2, int len2) { char b1[]=buff; if( b2==null && b1==null ) return true; if (b1== null || b2==null || end-start != len2) { return false; } int off1 = start; int len=end-start; while ( len-- > 0) { if ( b1[off1++] != (char)b2[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) { char[] c = buff; int len = s.length(); if (c == null || len > end-start) { return false; } int off = start; for (int i = 0; i < len; i++) { if (c[off++] != s.charAt(i)) { return false; } } return true; } /** * Returns true if the message bytes starts with the specified string. * @param s the string */ public boolean startsWithIgnoreCase(String s, int pos) { char[] c = buff; int len = s.length(); if (c == null || len+pos > end-start) { return false; } int off = start+pos; for (int i = 0; i < len; i++) { if (Ascii.toLower( c[off++] ) != Ascii.toLower( s.charAt(i))) { return false; } } return true; } // -------------------- Hash code -------------------- // normal hash. public int hash() { int code=0; for (int i = start; i < start + end-start; i++) { code = code * 37 + buff[i]; } return code; } // hash ignoring case public int hashIgnoreCase() { int code=0; for (int i = start; i < end; i++) { code = code * 37 + Ascii.toLower(buff[i]); } return code; } public int indexOf(char c) { return indexOf( c, start); } /** * Returns true if the message bytes starts with the specified string. * @param s the string */ 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( char chars[], int off, int cend, char qq ) { while( off < cend ) { char b=chars[off]; if( b==qq ) return off; off++; } return -1; } // -------------------- utils private int min(int a, int b) { if (a < b) return a; return b; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -