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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /** A Rawtext of length 0 */
  66. public static final RawText EMPTY_TEXT = new RawText(new byte[0]);
  67. /** Number of bytes to check for heuristics in {@link #isBinary(byte[])} */
  68. private static final int FIRST_FEW_BYTES = 8000;
  69. /** The file content for this sequence. */
  70. protected final byte[] content;
  71. /** Map of line number to starting position within {@link #content}. */
  72. protected final IntList lines;
  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. content = input;
  84. lines = RawParseUtils.lineMap(content, 0, content.length);
  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
  94. * if Exceptions occur while reading the file
  95. */
  96. public RawText(File file) throws IOException {
  97. this(IO.readFully(file));
  98. }
  99. /** @return total number of items in the sequence. */
  100. public int size() {
  101. // The line map is always 2 entries larger than the number of lines in
  102. // the file. Index 0 is padded out/unused. The last index is the total
  103. // length of the buffer, and acts as a sentinel.
  104. //
  105. return lines.size() - 2;
  106. }
  107. /**
  108. * Write a specific line to the output stream, without its trailing LF.
  109. * <p>
  110. * The specified line is copied as-is, with no character encoding
  111. * translation performed.
  112. * <p>
  113. * If the specified line ends with an LF ('\n'), the LF is <b>not</b>
  114. * copied. It is up to the caller to write the LF, if desired, between
  115. * output lines.
  116. *
  117. * @param out
  118. * stream to copy the line data onto.
  119. * @param i
  120. * index of the line to extract. Note this is 0-based, so line
  121. * number 1 is actually index 0.
  122. * @throws IOException
  123. * the stream write operation failed.
  124. */
  125. public void writeLine(final OutputStream out, final int i)
  126. throws IOException {
  127. int start = getStart(i);
  128. int end = getEnd(i);
  129. if (content[end - 1] == '\n')
  130. end--;
  131. out.write(content, start, end - start);
  132. }
  133. /**
  134. * Determine if the file ends with a LF ('\n').
  135. *
  136. * @return true if the last line has an LF; false otherwise.
  137. */
  138. public boolean isMissingNewlineAtEnd() {
  139. final int end = lines.get(lines.size() - 1);
  140. if (end == 0)
  141. return true;
  142. return content[end - 1] != '\n';
  143. }
  144. /**
  145. * Get the text for a single line.
  146. *
  147. * @param i
  148. * index of the line to extract. Note this is 0-based, so line
  149. * number 1 is actually index 0.
  150. * @return the text for the line, without a trailing LF.
  151. */
  152. public String getString(int i) {
  153. return getString(i, i + 1, true);
  154. }
  155. /**
  156. * Get the text for a region of lines.
  157. *
  158. * @param begin
  159. * index of the first line to extract. Note this is 0-based, so
  160. * line number 1 is actually index 0.
  161. * @param end
  162. * index of one past the last line to extract.
  163. * @param dropLF
  164. * if true the trailing LF ('\n') of the last returned line is
  165. * dropped, if present.
  166. * @return the text for lines {@code [begin, end)}.
  167. */
  168. public String getString(int begin, int end, boolean dropLF) {
  169. if (begin == end)
  170. return ""; //$NON-NLS-1$
  171. int s = getStart(begin);
  172. int e = getEnd(end - 1);
  173. if (dropLF && content[e - 1] == '\n')
  174. e--;
  175. return decode(s, e);
  176. }
  177. /**
  178. * Decode a region of the text into a String.
  179. *
  180. * The default implementation of this method tries to guess the character
  181. * set by considering UTF-8, the platform default, and falling back on
  182. * ISO-8859-1 if neither of those can correctly decode the region given.
  183. *
  184. * @param start
  185. * first byte of the content to decode.
  186. * @param end
  187. * one past the last byte of the content to decode.
  188. * @return the region {@code [start, end)} decoded as a String.
  189. */
  190. protected String decode(int start, int end) {
  191. return RawParseUtils.decode(content, start, end);
  192. }
  193. private int getStart(final int i) {
  194. return lines.get(i + 1);
  195. }
  196. private int getEnd(final int i) {
  197. return lines.get(i + 2);
  198. }
  199. /**
  200. * Determine heuristically whether a byte array represents binary (as
  201. * opposed to text) content.
  202. *
  203. * @param raw
  204. * the raw file content.
  205. * @return true if raw is likely to be a binary file, false otherwise
  206. */
  207. public static boolean isBinary(byte[] raw) {
  208. return isBinary(raw, raw.length);
  209. }
  210. /**
  211. * Determine heuristically whether the bytes contained in a stream
  212. * represents binary (as opposed to text) content.
  213. *
  214. * Note: Do not further use this stream after having called this method! The
  215. * stream may not be fully read and will be left at an unknown position
  216. * after consuming an unknown number of bytes. The caller is responsible for
  217. * closing the stream.
  218. *
  219. * @param raw
  220. * input stream containing the raw file content.
  221. * @return true if raw is likely to be a binary file, false otherwise
  222. * @throws IOException
  223. * if input stream could not be read
  224. */
  225. public static boolean isBinary(InputStream raw) throws IOException {
  226. final byte[] buffer = new byte[FIRST_FEW_BYTES];
  227. int cnt = 0;
  228. while (cnt < buffer.length) {
  229. final int n = raw.read(buffer, cnt, buffer.length - cnt);
  230. if (n == -1)
  231. break;
  232. cnt += n;
  233. }
  234. return isBinary(buffer, cnt);
  235. }
  236. /**
  237. * Determine heuristically whether a byte array represents binary (as
  238. * opposed to text) content.
  239. *
  240. * @param raw
  241. * the raw file content.
  242. * @param length
  243. * number of bytes in {@code raw} to evaluate. This should be
  244. * {@code raw.length} unless {@code raw} was over-allocated by
  245. * the caller.
  246. * @return true if raw is likely to be a binary file, false otherwise
  247. */
  248. public static boolean isBinary(byte[] raw, int length) {
  249. // Same heuristic as C Git
  250. if (length > FIRST_FEW_BYTES)
  251. length = FIRST_FEW_BYTES;
  252. for (int ptr = 0; ptr < length; ptr++)
  253. if (raw[ptr] == '\0')
  254. return true;
  255. return false;
  256. }
  257. /**
  258. * Get the line delimiter for the first line.
  259. *
  260. * @since 2.0
  261. * @return the line delimiter or <code>null</code>
  262. */
  263. public String getLineDelimiter() {
  264. if (size() == 0)
  265. return null;
  266. int e = getEnd(0);
  267. if (content[e - 1] != '\n')
  268. return null;
  269. if (content.length > 1 && e > 1 && content[e - 2] == '\r')
  270. return "\r\n"; //$NON-NLS-1$
  271. else
  272. return "\n"; //$NON-NLS-1$
  273. }
  274. }