diff --git a/jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/output/StringBuilderWriter.java b/jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/output/StringBuilderWriter.java new file mode 100644 index 000000000..f3243dd4b --- /dev/null +++ b/jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/output/StringBuilderWriter.java @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +//@@3RD PARTY CODE@@ + +package org.glassfish.jaxb.runtime.v2.runtime.output; + +import java.io.Serializable; +import java.io.Writer; + +/** + *
+ * Copied from + * Apache commons-io. + *
+ * {@link Writer} implementation that outputs to a {@link StringBuilder}. + *
+ * NOTE: This implementation, as an alternative to {@link java.io.StringWriter}, provides an + * un-synchronized implementation for better performance for use in a single thread. For safe usage with + * multiple {@link Thread}s, a {@link java.io.StringWriter} should be used. + */ +public class StringBuilderWriter extends Writer implements Serializable { + + private static final long serialVersionUID = -146927496096066153L; + + /** The append target. */ + private final StringBuilder builder; + + /** + * Constructs a new {@link StringBuilder} instance with default capacity. + */ + public StringBuilderWriter() { + this.builder = new StringBuilder(); + } + + /** + * Constructs a new {@link StringBuilder} instance with the specified capacity. + * + * @param capacity The initial capacity of the underlying {@link StringBuilder} + */ + public StringBuilderWriter(final int capacity) { + this.builder = new StringBuilder(capacity); + } + + /** + * Constructs a new instance with the specified {@link StringBuilder}. + * + *
If {@code builder} is null a new instance with default capacity will be created.
+ * + * @param builder The String builder. May be null. + */ + public StringBuilderWriter(final StringBuilder builder) { + this.builder = builder != null ? builder : new StringBuilder(); + } + + /** + * Appends a single character to this Writer. + * + * @param value The character to append + * @return This writer instance + */ + @Override + public Writer append(final char value) { + builder.append(value); + return this; + } + + /** + * Appends a character sequence to this Writer. + * + * @param value The character to append + * @return This writer instance + */ + @Override + public Writer append(final CharSequence value) { + builder.append(value); + return this; + } + + /** + * Appends a portion of a character sequence to the {@link StringBuilder}. + * + * @param value The character to append + * @param start The index of the first character + * @param end The index of the last character + 1 + * @return This writer instance + */ + @Override + public Writer append(final CharSequence value, final int start, final int end) { + builder.append(value, start, end); + return this; + } + + /** + * Closing this writer has no effect. + */ + @Override + public void close() { + // no-op + } + + /** + * Flushing this writer has no effect. + */ + @Override + public void flush() { + // no-op + } + + /** + * Gets the underlying builder. + * + * @return The underlying builder + */ + public StringBuilder getBuilder() { + return builder; + } + + /** + * Returns {@link StringBuilder#toString()}. + * + * @return The contents of the String builder. + */ + @Override + public String toString() { + return builder.toString(); + } + + /** + * Writes a portion of a character array to the {@link StringBuilder}. + * + * @param value The value to write + * @param offset The index of the first character + * @param length The number of characters to write + */ + @Override + public void write(final char[] value, final int offset, final int length) { + if (value != null) { + builder.append(value, offset, length); + } + } + + /** + * Writes a String to the {@link StringBuilder}. + * + * @param value The value to write + */ + @Override + public void write(final String value) { + if (value != null) { + builder.append(value); + } + } +} \ No newline at end of file diff --git a/jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/output/UTF8XmlOutput.java b/jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/output/UTF8XmlOutput.java index b1753937f..15a10f8ee 100644 --- a/jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/output/UTF8XmlOutput.java +++ b/jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/output/UTF8XmlOutput.java @@ -20,7 +20,6 @@ import javax.xml.stream.XMLStreamException; import java.io.IOException; import java.io.OutputStream; -import java.io.StringWriter; /** * {@link XmlOutput} implementation specialized for UTF-8. @@ -160,7 +159,7 @@ private int pushNsDecls() { Encoded e = prefixes[i]; - if(p.length()==0) { + if(p.isEmpty()) { e.buf = EMPTY_BYTE_ARRAY; e.len = 0; } else { @@ -186,9 +185,8 @@ protected void writeNsDecls(int base) throws IOException { protected final void writeNsDecl(int prefixIndex) throws IOException { String p = nsContext.getPrefix(prefixIndex); - if(p.length()==0) { - if(nsContext.getCurrent().isRootElement() - && nsContext.getNamespaceURI(prefixIndex).length()==0) + if(p.isEmpty()) { + if(nsContext.getCurrent().isRootElement() && nsContext.getNamespaceURI(prefixIndex).isEmpty()) return; // no point in declaring xmlns="" on the root element write(XMLNS_EQUALS); } else { @@ -288,7 +286,7 @@ public void text(Pcdata value, boolean needSP) throws IOException { private void doText(String value,boolean isAttribute) throws IOException { if (escapeHandler != null) { - StringWriter sw = new StringWriter(); + StringBuilderWriter sw = new StringBuilderWriter(); escapeHandler.escape(value.toCharArray(), 0, value.length(), isAttribute, sw); textBuffer.set(sw.toString()); } else { diff --git a/jaxb-ri/tools/config/copyright-exclude b/jaxb-ri/tools/config/copyright-exclude index eed5c6888..fefb4e4b6 100644 --- a/jaxb-ri/tools/config/copyright-exclude +++ b/jaxb-ri/tools/config/copyright-exclude @@ -39,6 +39,7 @@ jing-copying.html /rngom/src/test/java/Main.java /runtime/impl/src/main/java/com/sun/xml/bind/marshaller/XMLWriter.java +/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/output/StringBuilderWriter.java /tools/pretty-printer/src/com/sun/tools/xmlpp/DataWriter.java /tools/pretty-printer/src/com/sun/tools/xmlpp/XMLWriter.java