* * @param content a String. * @exception IOException on any I/O error */ protected void write(String content) throws IOException { if (content == null) { return; } int size = content.length(); if (tempChars == null || tempChars.length < size) { tempChars = new char[size]; } content.getChars(0, size, tempChars, 0); write(tempChars, 0, size); } /** * Writes the line separator. This invokes <code>output</code> directly * as well as setting the <code>lineLength</code> to 0. * * @since 1.3 */ protected void writeLineSeparator() throws IOException { String newline = getLineSeparator(); int length = newline.length(); if (newlineChars == null || newlineChars.length < length) { newlineChars = new char[length]; } newline.getChars(0, length, newlineChars, 0); output(newlineChars, 0, length); setCurrentLineLength(0); } /** * All write methods call into this one. If <code>getCanWrapLines()</code> * returns false, this will call <code>output</code> with each sequence * of <code>chars</code> that doesn't contain a NEWLINE, followed * by a call to <code>writeLineSeparator</code>. On the other hand, * if <code>getCanWrapLines()</code> returns true, this will split the * string, as necessary, so <code>getLineLength</code> is honored. * The only exception is if the current string contains no whitespace, * and won't fit in which case the line length will exceed * <code>getLineLength</code>. * * @since 1.3 */ protected void write(char[] chars, int startIndex, int length) throws IOException { if (!getCanWrapLines()) { // We can not break string, just track if a newline // is in it. int lastIndex = startIndex; int endIndex = startIndex + length; int newlineIndex = indexOf(chars, NEWLINE, startIndex, endIndex); while (newlineIndex != -1) { if (newlineIndex > lastIndex) { output(chars, lastIndex, newlineIndex - lastIndex); } writeLineSeparator(); lastIndex = newlineIndex + 1; newlineIndex = indexOf(chars, '\n', lastIndex, endIndex); } if (lastIndex < endIndex) { output(chars, lastIndex, endIndex - lastIndex); } } else { // We can break chars if the length exceeds maxLength. int lastIndex = startIndex; int endIndex = startIndex + length; int lineLength = getCurrentLineLength(); int maxLength = getLineLength(); while (lastIndex < endIndex) { int newlineIndex = indexOf(chars, NEWLINE, lastIndex, endIndex); boolean needsNewline = false; boolean forceNewLine = false; lineLength = getCurrentLineLength(); if (newlineIndex != -1 && (lineLength + (newlineIndex - lastIndex)) < maxLength) { if (newlineIndex > lastIndex) { output(chars, lastIndex, newlineIndex - lastIndex); } lastIndex = newlineIndex + 1; forceNewLine = true; } else if (newlineIndex == -1 && (lineLength + (endIndex - lastIndex)) < maxLength) { if (endIndex > lastIndex) { output(chars, lastIndex, endIndex - lastIndex); } lastIndex = endIndex; } else { // Need to break chars, find a place to split chars at, // from lastIndex to endIndex, // or maxLength - lineLength whichever is smaller int breakPoint = -1; int maxBreak = Math.min(endIndex - lastIndex, maxLength - lineLength - 1); int counter = 0; while (counter < maxBreak) { if (Character.isWhitespace(chars[counter + lastIndex])) { breakPoint = counter; } counter++; } if (breakPoint != -1) { // Found a place to break at. breakPoint += lastIndex + 1; output(chars, lastIndex, breakPoint - lastIndex); lastIndex = breakPoint; needsNewline = true; } else { // No where good to break. // find the next whitespace, or write out the // whole string. // maxBreak will be negative if current line too // long. counter = Math.max(0, maxBreak); maxBreak = endIndex - lastIndex; while (counter < maxBreak) { if (Character.isWhitespace(chars[counter + lastIndex])) { breakPoint = counter; break; } counter++; } if (breakPoint == -1) { output(chars, lastIndex, endIndex - lastIndex); breakPoint = endIndex; } else { breakPoint += lastIndex; if (chars[breakPoint] == NEWLINE) { output(chars, lastIndex, breakPoint++ - lastIndex); forceNewLine = true; } else { output(chars, lastIndex, ++breakPoint - lastIndex); needsNewline = true; } } lastIndex = breakPoint; } } if (forceNewLine || needsNewline || lastIndex < endIndex) { writeLineSeparator(); if (lastIndex < endIndex || !forceNewLine) { indent(); } } } } } /** * Writes out the set of attributes as " <name>=<value>" * pairs. It throws an IOException when encountered. * * @param attr an AttributeSet. * @exception IOException on any I/O error */ protected void writeAttributes(AttributeSet attr) throws IOException { Enumeration names = attr.getAttributeNames(); while (names.hasMoreElements()) { Object name = names.nextElement(); write(" " + name + "=" + attr.getAttribute(name)); } } /** * The last stop in writing out content. All the write methods eventually * make it to this method, which invokes <code>write</code> on the * Writer. * <p>This method also updates the line length based on * <code>length</code>. If this is invoked to output a newline, the * current line length will need to be reset as will no longer be * valid. If it is up to the caller to do this. Use * <code>writeLineSeparator</code> to write out a newline, which will * property update the current line length. * * @since 1.3 */ protected void output(char[] content, int start, int length) throws IOException { getWriter().write(content, start, length); setCurrentLineLength(getCurrentLineLength() + length); } /** * Support method to locate an occurence of a particular character. */ private int indexOf(char[] chars, char sChar, int startIndex, int endIndex) { while(startIndex < endIndex) { if (chars[startIndex] == sChar) { return startIndex; } startIndex++; } return -1; }}Lines 501–713 of 713. Previous lines
* * @param content a String. * @exception IOException on any I/O error */ protected void write(String content) throws IOException { if (content == null) { return; } int size = content.length(); if (tempChars == null || tempChars.length < size) { tempChars = new char[size]; } content.getChars(0, size, tempChars, 0); write(tempChars, 0, size); } /** * Writes the line separator. This invokes <code>output</code> directly * as well as setting the <code>lineLength</code> to 0. * * @since 1.3 */ protected void writeLineSeparator() throws IOException { String newline = getLineSeparator(); int length = newline.length(); if (newlineChars == null || newlineChars.length < length) { newlineChars = new char[length]; } newline.getChars(0, length, newlineChars, 0); output(newlineChars, 0, length); setCurrentLineLength(0); } /** * All write methods call into this one. If <code>getCanWrapLines()</code> * returns false, this will call <code>output</code> with each sequence * of <code>chars</code> that doesn't contain a NEWLINE, followed * by a call to <code>writeLineSeparator</code>. On the other hand, * if <code>getCanWrapLines()</code> returns true, this will split the * string, as necessary, so <code>getLineLength</code> is honored. * The only exception is if the current string contains no whitespace, * and won't fit in which case the line length will exceed * <code>getLineLength</code>. * * @since 1.3 */ protected void write(char[] chars, int startIndex, int length) throws IOException { if (!getCanWrapLines()) { // We can not break string, just track if a newline // is in it. int lastIndex = startIndex; int endIndex = startIndex + length; int newlineIndex = indexOf(chars, NEWLINE, startIndex, endIndex); while (newlineIndex != -1) { if (newlineIndex > lastIndex) { output(chars, lastIndex, newlineIndex - lastIndex); } writeLineSeparator(); lastIndex = newlineIndex + 1; newlineIndex = indexOf(chars, '\n', lastIndex, endIndex); } if (lastIndex < endIndex) { output(chars, lastIndex, endIndex - lastIndex); } } else { // We can break chars if the length exceeds maxLength. int lastIndex = startIndex; int endIndex = startIndex + length; int lineLength = getCurrentLineLength(); int maxLength = getLineLength(); while (lastIndex < endIndex) { int newlineIndex = indexOf(chars, NEWLINE, lastIndex, endIndex); boolean needsNewline = false; boolean forceNewLine = false; lineLength = getCurrentLineLength(); if (newlineIndex != -1 && (lineLength + (newlineIndex - lastIndex)) < maxLength) { if (newlineIndex > lastIndex) { output(chars, lastIndex, newlineIndex - lastIndex); } lastIndex = newlineIndex + 1; forceNewLine = true; } else if (newlineIndex == -1 && (lineLength + (endIndex - lastIndex)) < maxLength) { if (endIndex > lastIndex) { output(chars, lastIndex, endIndex - lastIndex); } lastIndex = endIndex; } else { // Need to break chars, find a place to split chars at, // from lastIndex to endIndex, // or maxLength - lineLength whichever is smaller int breakPoint = -1; int maxBreak = Math.min(endIndex - lastIndex, maxLength - lineLength - 1); int counter = 0; while (counter < maxBreak) { if (Character.isWhitespace(chars[counter + lastIndex])) { breakPoint = counter; } counter++; } if (breakPoint != -1) { // Found a place to break at. breakPoint += lastIndex + 1; output(chars, lastIndex, breakPoint - lastIndex); lastIndex = breakPoint; needsNewline = true; } else { // No where good to break. // find the next whitespace, or write out the // whole string. // maxBreak will be negative if current line too // long. counter = Math.max(0, maxBreak); maxBreak = endIndex - lastIndex; while (counter < maxBreak) { if (Character.isWhitespace(chars[counter + lastIndex])) { breakPoint = counter; break; } counter++; } if (breakPoint == -1) { output(chars, lastIndex, endIndex - lastIndex); breakPoint = endIndex; } else { breakPoint += lastIndex; if (chars[breakPoint] == NEWLINE) { output(chars, lastIndex, breakPoint++ - lastIndex); forceNewLine = true; } else { output(chars, lastIndex, ++breakPoint - lastIndex); needsNewline = true; } } lastIndex = breakPoint; } } if (forceNewLine || needsNewline || lastIndex < endIndex) { writeLineSeparator(); if (lastIndex < endIndex || !forceNewLine) { indent(); } } } } } /** * Writes out the set of attributes as " <name>=<value>" * pairs. It throws an IOException when encountered. * * @param attr an AttributeSet. * @exception IOException on any I/O error */ protected void writeAttributes(AttributeSet attr) throws IOException { Enumeration names = attr.getAttributeNames(); while (names.hasMoreElements()) { Object name = names.nextElement(); write(" " + name + "=" + attr.getAttribute(name)); } } /** * The last stop in writing out content. All the write methods eventually * make it to this method, which invokes <code>write</code> on the * Writer. * <p>This method also updates the line length based on * <code>length</code>. If this is invoked to output a newline, the * current line length will need to be reset as will no longer be * valid. If it is up to the caller to do this. Use * <code>writeLineSeparator</code> to write out a newline, which will * property update the current line length. * * @since 1.3 */ protected void output(char[] content, int start, int length) throws IOException { getWriter().write(content, start, length); setCurrentLineLength(getCurrentLineLength() + length); } /** * Support method to locate an occurence of a particular character. */ private int indexOf(char[] chars, char sChar, int startIndex, int endIndex) { while(startIndex < endIndex) { if (chars[startIndex] == sChar) { return startIndex; } startIndex++; } return -1; } }