writertoutf8buffered.java

来自「JAVA 所有包」· Java 代码 · 共 502 行 · 第 1/2 页

JAVA
502
字号
    final byte[] buf_loc = m_outputBytes; // local reference for faster access    int count_loc = count;      // local integer for faster access    int i = start;    {        /* This block could be omitted and the code would produce         * the same result. But this block exists to give the JIT         * a better chance of optimizing a tight and common loop which         * occurs when writing out ASCII characters.          */         char c;        for(; i < n && (c = chars[i])< 0x80 ; i++ )            buf_loc[count_loc++] = (byte)c;    }    for (; i < n; i++)    {      final char c = chars[i];      if (c < 0x80)        buf_loc[count_loc++] = (byte) (c);      else if (c < 0x800)      {        buf_loc[count_loc++] = (byte) (0xc0 + (c >> 6));        buf_loc[count_loc++] = (byte) (0x80 + (c & 0x3f));      }      /**        * The following else if condition is added to support XML 1.1 Characters for         * UTF-8:   [1111 0uuu] [10uu zzzz] [10yy yyyy] [10xx xxxx]*        * Unicode: [1101 10ww] [wwzz zzyy] (high surrogate)        *          [1101 11yy] [yyxx xxxx] (low surrogate)        *          * uuuuu = wwww + 1        */      else if (c >= 0xD800 && c <= 0xDBFF)       {          char high, low;          high = c;          i++;          low = chars[i];          buf_loc[count_loc++] = (byte) (0xF0 | (((high + 0x40) >> 8) & 0xf0));          buf_loc[count_loc++] = (byte) (0x80 | (((high + 0x40) >> 2) & 0x3f));          buf_loc[count_loc++] = (byte) (0x80 | ((low >> 6) & 0x0f) + ((high << 4) & 0x30));          buf_loc[count_loc++] = (byte) (0x80 | (low & 0x3f));      }      else      {        buf_loc[count_loc++] = (byte) (0xe0 + (c >> 12));        buf_loc[count_loc++] = (byte) (0x80 + ((c >> 6) & 0x3f));        buf_loc[count_loc++] = (byte) (0x80 + (c & 0x3f));      }    }    // Store the local integer back into the instance variable    count = count_loc;  }  /**   * Write a string.   *   * @param  s  String to be written   *   * @exception  IOException  If an I/O error occurs   */  public void write(final String s) throws IOException  {    // We multiply the length by three since this is the maximum length    // of the characters that we can put into the buffer.  It is possible    // for each Unicode character to expand to three bytes.    final int length = s.length();    int lengthx3 = 3*length;    if (lengthx3 >= BYTES_MAX - count)    {      // The requested length is greater than the unused part of the buffer      flushBuffer();      if (lengthx3 > BYTES_MAX)      {        /*         * The requested length exceeds the size of the buffer,         * so break it up in chunks that don't exceed the buffer size.         */         final int start = 0;         int split = length/CHARS_MAX;          final int chunks;         if (split > 1)             chunks = split;         else             chunks = 2;         int end_chunk = 0;         for (int chunk = 1; chunk <= chunks; chunk++)         {             int start_chunk = end_chunk;             end_chunk = start + (int) ((((long) length) * chunk) / chunks);             s.getChars(start_chunk,end_chunk, m_inputChars,0);             int len_chunk = (end_chunk - start_chunk);             // Adjust the end of the chunk if it ends on a high char              // of a Unicode surrogate pair and low char of the pair             // is not going to be in the same chunk             final char c = m_inputChars[len_chunk - 1];             if (c >= 0xD800 && c <= 0xDBFF) {                 // Exclude char in this chunk,                  // to avoid spanning a Unicode character                  // that is in two Java chars as a high/low surrogate                 end_chunk--;                 len_chunk--;                 if (chunk == chunks) {                     /* TODO: error message needed.                      * The String incorrectly ends in a high char                      * of a high/low surrogate pair, but there is                      * no corresponding low as the high is the last char                      * Recover by ignoring this last char.                      */                 }             }             this.write(m_inputChars,0, len_chunk);         }         return;      }    }    s.getChars(0, length , m_inputChars, 0);    final char[] chars = m_inputChars;    final int n = length;    final byte[] buf_loc = m_outputBytes; // local reference for faster access    int count_loc = count;      // local integer for faster access    int i = 0;    {        /* This block could be omitted and the code would produce         * the same result. But this block exists to give the JIT         * a better chance of optimizing a tight and common loop which         * occurs when writing out ASCII characters.          */         char c;        for(; i < n && (c = chars[i])< 0x80 ; i++ )            buf_loc[count_loc++] = (byte)c;    }    for (; i < n; i++)    {      final char c = chars[i];      if (c < 0x80)        buf_loc[count_loc++] = (byte) (c);      else if (c < 0x800)      {        buf_loc[count_loc++] = (byte) (0xc0 + (c >> 6));        buf_loc[count_loc++] = (byte) (0x80 + (c & 0x3f));      }    /**      * The following else if condition is added to support XML 1.1 Characters for       * UTF-8:   [1111 0uuu] [10uu zzzz] [10yy yyyy] [10xx xxxx]*      * Unicode: [1101 10ww] [wwzz zzyy] (high surrogate)      *          [1101 11yy] [yyxx xxxx] (low surrogate)      *          * uuuuu = wwww + 1      */    else if (c >= 0xD800 && c <= 0xDBFF)     {        char high, low;        high = c;        i++;        low = chars[i];        buf_loc[count_loc++] = (byte) (0xF0 | (((high + 0x40) >> 8) & 0xf0));        buf_loc[count_loc++] = (byte) (0x80 | (((high + 0x40) >> 2) & 0x3f));        buf_loc[count_loc++] = (byte) (0x80 | ((low >> 6) & 0x0f) + ((high << 4) & 0x30));        buf_loc[count_loc++] = (byte) (0x80 | (low & 0x3f));    }      else      {        buf_loc[count_loc++] = (byte) (0xe0 + (c >> 12));        buf_loc[count_loc++] = (byte) (0x80 + ((c >> 6) & 0x3f));        buf_loc[count_loc++] = (byte) (0x80 + (c & 0x3f));      }    }    // Store the local integer back into the instance variable    count = count_loc;  }  /**   * Flush the internal buffer   *   * @throws IOException   */  public void flushBuffer() throws IOException  {    if (count > 0)    {      m_os.write(m_outputBytes, 0, count);      count = 0;    }  }  /**   * Flush the stream.  If the stream has saved any characters from the   * various write() methods in a buffer, write them immediately to their   * intended destination.  Then, if that destination is another character or   * byte stream, flush it.  Thus one flush() invocation will flush all the   * buffers in a chain of Writers and OutputStreams.   *   * @exception  IOException  If an I/O error occurs   *   * @throws java.io.IOException   */  public void flush() throws java.io.IOException  {    flushBuffer();    m_os.flush();  }  /**   * Close the stream, flushing it first.  Once a stream has been closed,   * further write() or flush() invocations will cause an IOException to be   * thrown.  Closing a previously-closed stream, however, has no effect.   *   * @exception  IOException  If an I/O error occurs   *   * @throws java.io.IOException   */  public void close() throws java.io.IOException  {    flushBuffer();    m_os.close();  }  /**   * Get the output stream where the events will be serialized to.   *   * @return reference to the result stream, or null of only a writer was   * set.   */  public OutputStream getOutputStream()  {    return m_os;  }  public Writer getWriter()  {    // Only one of getWriter() or getOutputStream() can return null    // This type of writer wraps an OutputStream, not a Writer.    return null;  }}

⌨️ 快捷键说明

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