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.

IO.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.util;
  46. import java.io.EOFException;
  47. import java.io.File;
  48. import java.io.FileInputStream;
  49. import java.io.FileNotFoundException;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.nio.ByteBuffer;
  53. import java.text.MessageFormat;
  54. import org.eclipse.jgit.JGitText;
  55. /**
  56. * Input/Output utilities
  57. */
  58. public class IO {
  59. /**
  60. * Read an entire local file into memory as a byte array.
  61. *
  62. * @param path
  63. * location of the file to read.
  64. * @return complete contents of the requested local file.
  65. * @throws FileNotFoundException
  66. * the file does not exist.
  67. * @throws IOException
  68. * the file exists, but its contents cannot be read.
  69. */
  70. public static final byte[] readFully(final File path)
  71. throws FileNotFoundException, IOException {
  72. return IO.readFully(path, Integer.MAX_VALUE);
  73. }
  74. /**
  75. * Read an entire local file into memory as a byte array.
  76. *
  77. * @param path
  78. * location of the file to read.
  79. * @param max
  80. * maximum number of bytes to read, if the file is larger than
  81. * this limit an IOException is thrown.
  82. * @return complete contents of the requested local file.
  83. * @throws FileNotFoundException
  84. * the file does not exist.
  85. * @throws IOException
  86. * the file exists, but its contents cannot be read.
  87. */
  88. public static final byte[] readFully(final File path, final int max)
  89. throws FileNotFoundException, IOException {
  90. final FileInputStream in = new FileInputStream(path);
  91. try {
  92. final long sz = in.getChannel().size();
  93. if (sz > max)
  94. throw new IOException(MessageFormat.format(
  95. JGitText.get().fileIsTooLarge, path));
  96. final byte[] buf = new byte[(int) sz];
  97. IO.readFully(in, buf, 0, buf.length);
  98. return buf;
  99. } finally {
  100. try {
  101. in.close();
  102. } catch (IOException ignored) {
  103. // ignore any close errors, this was a read only stream
  104. }
  105. }
  106. }
  107. /**
  108. * Read an entire input stream into memory as a ByteBuffer.
  109. *
  110. * Note: The stream is read to its end and is not usable after calling this
  111. * method. The caller is responsible for closing the stream.
  112. *
  113. * @param in
  114. * input stream to be read.
  115. * @param sizeHint
  116. * a hint on the approximate number of bytes contained in the
  117. * stream, used to allocate temporary buffers more efficiently
  118. * @return complete contents of the input stream. The ByteBuffer always has
  119. * a writable backing array, with {@code position() == 0} and
  120. * {@code limit()} equal to the actual length read. Callers may rely
  121. * on obtaining the underlying array for efficient data access. If
  122. * {@code sizeHint} was too large, the array may be over-allocated,
  123. * resulting in {@code limit() < array().length}.
  124. * @throws IOException
  125. * there was an error reading from the stream.
  126. */
  127. public static ByteBuffer readWholeStream(InputStream in, int sizeHint)
  128. throws IOException {
  129. byte[] out = new byte[sizeHint];
  130. int pos = 0;
  131. while (pos < out.length) {
  132. int read = in.read(out, pos, out.length - pos);
  133. if (read < 0)
  134. return ByteBuffer.wrap(out, 0, pos);
  135. pos += read;
  136. }
  137. int last = in.read();
  138. if (last < 0)
  139. return ByteBuffer.wrap(out, 0, pos);
  140. TemporaryBuffer.Heap tmp = new TemporaryBuffer.Heap(Integer.MAX_VALUE);
  141. tmp.write(out);
  142. tmp.write(last);
  143. tmp.copy(in);
  144. return ByteBuffer.wrap(tmp.toByteArray());
  145. }
  146. /**
  147. * Read the entire byte array into memory, or throw an exception.
  148. *
  149. * @param fd
  150. * input stream to read the data from.
  151. * @param dst
  152. * buffer that must be fully populated, [off, off+len).
  153. * @param off
  154. * position within the buffer to start writing to.
  155. * @param len
  156. * number of bytes that must be read.
  157. * @throws EOFException
  158. * the stream ended before dst was fully populated.
  159. * @throws IOException
  160. * there was an error reading from the stream.
  161. */
  162. public static void readFully(final InputStream fd, final byte[] dst,
  163. int off, int len) throws IOException {
  164. while (len > 0) {
  165. final int r = fd.read(dst, off, len);
  166. if (r <= 0)
  167. throw new EOFException(JGitText.get().shortReadOfBlock);
  168. off += r;
  169. len -= r;
  170. }
  171. }
  172. /**
  173. * Skip an entire region of an input stream.
  174. * <p>
  175. * The input stream's position is moved forward by the number of requested
  176. * bytes, discarding them from the input. This method does not return until
  177. * the exact number of bytes requested has been skipped.
  178. *
  179. * @param fd
  180. * the stream to skip bytes from.
  181. * @param toSkip
  182. * total number of bytes to be discarded. Must be >= 0.
  183. * @throws EOFException
  184. * the stream ended before the requested number of bytes were
  185. * skipped.
  186. * @throws IOException
  187. * there was an error reading from the stream.
  188. */
  189. public static void skipFully(final InputStream fd, long toSkip)
  190. throws IOException {
  191. while (toSkip > 0) {
  192. final long r = fd.skip(toSkip);
  193. if (r <= 0)
  194. throw new EOFException(JGitText.get().shortSkipOfBlock);
  195. toSkip -= r;
  196. }
  197. }
  198. private IO() {
  199. // Don't create instances of a static only utility.
  200. }
  201. }