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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.text.MessageFormat;
  53. import org.eclipse.jgit.JGitText;
  54. /**
  55. * Input/Output utilities
  56. */
  57. public class IO {
  58. /**
  59. * Read an entire local file into memory as a byte array.
  60. *
  61. * @param path
  62. * location of the file to read.
  63. * @return complete contents of the requested local file.
  64. * @throws FileNotFoundException
  65. * the file does not exist.
  66. * @throws IOException
  67. * the file exists, but its contents cannot be read.
  68. */
  69. public static final byte[] readFully(final File path)
  70. throws FileNotFoundException, IOException {
  71. return IO.readFully(path, Integer.MAX_VALUE);
  72. }
  73. /**
  74. * Read an entire local file into memory as a byte array.
  75. *
  76. * @param path
  77. * location of the file to read.
  78. * @param max
  79. * maximum number of bytes to read, if the file is larger than
  80. * this limit an IOException is thrown.
  81. * @return complete contents of the requested local file.
  82. * @throws FileNotFoundException
  83. * the file does not exist.
  84. * @throws IOException
  85. * the file exists, but its contents cannot be read.
  86. */
  87. public static final byte[] readFully(final File path, final int max)
  88. throws FileNotFoundException, IOException {
  89. final FileInputStream in = new FileInputStream(path);
  90. try {
  91. final long sz = in.getChannel().size();
  92. if (sz > max)
  93. throw new IOException(MessageFormat.format(JGitText.get().fileIsTooLarge, path));
  94. final byte[] buf = new byte[(int) sz];
  95. IO.readFully(in, buf, 0, buf.length);
  96. return buf;
  97. } finally {
  98. try {
  99. in.close();
  100. } catch (IOException ignored) {
  101. // ignore any close errors, this was a read only stream
  102. }
  103. }
  104. }
  105. /**
  106. * Read the entire byte array into memory, or throw an exception.
  107. *
  108. * @param fd
  109. * input stream to read the data from.
  110. * @param dst
  111. * buffer that must be fully populated, [off, off+len).
  112. * @param off
  113. * position within the buffer to start writing to.
  114. * @param len
  115. * number of bytes that must be read.
  116. * @throws EOFException
  117. * the stream ended before dst was fully populated.
  118. * @throws IOException
  119. * there was an error reading from the stream.
  120. */
  121. public static void readFully(final InputStream fd, final byte[] dst,
  122. int off, int len) throws IOException {
  123. while (len > 0) {
  124. final int r = fd.read(dst, off, len);
  125. if (r <= 0)
  126. throw new EOFException(JGitText.get().shortReadOfBlock);
  127. off += r;
  128. len -= r;
  129. }
  130. }
  131. /**
  132. * Skip an entire region of an input stream.
  133. * <p>
  134. * The input stream's position is moved forward by the number of requested
  135. * bytes, discarding them from the input. This method does not return until
  136. * the exact number of bytes requested has been skipped.
  137. *
  138. * @param fd
  139. * the stream to skip bytes from.
  140. * @param toSkip
  141. * total number of bytes to be discarded. Must be >= 0.
  142. * @throws EOFException
  143. * the stream ended before the requested number of bytes were
  144. * skipped.
  145. * @throws IOException
  146. * there was an error reading from the stream.
  147. */
  148. public static void skipFully(final InputStream fd, long toSkip)
  149. throws IOException {
  150. while (toSkip > 0) {
  151. final long r = fd.skip(toSkip);
  152. if (r <= 0)
  153. throw new EOFException(JGitText.get().shortSkipOfBlock);
  154. toSkip -= r;
  155. }
  156. }
  157. private IO() {
  158. // Don't create instances of a static only utility.
  159. }
  160. }