Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RawText.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2008-2009, Johannes E. Schindelin <johannes.schindelin@gmx.de>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.diff;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.io.InputStream;
  48. import java.io.OutputStream;
  49. import org.eclipse.jgit.util.IO;
  50. import org.eclipse.jgit.util.IntList;
  51. import org.eclipse.jgit.util.RawParseUtils;
  52. /**
  53. * A Sequence supporting UNIX formatted text in byte[] format.
  54. * <p>
  55. * Elements of the sequence are the lines of the file, as delimited by the UNIX
  56. * newline character ('\n'). The file content is treated as 8 bit binary text,
  57. * with no assumptions or requirements on character encoding.
  58. * <p>
  59. * Note that the first line of the file is element 0, as defined by the Sequence
  60. * interface API. Traditionally in a text editor a patch file the first line is
  61. * line number 1. Callers may need to subtract 1 prior to invoking methods if
  62. * they are converting from "line number" to "element index".
  63. */
  64. public class RawText extends Sequence {
  65. /** Number of bytes to check for heuristics in {@link #isBinary(byte[])} */
  66. private static final int FIRST_FEW_BYTES = 8000;
  67. /** The file content for this sequence. */
  68. protected final byte[] content;
  69. /** Map of line number to starting position within {@link #content}. */
  70. protected final IntList lines;
  71. /** Hash code for each line, for fast equality elimination. */
  72. protected final int[] hashes;
  73. /**
  74. * Create a new sequence from an existing content byte array.
  75. * <p>
  76. * The entire array (indexes 0 through length-1) is used as the content.
  77. *
  78. * @param input
  79. * the content array. The array is never modified, so passing
  80. * through cached arrays is safe.
  81. */
  82. public RawText(final byte[] input) {
  83. this(RawTextComparator.DEFAULT, input);
  84. }
  85. /**
  86. * Create a new sequence from an existing content byte array.
  87. *
  88. * The entire array (indexes 0 through length-1) is used as the content.
  89. *
  90. * @param cmp
  91. * comparator that will later be used to compare texts.
  92. * @param input
  93. * the content array. The array is never modified, so passing
  94. * through cached arrays is safe.
  95. */
  96. public RawText(RawTextComparator cmp, byte[] input) {
  97. content = input;
  98. lines = RawParseUtils.lineMap(content, 0, content.length);
  99. hashes = computeHashes(cmp);
  100. }
  101. /**
  102. * Create a new sequence from a file.
  103. * <p>
  104. * The entire file contents are used.
  105. *
  106. * @param file
  107. * the text file.
  108. * @throws IOException
  109. * if Exceptions occur while reading the file
  110. */
  111. public RawText(File file) throws IOException {
  112. this(IO.readFully(file));
  113. }
  114. /** @return total number of items in the sequence. */
  115. public int size() {
  116. // The line map is always 2 entries larger than the number of lines in
  117. // the file. Index 0 is padded out/unused. The last index is the total
  118. // length of the buffer, and acts as a sentinel.
  119. //
  120. return lines.size() - 2;
  121. }
  122. /**
  123. * Write a specific line to the output stream, without its trailing LF.
  124. * <p>
  125. * The specified line is copied as-is, with no character encoding
  126. * translation performed.
  127. * <p>
  128. * If the specified line ends with an LF ('\n'), the LF is <b>not</b>
  129. * copied. It is up to the caller to write the LF, if desired, between
  130. * output lines.
  131. *
  132. * @param out
  133. * stream to copy the line data onto.
  134. * @param i
  135. * index of the line to extract. Note this is 0-based, so line
  136. * number 1 is actually index 0.
  137. * @throws IOException
  138. * the stream write operation failed.
  139. */
  140. public void writeLine(final OutputStream out, final int i)
  141. throws IOException {
  142. final int start = lines.get(i + 1);
  143. int end = lines.get(i + 2);
  144. if (content[end - 1] == '\n')
  145. end--;
  146. out.write(content, start, end - start);
  147. }
  148. /**
  149. * Determine if the file ends with a LF ('\n').
  150. *
  151. * @return true if the last line has an LF; false otherwise.
  152. */
  153. public boolean isMissingNewlineAtEnd() {
  154. final int end = lines.get(lines.size() - 1);
  155. if (end == 0)
  156. return true;
  157. return content[end - 1] != '\n';
  158. }
  159. private int[] computeHashes(RawTextComparator cmp) {
  160. final int[] r = new int[lines.size()];
  161. for (int lno = 1; lno < lines.size() - 1; lno++) {
  162. final int ptr = lines.get(lno);
  163. final int end = lines.get(lno + 1);
  164. r[lno] = cmp.hashRegion(content, ptr, end);
  165. }
  166. return r;
  167. }
  168. /**
  169. * Determine heuristically whether a byte array represents binary (as
  170. * opposed to text) content.
  171. *
  172. * @param raw
  173. * the raw file content.
  174. * @return true if raw is likely to be a binary file, false otherwise
  175. */
  176. public static boolean isBinary(byte[] raw) {
  177. return isBinary(raw, raw.length);
  178. }
  179. /**
  180. * Determine heuristically whether the bytes contained in a stream
  181. * represents binary (as opposed to text) content.
  182. *
  183. * Note: Do not further use this stream after having called this method! The
  184. * stream may not be fully read and will be left at an unknown position
  185. * after consuming an unknown number of bytes. The caller is responsible for
  186. * closing the stream.
  187. *
  188. * @param raw
  189. * input stream containing the raw file content.
  190. * @return true if raw is likely to be a binary file, false otherwise
  191. * @throws IOException
  192. * if input stream could not be read
  193. */
  194. public static boolean isBinary(InputStream raw) throws IOException {
  195. final byte[] buffer = new byte[FIRST_FEW_BYTES];
  196. int cnt = 0;
  197. while (cnt < buffer.length) {
  198. final int n = raw.read(buffer, cnt, buffer.length - cnt);
  199. if (n == -1)
  200. break;
  201. cnt += n;
  202. }
  203. return isBinary(buffer, cnt);
  204. }
  205. /**
  206. * Determine heuristically whether a byte array represents binary (as
  207. * opposed to text) content.
  208. *
  209. * @param raw
  210. * the raw file content.
  211. * @param length
  212. * number of bytes in {@code raw} to evaluate. This should be
  213. * {@code raw.length} unless {@code raw} was over-allocated by
  214. * the caller.
  215. * @return true if raw is likely to be a binary file, false otherwise
  216. */
  217. public static boolean isBinary(byte[] raw, int length) {
  218. // Same heuristic as C Git
  219. if (length > FIRST_FEW_BYTES)
  220. length = FIRST_FEW_BYTES;
  221. for (int ptr = 0; ptr < length; ptr++)
  222. if (raw[ptr] == '\0')
  223. return true;
  224. return false;
  225. }
  226. }