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.

RawText.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.OutputStream;
  48. import org.eclipse.jgit.util.IO;
  49. import org.eclipse.jgit.util.IntList;
  50. import org.eclipse.jgit.util.RawParseUtils;
  51. /**
  52. * A Sequence supporting UNIX formatted text in byte[] format.
  53. * <p>
  54. * Elements of the sequence are the lines of the file, as delimited by the UNIX
  55. * newline character ('\n'). The file content is treated as 8 bit binary text,
  56. * with no assumptions or requirements on character encoding.
  57. * <p>
  58. * Note that the first line of the file is element 0, as defined by the Sequence
  59. * interface API. Traditionally in a text editor a patch file the first line is
  60. * line number 1. Callers may need to subtract 1 prior to invoking methods if
  61. * they are converting from "line number" to "element index".
  62. */
  63. public class RawText implements Sequence {
  64. /** Number of bytes to check for heuristics in {@link #isBinary(byte[])} */
  65. private static final int FIRST_FEW_BYTES = 8000;
  66. /** The file content for this sequence. */
  67. protected final byte[] content;
  68. /** Map of line number to starting position within {@link #content}. */
  69. protected final IntList lines;
  70. /** Hash code for each line, for fast equality elimination. */
  71. protected final IntList hashes;
  72. /**
  73. * Create a new sequence from an existing content byte array.
  74. * <p>
  75. * The entire array (indexes 0 through length-1) is used as the content.
  76. *
  77. * @param input
  78. * the content array. The array is never modified, so passing
  79. * through cached arrays is safe.
  80. */
  81. public RawText(final byte[] input) {
  82. content = input;
  83. lines = RawParseUtils.lineMap(content, 0, content.length);
  84. hashes = computeHashes();
  85. }
  86. /**
  87. * Create a new sequence from a file.
  88. * <p>
  89. * The entire file contents are used.
  90. *
  91. * @param file
  92. * the text file.
  93. * @throws IOException if Exceptions occur while reading the file
  94. */
  95. public RawText(File file) throws IOException {
  96. this(IO.readFully(file));
  97. }
  98. public int size() {
  99. // The line map is always 2 entries larger than the number of lines in
  100. // the file. Index 0 is padded out/unused. The last index is the total
  101. // length of the buffer, and acts as a sentinel.
  102. //
  103. return lines.size() - 2;
  104. }
  105. public boolean equals(final int i, final Sequence other, final int j) {
  106. return equals(this, i + 1, (RawText) other, j + 1);
  107. }
  108. private static boolean equals(final RawText a, final int ai,
  109. final RawText b, final int bi) {
  110. if (a.hashes.get(ai) != b.hashes.get(bi))
  111. return false;
  112. int as = a.lines.get(ai);
  113. int bs = b.lines.get(bi);
  114. final int ae = a.lines.get(ai + 1);
  115. final int be = b.lines.get(bi + 1);
  116. if (ae - as != be - bs)
  117. return false;
  118. while (as < ae) {
  119. if (a.content[as++] != b.content[bs++])
  120. return false;
  121. }
  122. return true;
  123. }
  124. /**
  125. * Write a specific line to the output stream, without its trailing LF.
  126. * <p>
  127. * The specified line is copied as-is, with no character encoding
  128. * translation performed.
  129. * <p>
  130. * If the specified line ends with an LF ('\n'), the LF is <b>not</b>
  131. * copied. It is up to the caller to write the LF, if desired, between
  132. * output lines.
  133. *
  134. * @param out
  135. * stream to copy the line data onto.
  136. * @param i
  137. * index of the line to extract. Note this is 0-based, so line
  138. * number 1 is actually index 0.
  139. * @throws IOException
  140. * the stream write operation failed.
  141. */
  142. public void writeLine(final OutputStream out, final int i)
  143. throws IOException {
  144. final int start = lines.get(i + 1);
  145. int end = lines.get(i + 2);
  146. if (content[end - 1] == '\n')
  147. end--;
  148. out.write(content, start, end - start);
  149. }
  150. /**
  151. * Determine if the file ends with a LF ('\n').
  152. *
  153. * @return true if the last line has an LF; false otherwise.
  154. */
  155. public boolean isMissingNewlineAtEnd() {
  156. final int end = lines.get(lines.size() - 1);
  157. if (end == 0)
  158. return true;
  159. return content[end - 1] != '\n';
  160. }
  161. private IntList computeHashes() {
  162. final IntList r = new IntList(lines.size());
  163. r.add(0);
  164. for (int lno = 1; lno < lines.size() - 1; lno++) {
  165. final int ptr = lines.get(lno);
  166. final int end = lines.get(lno + 1);
  167. r.add(hashLine(content, ptr, end));
  168. }
  169. r.add(0);
  170. return r;
  171. }
  172. /**
  173. * Compute a hash code for a single line.
  174. *
  175. * @param raw
  176. * the raw file content.
  177. * @param ptr
  178. * first byte of the content line to hash.
  179. * @param end
  180. * 1 past the last byte of the content line.
  181. * @return hash code for the region <code>[ptr, end)</code> of raw.
  182. */
  183. protected int hashLine(final byte[] raw, int ptr, final int end) {
  184. int hash = 5381;
  185. for (; ptr < end; ptr++)
  186. hash = (hash << 5) ^ (raw[ptr] & 0xff);
  187. return hash;
  188. }
  189. /**
  190. * Determine heuristically whether a byte array represents binary (as
  191. * opposed to text) content.
  192. *
  193. * @param raw
  194. * the raw file content.
  195. * @return true if raw is likely to be a binary file, false otherwise
  196. */
  197. public static boolean isBinary(byte[] raw) {
  198. // Same heuristic as C Git
  199. int size = raw.length > FIRST_FEW_BYTES ? FIRST_FEW_BYTES : raw.length;
  200. for (int ptr = 0; ptr < size; ptr++)
  201. if (raw[ptr] == '\0')
  202. return true;
  203. return false;
  204. }
  205. }