/hudson-core/src/main/java/hudson/util/LineEndNormalizingWriter.java

http://github.com/hudson/hudson · Java · 104 lines · 53 code · 14 blank · 37 comment · 8 complexity · 01def6deb1c53178f7c1d25d2a83766e MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.util;
  25. import java.io.FilterWriter;
  26. import java.io.Writer;
  27. import java.io.IOException;
  28. /**
  29. * Finds the lone LF and converts that to CR+LF.
  30. *
  31. * <p>
  32. * Internet Explorer's <tt>XmlHttpRequest.responseText</tt> seems to
  33. * normalize the line end, and if we only send LF without CR, it will
  34. * not recognize that as a new line. To work around this problem,
  35. * we use this filter to always convert LF to CR+LF.
  36. *
  37. * @author Kohsuke Kawaguchi
  38. * @deprecated since 2008-05-28. moved to stapler
  39. */
  40. public class LineEndNormalizingWriter extends FilterWriter {
  41. private boolean seenCR;
  42. public LineEndNormalizingWriter(Writer out) {
  43. super(out);
  44. }
  45. public void write(char cbuf[]) throws IOException {
  46. write(cbuf, 0, cbuf.length);
  47. }
  48. public void write(String str) throws IOException {
  49. write(str,0,str.length());
  50. }
  51. public void write(int c) throws IOException {
  52. if(!seenCR && c==LF)
  53. super.write("\r\n");
  54. else
  55. super.write(c);
  56. seenCR = (c==CR);
  57. }
  58. public void write(char cbuf[], int off, int len) throws IOException {
  59. int end = off+len;
  60. int writeBegin = off;
  61. for( int i=off; i<end; i++ ) {
  62. char ch = cbuf[i];
  63. if(!seenCR && ch==LF) {
  64. // write up to the char before LF
  65. super.write(cbuf,writeBegin,i-writeBegin);
  66. super.write("\r\n");
  67. writeBegin=i+1;
  68. }
  69. seenCR = (ch==CR);
  70. }
  71. super.write(cbuf,writeBegin,end-writeBegin);
  72. }
  73. public void write(String str, int off, int len) throws IOException {
  74. int end = off+len;
  75. int writeBegin = off;
  76. for( int i=off; i<end; i++ ) {
  77. char ch = str.charAt(i);
  78. if(!seenCR && ch==LF) {
  79. // write up to the char before LF
  80. super.write(str,writeBegin,i-writeBegin);
  81. super.write("\r\n");
  82. writeBegin=i+1;
  83. }
  84. seenCR = (ch==CR);
  85. }
  86. super.write(str,writeBegin,end-writeBegin);
  87. }
  88. private static final int CR = 0x0D;
  89. private static final int LF = 0x0A;
  90. }