Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/com/sun/xml/internal/stream/writers/XMLWriter.java
48527 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.xml.internal.stream.writers;2627import java.io.IOException;28import java.io.Writer;29import com.sun.org.apache.xerces.internal.util.XMLStringBuffer;3031/**32* XMLWriter33*34* <code>XMLWriter</code> is not thread safe.35*36* For efficiency this writer buffers the input. Use <code>flush()</code> function37* to explicitly write the data to underlying stream.38*39* This writer is designed in such a way that it atleast buffers the input to the40* <code>size</code> specified. Unless <code>flush</code> is called, it guarantees that41* data in chunks of size equal to or more than <code>size</code> specified will be written.42*43*44* <code>XMLWriter</code> instance can be reused. <code>setWriter()</code> internally clears the45* buffer and stores the reference to newly supplied <code>Writer</code> instance.46*47* @author Neeraj Bajaj Sun Microsystems, inc.48*/49public class XMLWriter extends Writer {5051private Writer writer ;52private int size ;53//keep the size of internal buffer more than 'size' required to avoid resizing54private XMLStringBuffer buffer = new XMLStringBuffer(6 * (1 << 11) ); // 6 KB55private static final int THRESHHOLD_LENGTH = 1 << 12 ; // 4 KB56private static final boolean DEBUG = false;5758/** Creates the instance of <code>XMLWriter</code>59*/6061public XMLWriter(Writer writer){62this(writer, THRESHHOLD_LENGTH);63}6465/**66* Creates the instnace of <code>XMLWriter</code>.67*68* atleast buffers the input to the69* <code>size</code> specified.70*/71public XMLWriter(Writer writer, int size){72this.writer = writer ;73this.size = size;74}7576/**77* Write a single character. The character to be written is contained in78* the 16 low-order bits of the given integer value; the 16 high-order bits79* are ignored.80*81* <p> Subclasses that intend to support efficient single-character output82* should override this method.83*84* @param c int specifying a character to be written.85* @exception IOException If an I/O error occurs86*/8788public void write(int c) throws IOException {89ensureOpen();90buffer.append((char)c);91conditionalWrite();92}9394/**95* Write an array of characters.96*97* @param cbuf Array of characters to be written98*99* @exception IOException If an I/O error occurs100*/101102public void write(char cbuf[]) throws IOException {103write(cbuf, 0, cbuf.length);104}105106/**107* Write a portion of an array of characters.108*109* @param cbuf Array of characters110* @param off Offset from which to start writing characters111* @param len Number of characters to write112*113* @exception IOException If an I/O error occurs114*/115116public void write(char cbuf[], int off, int len) throws IOException{117ensureOpen();118//optimization: if data size to be written is more than the 'size' specified,119//do not buffer the data but write the data straight to the underlying stream120if(len > size){121//first write the data that may be present in the buffer122writeBufferedData();123//write directly to stream124writer.write(cbuf, off, len);125}else{126buffer.append(cbuf, off, len);127conditionalWrite();128}129}130131/**132* Write a portion of a string.133*134* @param str A String135* @param off Offset from which to start writing characters136* @param len Number of characters to write137*138* @exception IOException If an I/O error occurs139*/140public void write(String str, int off, int len) throws IOException {141write(str.toCharArray(), off, len);142}143144/**145* Write a string.146*147* @param str String to be written148*149* @exception IOException If an I/O error occurs150*/151public void write(String str) throws IOException {152//optimization: if data size to be written is more than the 'size' specified,153//do not buffer the data but write the data straight to the underlying stream - nb.154if(str.length() > size){155//first write the data that may be present in the buffer156writeBufferedData();157//write directly to stream158writer.write(str);159}else{160buffer.append(str);161conditionalWrite();162}163}164165/**166* Close the stream, flushing it first. Once a stream has been closed,167* further write() or flush() invocations will cause an IOException to be168* thrown. Closing a previously-closed stream, however, has no effect.169*170* @exception IOException If an I/O error occurs171*/172public void close() throws IOException {173if(writer == null) return;174//flush it first175flush();176writer.close();177writer = null ;178}179180/**181* Flush the stream. If the stream has saved any characters from the182* various write() methods in a buffer, write them immediately to their183* intended destination. Then, if that destination is another character or184* byte stream, flush it. Thus one flush() invocation will flush all the185* buffers in a chain of Writers and OutputStreams.186*187* @exception IOException If an I/O error occurs188*/189190public void flush() throws IOException {191ensureOpen();192//write current data present in the buffer193writeBufferedData();194writer.flush();195}196197/** Reset this Writer.198*199* see @setWriter()200*/201public void reset(){202this.writer = null;203buffer.clear();204this.size = THRESHHOLD_LENGTH;205}206207/**208* Set the given <code>Writer</code>.209*210* @param Writer Writer.211*/212public void setWriter(Writer writer){213this.writer = writer;214buffer.clear();215this.size = THRESHHOLD_LENGTH;216}217218/** Set the given <code>Writer</code>219*220* @param Writer Writer.221* @param int Writer will buffer the character data size, after that data is written to stream.222*/223public void setWriter(Writer writer, int size){224this.writer = writer;225this.size = size;226}227228/**229* Returns underlying <code>Writer</code>230*/231protected Writer getWriter() {232return writer;233}234235/** write the buffer data, if the buffer size has increased the size specified236*/237private void conditionalWrite() throws IOException {238if(buffer.length > size){239if(DEBUG){240System.out.println("internal buffer length " + buffer.length + " increased size limit : " + size);241System.out.println("Data: ('" + new String(buffer.ch, buffer.offset, buffer.length) + "')");242}243writeBufferedData();244}245}246247/** Write the data present in the buffer to the writer.248* buffer is cleared after write operation.249*/250private void writeBufferedData() throws IOException {251writer.write(buffer.ch, buffer.offset, buffer.length);252buffer.clear();253}254255/** Check to make sure that the stream has not been closed */256private void ensureOpen() throws IOException {257if (writer == null)throw new IOException("Stream closed");258}259}260261262