1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: WriterOutputBuffer.java,v 1.5 2004/02/16 22:56:25 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.runtime.output;
  20. import java.io.BufferedWriter;
  21. import java.io.IOException;
  22. import java.io.Writer;
  23. /**
  24. * @author Santiago Pericas-Geertsen
  25. */
  26. class WriterOutputBuffer implements OutputBuffer {
  27. private static final int KB = 1024;
  28. private static int BUFFER_SIZE = 4 * KB;
  29. static {
  30. // Set a larger buffer size for Solaris
  31. final String osName = System.getProperty("os.name");
  32. if (osName.equalsIgnoreCase("solaris")) {
  33. BUFFER_SIZE = 32 * KB;
  34. }
  35. }
  36. private Writer _writer;
  37. /**
  38. * Initializes a WriterOutputBuffer by creating an instance of a
  39. * BufferedWriter. The size of the buffer in this writer may have
  40. * a significant impact on throughput. Solaris prefers a larger
  41. * buffer, while Linux works better with a smaller one.
  42. */
  43. public WriterOutputBuffer(Writer writer) {
  44. _writer = new BufferedWriter(writer, BUFFER_SIZE);
  45. }
  46. public String close() {
  47. try {
  48. _writer.flush();
  49. }
  50. catch (IOException e) {
  51. throw new RuntimeException(e.toString());
  52. }
  53. return "";
  54. }
  55. public OutputBuffer append(String s) {
  56. try {
  57. _writer.write(s);
  58. }
  59. catch (IOException e) {
  60. throw new RuntimeException(e.toString());
  61. }
  62. return this;
  63. }
  64. public OutputBuffer append(char[] s, int from, int to) {
  65. try {
  66. _writer.write(s, from, to);
  67. }
  68. catch (IOException e) {
  69. throw new RuntimeException(e.toString());
  70. }
  71. return this;
  72. }
  73. public OutputBuffer append(char ch) {
  74. try {
  75. _writer.write(ch);
  76. }
  77. catch (IOException e) {
  78. throw new RuntimeException(e.toString());
  79. }
  80. return this;
  81. }
  82. }