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.

EolCanonicalizingInputStream.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (C) 2010, 2013 Marc Strapetz <marc.strapetz@syntevo.com>
  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.InputStream;
  46. import org.eclipse.jgit.diff.RawText;
  47. /**
  48. * An input stream which canonicalizes EOLs bytes on the fly to '\n'.
  49. *
  50. * Optionally, a binary check on the first 8000 bytes is performed
  51. * and in case of binary files, canonicalization is turned off
  52. * (for the complete file).
  53. */
  54. public class EolCanonicalizingInputStream extends InputStream {
  55. private final byte[] single = new byte[1];
  56. private final byte[] buf = new byte[8096];
  57. private final InputStream in;
  58. private int cnt;
  59. private int ptr;
  60. private boolean isBinary;
  61. private boolean detectBinary;
  62. private boolean abortIfBinary;
  63. /**
  64. * A special exception thrown when {@link EolCanonicalizingInputStream} is
  65. * told to throw an exception when attempting to read a binary file. The
  66. * exception may be thrown at any stage during reading.
  67. *
  68. * @since 3.2
  69. */
  70. public static class IsBinaryException extends IOException {
  71. private static final long serialVersionUID = 1L;
  72. IsBinaryException() {
  73. super();
  74. }
  75. }
  76. /**
  77. * Creates a new InputStream, wrapping the specified stream
  78. *
  79. * @param in
  80. * raw input stream
  81. * @param detectBinary
  82. * whether binaries should be detected
  83. * @since 2.0
  84. */
  85. public EolCanonicalizingInputStream(InputStream in, boolean detectBinary) {
  86. this(in, detectBinary, false);
  87. }
  88. /**
  89. * Creates a new InputStream, wrapping the specified stream
  90. *
  91. * @param in
  92. * raw input stream
  93. * @param detectBinary
  94. * whether binaries should be detected
  95. * @param abortIfBinary
  96. * throw an IOException if the file is binary
  97. * @since 3.2
  98. */
  99. public EolCanonicalizingInputStream(InputStream in, boolean detectBinary,
  100. boolean abortIfBinary) {
  101. this.in = in;
  102. this.detectBinary = detectBinary;
  103. this.abortIfBinary = abortIfBinary;
  104. }
  105. @Override
  106. public int read() throws IOException {
  107. final int read = read(single, 0, 1);
  108. return read == 1 ? single[0] & 0xff : -1;
  109. }
  110. @Override
  111. public int read(byte[] bs, final int off, final int len) throws IOException {
  112. if (len == 0)
  113. return 0;
  114. if (cnt == -1)
  115. return -1;
  116. int i = off;
  117. final int end = off + len;
  118. while (i < end) {
  119. if (ptr == cnt && !fillBuffer()) {
  120. break;
  121. }
  122. byte b = buf[ptr++];
  123. if (isBinary || b != '\r') {
  124. // Logic for binary files ends here
  125. bs[i++] = b;
  126. continue;
  127. }
  128. if (ptr == cnt && !fillBuffer()) {
  129. bs[i++] = '\r';
  130. break;
  131. }
  132. if (buf[ptr] == '\n') {
  133. bs[i++] = '\n';
  134. ptr++;
  135. } else
  136. bs[i++] = '\r';
  137. }
  138. return i == off ? -1 : i - off;
  139. }
  140. /**
  141. * @return true if the stream has detected as a binary so far
  142. * @since 3.2
  143. */
  144. public boolean isBinary() {
  145. return isBinary;
  146. }
  147. @Override
  148. public void close() throws IOException {
  149. in.close();
  150. }
  151. private boolean fillBuffer() throws IOException {
  152. cnt = in.read(buf, 0, buf.length);
  153. if (cnt < 1)
  154. return false;
  155. if (detectBinary) {
  156. isBinary = RawText.isBinary(buf, cnt);
  157. detectBinary = false;
  158. if (isBinary && abortIfBinary)
  159. throw new IsBinaryException();
  160. }
  161. ptr = 0;
  162. return true;
  163. }
  164. }