📄 util.java
字号:
// HTML 4.0, section B.7.1: ampersands followed by // an open brace don't get escaped if ((i + 1 < end) && (text[i + 1] == '{')) { out.write(ch); } else { out.write("&"); } } else if (ch == '"') { buffIndex = flushBuffer(out, buff, buffIndex); out.write("""); } else { buffIndex = addToBuffer(out, buff, buffIndex, buffLength, ch); } } } else { buffIndex = flushBuffer(out, buff, buffIndex); // Double-byte characters to encode. // PENDING: when outputting to an encoding that // supports double-byte characters (UTF-8, for example), // we should not be encoding _writeDecRef(out, ch); } } flushBuffer(out, buff, buffIndex); } //------------------------------------------------- // The following methods include the handling of // escape characters.... //------------------------------------------------- static public void writeText(Writer out, char[] buffer, char[] text) throws IOException { writeText(out, buffer, text, 0, text.length); } /** * Write char array text. Note that this code is duplicated below * for Strings - change both places if you make any changes!!! */ static public void writeText(Writer out, char[] buff, char[] text, int start, int length) throws IOException { int buffLength = buff.length; int buffIndex = 0; int end = start + length; for (int i = start; i < end; i++) { char ch = text[i]; // Tilde or less... if (ch < 0xA0) { // If "?" or over, no escaping is needed (this covers // most of the Latin alphabet) if (ch >= 0x3f) { buffIndex = addToBuffer(out, buff, buffIndex, buffLength, ch); } else if (ch >= 0x27) { // If above "'"... // If between "'" and ";", no escaping is needed if (ch < 0x3c) { buffIndex = addToBuffer(out, buff, buffIndex, buffLength, ch); } else if (ch == '<') { buffIndex = flushBuffer(out, buff, buffIndex); out.write("<"); } else if (ch == '>') { buffIndex = flushBuffer(out, buff, buffIndex); out.write(">"); } else { buffIndex = addToBuffer(out, buff, buffIndex, buffLength, ch); } } else { if (ch == '&') { buffIndex = flushBuffer(out, buff, buffIndex); out.write("&"); } else { buffIndex = addToBuffer(out, buff, buffIndex, buffLength, ch); } } } else { // Double-byte characters to encode. // PENDING: when outputting to an encoding that // supports double-byte characters (UTF-8, for example), // we should not be encoding buffIndex = flushBuffer(out, buff, buffIndex); _writeDecRef(out, ch); } } flushBuffer(out, buff, buffIndex); } /** * Write String text. Note that this code is duplicated above for * character arrays - change both places if you make any changes!!! */ static public void writeText(Writer out, char[] buff, String text) throws IOException { int buffLength = buff.length; int buffIndex = 0; int length = text.length(); for (int i = 0; i < length; i++) { char ch = text.charAt(i); // Tilde or less... if (ch < 0xA0) { // If "?" or over, no escaping is needed (this covers // most of the Latin alphabet) if (ch >= 0x3f) { buffIndex = addToBuffer(out, buff, buffIndex, buffLength, ch); } else if (ch >= 0x27) { // If above "'"... // If between "'" and ";", no escaping is needed if (ch < 0x3c) { buffIndex = addToBuffer(out, buff, buffIndex, buffLength, ch); } else if (ch == '<') { buffIndex = flushBuffer(out, buff, buffIndex); out.write("<"); } else if (ch == '>') { buffIndex = flushBuffer(out, buff, buffIndex); out.write(">"); } else { buffIndex = addToBuffer(out, buff, buffIndex, buffLength, ch); } } else { if (ch == '&') { buffIndex = flushBuffer(out, buff, buffIndex); out.write("&"); } else { buffIndex = addToBuffer(out, buff, buffIndex, buffLength, ch); } } } else { // Double-byte characters to encode. // PENDING: when outputting to an encoding that // supports double-byte characters (UTF-8, for example), // we should not be encoding buffIndex = flushBuffer(out, buff, buffIndex); _writeDecRef(out, ch); } } flushBuffer(out, buff, buffIndex); } /** * Writes a character as a decimal escape. Hex escapes are smaller than * the decimal version, but Netscape didn't support hex escapes until * 4.7.4. */ static private void _writeDecRef(Writer out, char ch) throws IOException { if (ch == '\u20ac') { out.write("€"); return; } out.write("&#"); // Formerly used String.valueOf(). This version tests out // about 40% faster in a microbenchmark (and on systems where GC is // going gonzo, it should be even better) int i = (int) ch; if (i > 10000) { out.write('0' + (i / 10000)); i = i % 10000; out.write('0' + (i / 1000)); i = i % 1000; out.write('0' + (i / 100)); i = i % 100; out.write('0' + (i / 10)); i = i % 10; out.write('0' + i); } else if (i > 1000) { out.write('0' + (i / 1000)); i = i % 1000; out.write('0' + (i / 100)); i = i % 100; out.write('0' + (i / 10)); i = i % 10; out.write('0' + i); } else { out.write('0' + (i / 100)); i = i % 100; out.write('0' + (i / 10)); i = i % 10; out.write('0' + i); } out.write(';'); } /** * Flush the contents of the buffer to the output stream * and return the reset buffer index */ private static int flushBuffer(Writer out, char[] buffer, int bufferIndex) throws IOException { if (bufferIndex > 0) { out.write(buffer, 0, bufferIndex); } return 0; } /** * Add a character to the buffer, flushing the buffer if the buffer is * full, and returning the new buffer index */ private static int addToBuffer(Writer out, char[] buffer, int bufferIndex, int bufferLength, char ch) throws IOException { if (bufferIndex >= bufferLength) { out.write(buffer, 0, bufferIndex); bufferIndex = 0; } buffer[bufferIndex] = ch; return bufferIndex + 1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -