001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: WriterOutputBuffer.java 468652 2006-10-28 07:05:17Z minchau $
020     */
021    
022    package org.apache.xalan.xsltc.runtime.output;
023    
024    import java.io.BufferedWriter;
025    import java.io.IOException;
026    import java.io.Writer;
027    
028    /**
029     * @author Santiago Pericas-Geertsen
030     */
031    class WriterOutputBuffer implements OutputBuffer {
032        private static final int KB = 1024;
033        private static int BUFFER_SIZE = 4 * KB;
034    
035        static {
036            // Set a larger buffer size for Solaris
037            final String osName = System.getProperty("os.name");
038            if (osName.equalsIgnoreCase("solaris")) {
039                BUFFER_SIZE = 32 * KB;
040            }
041        }
042    
043        private Writer _writer;
044    
045        /**
046         * Initializes a WriterOutputBuffer by creating an instance of a 
047         * BufferedWriter. The size of the buffer in this writer may have 
048         * a significant impact on throughput. Solaris prefers a larger
049         * buffer, while Linux works better with a smaller one.
050         */
051        public WriterOutputBuffer(Writer writer) {
052            _writer = new BufferedWriter(writer, BUFFER_SIZE);
053        }
054    
055        public String close() {
056            try {
057                _writer.flush();
058            }
059            catch (IOException e) {
060                throw new RuntimeException(e.toString());
061            }
062            return "";
063        }
064    
065        public OutputBuffer append(String s) {
066            try {
067                _writer.write(s);
068            }
069            catch (IOException e) {
070                throw new RuntimeException(e.toString());
071            }
072            return this;
073        }
074    
075        public OutputBuffer append(char[] s, int from, int to) {
076            try {
077                _writer.write(s, from, to);
078            }
079            catch (IOException e) {
080                throw new RuntimeException(e.toString());
081            }
082            return this;
083        }
084    
085        public OutputBuffer append(char ch) {
086            try {
087                _writer.write(ch);
088            }
089            catch (IOException e) {
090                throw new RuntimeException(e.toString());
091            }
092            return this;
093        }
094    }
095    
096