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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /** The file content for this sequence. */
  65. protected final byte[] content;
  66. /** Map of line number to starting position within {@link #content}. */
  67. protected final IntList lines;
  68. /** Hash code for each line, for fast equality elimination. */
  69. protected final IntList hashes;
  70. /**
  71. * Create a new sequence from an existing content byte array.
  72. * <p>
  73. * The entire array (indexes 0 through length-1) is used as the content.
  74. *
  75. * @param input
  76. * the content array. The array is never modified, so passing
  77. * through cached arrays is safe.
  78. */
  79. public RawText(final byte[] input) {
  80. content = input;
  81. lines = RawParseUtils.lineMap(content, 0, content.length);
  82. hashes = computeHashes();
  83. }
  84. /**
  85. * Create a new sequence from a file.
  86. * <p>
  87. * The entire file contents are used.
  88. *
  89. * @param file
  90. * the text file.
  91. * @throws IOException if Exceptions occur while reading the file
  92. */
  93. public RawText(File file) throws IOException {
  94. this(IO.readFully(file));
  95. }
  96. public int size() {
  97. // The line map is always 2 entries larger than the number of lines in
  98. // the file. Index 0 is padded out/unused. The last index is the total
  99. // length of the buffer, and acts as a sentinel.
  100. //
  101. return lines.size() - 2;
  102. }
  103. public boolean equals(final int i, final Sequence other, final int j) {
  104. return equals(this, i + 1, (RawText) other, j + 1);
  105. }
  106. private static boolean equals(final RawText a, final int ai,
  107. final RawText b, final int bi) {
  108. if (a.hashes.get(ai) != b.hashes.get(bi))
  109. return false;
  110. int as = a.lines.get(ai);
  111. int bs = b.lines.get(bi);
  112. final int ae = a.lines.get(ai + 1);
  113. final int be = b.lines.get(bi + 1);
  114. if (ae - as != be - bs)
  115. return false;
  116. while (as < ae) {
  117. if (a.content[as++] != b.content[bs++])
  118. return false;
  119. }
  120. return true;
  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 IntList computeHashes() {
  160. final IntList r = new IntList(lines.size());
  161. r.add(0);
  162. for (int lno = 1; lno < lines.size() - 1; lno++) {
  163. final int ptr = lines.get(lno);
  164. final int end = lines.get(lno + 1);
  165. r.add(hashLine(content, ptr, end));
  166. }
  167. r.add(0);
  168. return r;
  169. }
  170. /**
  171. * Compute a hash code for a single line.
  172. *
  173. * @param raw
  174. * the raw file content.
  175. * @param ptr
  176. * first byte of the content line to hash.
  177. * @param end
  178. * 1 past the last byte of the content line.
  179. * @return hash code for the region <code>[ptr, end)</code> of raw.
  180. */
  181. protected int hashLine(final byte[] raw, int ptr, final int end) {
  182. int hash = 5381;
  183. for (; ptr < end; ptr++)
  184. hash = (hash << 5) ^ (raw[ptr] & 0xff);
  185. return hash;
  186. }
  187. }