faststringbuffer.java

来自「JAVA 所有包」· Java 代码 · 共 1,295 行 · 第 1/3 页

JAVA
1,295
字号
        char[][] newarray = new char[i + 16][];        System.arraycopy(m_array, 0, newarray, 0, i);        m_array = newarray;      }      // Advance one chunk      chunk = m_array[++m_lastChunk];      if (chunk == null)      {        // Hierarchical encapsulation        if (m_lastChunk == 1 << m_rebundleBits                && m_chunkBits < m_maxChunkBits)        {          // Should do all the work of both encapsulating          // existing data and establishing new sizes/offsets          m_innerFSB = new FastStringBuffer(this);        }        // Add a chunk.        chunk = m_array[m_lastChunk] = new char[m_chunkSize];      }      m_firstFree = 0;    }    // Space exists in the chunk. Append the character.    chunk[m_firstFree++] = value;  }  /**   * Append the contents of a String onto the FastStringBuffer,   * growing the storage if necessary.   * <p>   * NOTE THAT after calling append(), previously obtained   * references to m_array[] may no longer be valid.   *   * @param value String whose contents are to be appended.   */  public final void append(String value)  {    if (value == null)       return;    int strlen = value.length();    if (0 == strlen)      return;    int copyfrom = 0;    char[] chunk = m_array[m_lastChunk];    int available = m_chunkSize - m_firstFree;    // Repeat while data remains to be copied    while (strlen > 0)    {      // Copy what fits      if (available > strlen)        available = strlen;      value.getChars(copyfrom, copyfrom + available, m_array[m_lastChunk],                     m_firstFree);      strlen -= available;      copyfrom += available;      // If there's more left, allocate another chunk and continue      if (strlen > 0)      {        // Extend array?        int i = m_array.length;        if (m_lastChunk + 1 == i)        {          char[][] newarray = new char[i + 16][];          System.arraycopy(m_array, 0, newarray, 0, i);          m_array = newarray;        }        // Advance one chunk        chunk = m_array[++m_lastChunk];        if (chunk == null)        {          // Hierarchical encapsulation          if (m_lastChunk == 1 << m_rebundleBits                  && m_chunkBits < m_maxChunkBits)          {            // Should do all the work of both encapsulating            // existing data and establishing new sizes/offsets            m_innerFSB = new FastStringBuffer(this);          }          // Add a chunk.           chunk = m_array[m_lastChunk] = new char[m_chunkSize];        }        available = m_chunkSize;        m_firstFree = 0;      }    }    // Adjust the insert point in the last chunk, when we've reached it.    m_firstFree += available;  }  /**   * Append the contents of a StringBuffer onto the FastStringBuffer,   * growing the storage if necessary.   * <p>   * NOTE THAT after calling append(), previously obtained   * references to m_array[] may no longer be valid.   *   * @param value StringBuffer whose contents are to be appended.   */  public final void append(StringBuffer value)  {    if (value == null)       return;    int strlen = value.length();    if (0 == strlen)      return;    int copyfrom = 0;    char[] chunk = m_array[m_lastChunk];    int available = m_chunkSize - m_firstFree;    // Repeat while data remains to be copied    while (strlen > 0)    {      // Copy what fits      if (available > strlen)        available = strlen;      value.getChars(copyfrom, copyfrom + available, m_array[m_lastChunk],                     m_firstFree);      strlen -= available;      copyfrom += available;      // If there's more left, allocate another chunk and continue      if (strlen > 0)      {        // Extend array?        int i = m_array.length;        if (m_lastChunk + 1 == i)        {          char[][] newarray = new char[i + 16][];          System.arraycopy(m_array, 0, newarray, 0, i);          m_array = newarray;        }        // Advance one chunk        chunk = m_array[++m_lastChunk];        if (chunk == null)        {          // Hierarchical encapsulation          if (m_lastChunk == 1 << m_rebundleBits                  && m_chunkBits < m_maxChunkBits)          {            // Should do all the work of both encapsulating            // existing data and establishing new sizes/offsets            m_innerFSB = new FastStringBuffer(this);          }          // Add a chunk.          chunk = m_array[m_lastChunk] = new char[m_chunkSize];        }        available = m_chunkSize;        m_firstFree = 0;      }    }    // Adjust the insert point in the last chunk, when we've reached it.    m_firstFree += available;  }  /**   * Append part of the contents of a Character Array onto the   * FastStringBuffer,  growing the storage if necessary.   * <p>   * NOTE THAT after calling append(), previously obtained   * references to m_array[] may no longer be valid.   *   * @param chars character array from which data is to be copied   * @param start offset in chars of first character to be copied,   * zero-based.   * @param length number of characters to be copied   */  public final void append(char[] chars, int start, int length)  {    int strlen = length;    if (0 == strlen)      return;    int copyfrom = start;    char[] chunk = m_array[m_lastChunk];    int available = m_chunkSize - m_firstFree;    // Repeat while data remains to be copied    while (strlen > 0)    {      // Copy what fits      if (available > strlen)        available = strlen;      System.arraycopy(chars, copyfrom, m_array[m_lastChunk], m_firstFree,                       available);      strlen -= available;      copyfrom += available;      // If there's more left, allocate another chunk and continue      if (strlen > 0)      {        // Extend array?        int i = m_array.length;        if (m_lastChunk + 1 == i)        {          char[][] newarray = new char[i + 16][];          System.arraycopy(m_array, 0, newarray, 0, i);          m_array = newarray;        }        // Advance one chunk        chunk = m_array[++m_lastChunk];        if (chunk == null)        {          // Hierarchical encapsulation          if (m_lastChunk == 1 << m_rebundleBits                  && m_chunkBits < m_maxChunkBits)          {            // Should do all the work of both encapsulating            // existing data and establishing new sizes/offsets            m_innerFSB = new FastStringBuffer(this);          }          // Add a chunk.          chunk = m_array[m_lastChunk] = new char[m_chunkSize];        }        available = m_chunkSize;        m_firstFree = 0;      }    }    // Adjust the insert point in the last chunk, when we've reached it.    m_firstFree += available;  }  /**   * Append the contents of another FastStringBuffer onto   * this FastStringBuffer, growing the storage if necessary.   * <p>   * NOTE THAT after calling append(), previously obtained   * references to m_array[] may no longer be valid.   *   * @param value FastStringBuffer whose contents are   * to be appended.   */  public final void append(FastStringBuffer value)  {    // Complicating factor here is that the two buffers may use    // different chunk sizes, and even if they're the same we're    // probably on a different alignment due to previously appended    // data. We have to work through the source in bite-sized chunks.    if (value == null)       return;    int strlen = value.length();    if (0 == strlen)      return;    int copyfrom = 0;    char[] chunk = m_array[m_lastChunk];    int available = m_chunkSize - m_firstFree;    // Repeat while data remains to be copied    while (strlen > 0)    {      // Copy what fits      if (available > strlen)        available = strlen;      int sourcechunk = (copyfrom + value.m_chunkSize - 1)                        >>> value.m_chunkBits;      int sourcecolumn = copyfrom & value.m_chunkMask;      int runlength = value.m_chunkSize - sourcecolumn;      if (runlength > available)        runlength = available;      System.arraycopy(value.m_array[sourcechunk], sourcecolumn,                       m_array[m_lastChunk], m_firstFree, runlength);      if (runlength != available)        System.arraycopy(value.m_array[sourcechunk + 1], 0,                         m_array[m_lastChunk], m_firstFree + runlength,                         available - runlength);      strlen -= available;      copyfrom += available;      // If there's more left, allocate another chunk and continue      if (strlen > 0)      {        // Extend array?        int i = m_array.length;        if (m_lastChunk + 1 == i)        {          char[][] newarray = new char[i + 16][];          System.arraycopy(m_array, 0, newarray, 0, i);          m_array = newarray;        }        // Advance one chunk        chunk = m_array[++m_lastChunk];        if (chunk == null)        {          // Hierarchical encapsulation          if (m_lastChunk == 1 << m_rebundleBits                  && m_chunkBits < m_maxChunkBits)          {            // Should do all the work of both encapsulating            // existing data and establishing new sizes/offsets            m_innerFSB = new FastStringBuffer(this);          }          // Add a chunk.           chunk = m_array[m_lastChunk] = new char[m_chunkSize];        }        available = m_chunkSize;        m_firstFree = 0;      }    }    // Adjust the insert point in the last chunk, when we've reached it.    m_firstFree += available;  }  /**   * @return true if the specified range of characters are all whitespace,   * as defined by XMLCharacterRecognizer.   * <p>   * CURRENTLY DOES NOT CHECK FOR OUT-OF-RANGE.   *   * @param start Offset of first character in the range.   * @param length Number of characters to send.   */  public boolean isWhitespace(int start, int length)  {    int sourcechunk = start >>> m_chunkBits;    int sourcecolumn = start & m_chunkMask;    int available = m_chunkSize - sourcecolumn;    boolean chunkOK;    while (length > 0)    {      int runlength = (length <= available) ? length : available;      if (sourcechunk == 0 && m_innerFSB != null)        chunkOK = m_innerFSB.isWhitespace(sourcecolumn, runlength);      else        chunkOK = com.sun.org.apache.xml.internal.utils.XMLCharacterRecognizer.isWhiteSpace(          m_array[sourcechunk], sourcecolumn, runlength);      if (!chunkOK)        return false;      length -= runlength;      ++sourcechunk;      sourcecolumn = 0;      available = m_chunkSize;    }    return true;  }  /**   * @param start Offset of first character in the range.   * @param length Number of characters to send.   * @return a new String object initialized from the specified range of   * characters.   */  public String getString(int start, int length)  {    int startColumn = start & m_chunkMask;    int startChunk = start >>> m_chunkBits;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?