You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AutoCRLFOutputStream.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2011, 2013 Robin Rosenberg
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util.io;
  44. import java.io.IOException;
  45. import java.io.OutputStream;
  46. import org.eclipse.jgit.diff.RawText;
  47. /**
  48. * An OutputStream that expands LF to CRLF.
  49. * <p>
  50. * Existing CRLF are not expanded to CRCRLF, but retained as is.
  51. */
  52. public class AutoCRLFOutputStream extends OutputStream {
  53. static final int BUFFER_SIZE = 8000;
  54. private final OutputStream out;
  55. private int buf = -1;
  56. private byte[] binbuf = new byte[BUFFER_SIZE];
  57. private byte[] onebytebuf = new byte[1];
  58. private int binbufcnt = 0;
  59. private boolean isBinary;
  60. /**
  61. * @param out
  62. */
  63. public AutoCRLFOutputStream(OutputStream out) {
  64. this.out = out;
  65. }
  66. @Override
  67. public void write(int b) throws IOException {
  68. onebytebuf[0] = (byte) b;
  69. write(onebytebuf, 0, 1);
  70. }
  71. @Override
  72. public void write(byte[] b) throws IOException {
  73. int overflow = buffer(b, 0, b.length);
  74. if (overflow > 0)
  75. write(b, b.length - overflow, overflow);
  76. }
  77. @Override
  78. public void write(byte[] b, final int startOff, final int startLen)
  79. throws IOException {
  80. final int overflow = buffer(b, startOff, startLen);
  81. if (overflow < 0)
  82. return;
  83. final int off = startOff + startLen - overflow;
  84. final int len = overflow;
  85. if (len == 0)
  86. return;
  87. int lastw = off;
  88. if (isBinary) {
  89. out.write(b, off, len);
  90. return;
  91. }
  92. for (int i = off; i < off + len; ++i) {
  93. final byte c = b[i];
  94. if (c == '\r') {
  95. buf = '\r';
  96. } else if (c == '\n') {
  97. if (buf != '\r') {
  98. if (lastw < i) {
  99. out.write(b, lastw, i - lastw);
  100. }
  101. out.write('\r');
  102. lastw = i;
  103. }
  104. buf = -1;
  105. } else {
  106. buf = -1;
  107. }
  108. }
  109. if (lastw < off + len) {
  110. out.write(b, lastw, off + len - lastw);
  111. }
  112. if (b[off + len - 1] == '\r')
  113. buf = '\r';
  114. }
  115. private int buffer(byte[] b, int off, int len) throws IOException {
  116. if (binbufcnt > binbuf.length)
  117. return len;
  118. int copy = Math.min(binbuf.length - binbufcnt, len);
  119. System.arraycopy(b, off, binbuf, binbufcnt, copy);
  120. binbufcnt += copy;
  121. int remaining = len - copy;
  122. if (remaining > 0)
  123. decideMode();
  124. return remaining;
  125. }
  126. private void decideMode() throws IOException {
  127. isBinary = RawText.isBinary(binbuf, binbufcnt);
  128. int cachedLen = binbufcnt;
  129. binbufcnt = binbuf.length + 1; // full!
  130. write(binbuf, 0, cachedLen);
  131. }
  132. @Override
  133. public void flush() throws IOException {
  134. if (binbufcnt <= binbuf.length)
  135. decideMode();
  136. buf = -1;
  137. out.flush();
  138. }
  139. @Override
  140. public void close() throws IOException {
  141. flush();
  142. out.close();
  143. }
  144. }