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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 implements Sequence {
  65. /** Creates a RawText instance. */
  66. public static interface Factory {
  67. /**
  68. * Construct a RawText instance for the content.
  69. *
  70. * @param input
  71. * the content array.
  72. * @return a RawText instance wrapping this content.
  73. */
  74. RawText create(byte[] input);
  75. }
  76. /** Creates RawText that does not treat whitespace specially. */
  77. public static final Factory FACTORY = new Factory() {
  78. public RawText create(byte[] input) {
  79. return new RawText(input);
  80. }
  81. };
  82. /** Number of bytes to check for heuristics in {@link #isBinary(byte[])} */
  83. private static final int FIRST_FEW_BYTES = 8000;
  84. /** The file content for this sequence. */
  85. protected final byte[] content;
  86. /** Map of line number to starting position within {@link #content}. */
  87. protected final IntList lines;
  88. /** Hash code for each line, for fast equality elimination. */
  89. protected final IntList hashes;
  90. /**
  91. * Create a new sequence from an existing content byte array.
  92. * <p>
  93. * The entire array (indexes 0 through length-1) is used as the content.
  94. *
  95. * @param input
  96. * the content array. The array is never modified, so passing
  97. * through cached arrays is safe.
  98. */
  99. public RawText(final byte[] input) {
  100. content = input;
  101. lines = RawParseUtils.lineMap(content, 0, content.length);
  102. hashes = computeHashes();
  103. }
  104. /**
  105. * Create a new sequence from a file.
  106. * <p>
  107. * The entire file contents are used.
  108. *
  109. * @param file
  110. * the text file.
  111. * @throws IOException
  112. * if Exceptions occur while reading the file
  113. */
  114. public RawText(File file) throws IOException {
  115. this(IO.readFully(file));
  116. }
  117. public int size() {
  118. // The line map is always 2 entries larger than the number of lines in
  119. // the file. Index 0 is padded out/unused. The last index is the total
  120. // length of the buffer, and acts as a sentinel.
  121. //
  122. return lines.size() - 2;
  123. }
  124. public boolean equals(final int i, final Sequence other, final int j) {
  125. return equals(this, i + 1, (RawText) other, j + 1);
  126. }
  127. private static boolean equals(final RawText a, final int ai,
  128. final RawText b, final int bi) {
  129. if (a.hashes.get(ai) != b.hashes.get(bi))
  130. return false;
  131. int as = a.lines.get(ai);
  132. int bs = b.lines.get(bi);
  133. final int ae = a.lines.get(ai + 1);
  134. final int be = b.lines.get(bi + 1);
  135. if (ae - as != be - bs)
  136. return false;
  137. while (as < ae) {
  138. if (a.content[as++] != b.content[bs++])
  139. return false;
  140. }
  141. return true;
  142. }
  143. /**
  144. * Write a specific line to the output stream, without its trailing LF.
  145. * <p>
  146. * The specified line is copied as-is, with no character encoding
  147. * translation performed.
  148. * <p>
  149. * If the specified line ends with an LF ('\n'), the LF is <b>not</b>
  150. * copied. It is up to the caller to write the LF, if desired, between
  151. * output lines.
  152. *
  153. * @param out
  154. * stream to copy the line data onto.
  155. * @param i
  156. * index of the line to extract. Note this is 0-based, so line
  157. * number 1 is actually index 0.
  158. * @throws IOException
  159. * the stream write operation failed.
  160. */
  161. public void writeLine(final OutputStream out, final int i)
  162. throws IOException {
  163. final int start = lines.get(i + 1);
  164. int end = lines.get(i + 2);
  165. if (content[end - 1] == '\n')
  166. end--;
  167. out.write(content, start, end - start);
  168. }
  169. /**
  170. * Determine if the file ends with a LF ('\n').
  171. *
  172. * @return true if the last line has an LF; false otherwise.
  173. */
  174. public boolean isMissingNewlineAtEnd() {
  175. final int end = lines.get(lines.size() - 1);
  176. if (end == 0)
  177. return true;
  178. return content[end - 1] != '\n';
  179. }
  180. private IntList computeHashes() {
  181. final IntList r = new IntList(lines.size());
  182. r.add(0);
  183. for (int lno = 1; lno < lines.size() - 1; lno++) {
  184. final int ptr = lines.get(lno);
  185. final int end = lines.get(lno + 1);
  186. r.add(hashLine(content, ptr, end));
  187. }
  188. r.add(0);
  189. return r;
  190. }
  191. /**
  192. * Compute a hash code for a single line.
  193. *
  194. * @param raw
  195. * the raw file content.
  196. * @param ptr
  197. * first byte of the content line to hash.
  198. * @param end
  199. * 1 past the last byte of the content line.
  200. * @return hash code for the region <code>[ptr, end)</code> of raw.
  201. */
  202. protected int hashLine(final byte[] raw, int ptr, final int end) {
  203. int hash = 5381;
  204. for (; ptr < end; ptr++)
  205. hash = (hash << 5) ^ (raw[ptr] & 0xff);
  206. return hash;
  207. }
  208. /**
  209. * Determine heuristically whether a byte array represents binary (as
  210. * opposed to text) content.
  211. *
  212. * @param raw
  213. * the raw file content.
  214. * @return true if raw is likely to be a binary file, false otherwise
  215. */
  216. public static boolean isBinary(byte[] raw) {
  217. return isBinary(raw, raw.length);
  218. }
  219. /**
  220. * Determine heuristically whether the bytes contained in a stream
  221. * represents binary (as opposed to text) content.
  222. *
  223. * Note: Do not further use this stream after having called this method! The
  224. * stream may not be fully read and will be left at an unknown position
  225. * after consuming an unknown number of bytes. The caller is responsible for
  226. * closing the stream.
  227. *
  228. * @param raw
  229. * input stream containing the raw file content.
  230. * @return true if raw is likely to be a binary file, false otherwise
  231. * @throws IOException
  232. * if input stream could not be read
  233. */
  234. public static boolean isBinary(InputStream raw) throws IOException {
  235. final byte[] buffer = new byte[FIRST_FEW_BYTES];
  236. int cnt = 0;
  237. while (cnt < buffer.length) {
  238. final int n = raw.read(buffer, cnt, buffer.length - cnt);
  239. if (n == -1)
  240. break;
  241. cnt += n;
  242. }
  243. return isBinary(buffer, cnt);
  244. }
  245. /**
  246. * Determine heuristically whether a byte array represents binary (as
  247. * opposed to text) content.
  248. *
  249. * @param raw
  250. * the raw file content.
  251. * @param length
  252. * number of bytes in {@code raw} to evaluate. This should be
  253. * {@code raw.length} unless {@code raw} was over-allocated by
  254. * the caller.
  255. * @return true if raw is likely to be a binary file, false otherwise
  256. */
  257. public static boolean isBinary(byte[] raw, int length) {
  258. // Same heuristic as C Git
  259. if (length > FIRST_FEW_BYTES)
  260. length = FIRST_FEW_BYTES;
  261. for (int ptr = 0; ptr < length; ptr++)
  262. if (raw[ptr] == '\0')
  263. return true;
  264. return false;
  265. }
  266. }