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.

LargeObjectException.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.errors;
  44. import java.text.MessageFormat;
  45. import org.eclipse.jgit.internal.JGitText;
  46. import org.eclipse.jgit.lib.AnyObjectId;
  47. import org.eclipse.jgit.lib.ObjectId;
  48. /** An object is too big to load into memory as a single byte array. */
  49. public class LargeObjectException extends RuntimeException {
  50. private static final long serialVersionUID = 1L;
  51. private ObjectId objectId;
  52. /** Create a large object exception, where the object isn't known. */
  53. public LargeObjectException() {
  54. // Do nothing.
  55. }
  56. /**
  57. * Create a large object exception, naming the object that is too big.
  58. *
  59. * @param id
  60. * identity of the object that is too big to be loaded as a byte
  61. * array in this JVM.
  62. */
  63. public LargeObjectException(AnyObjectId id) {
  64. setObjectId(id);
  65. }
  66. /** @return identity of the object that is too large; may be null. */
  67. public ObjectId getObjectId() {
  68. return objectId;
  69. }
  70. /** @return either the hex encoded name of the object, or 'unknown object'. */
  71. protected String getObjectName() {
  72. if (getObjectId() != null)
  73. return getObjectId().name();
  74. return JGitText.get().unknownObject;
  75. }
  76. /**
  77. * Set the identity of the object, if its not already set.
  78. *
  79. * @param id
  80. * the id of the object that is too large to process.
  81. */
  82. public void setObjectId(AnyObjectId id) {
  83. if (objectId == null)
  84. objectId = id.copy();
  85. }
  86. @Override
  87. public String getMessage() {
  88. return MessageFormat.format(JGitText.get().largeObjectException,
  89. getObjectName());
  90. }
  91. /** An error caused by the JVM being out of heap space. */
  92. public static class OutOfMemory extends LargeObjectException {
  93. private static final long serialVersionUID = 1L;
  94. /**
  95. * Construct a wrapper around the original OutOfMemoryError.
  96. *
  97. * @param cause
  98. * the original root cause.
  99. */
  100. public OutOfMemory(OutOfMemoryError cause) {
  101. initCause(cause);
  102. }
  103. @Override
  104. public String getMessage() {
  105. return MessageFormat.format(JGitText.get().largeObjectOutOfMemory,
  106. getObjectName());
  107. }
  108. }
  109. /** Object size exceeds JVM limit of 2 GiB per byte array. */
  110. public static class ExceedsByteArrayLimit extends LargeObjectException {
  111. private static final long serialVersionUID = 1L;
  112. @Override
  113. public String getMessage() {
  114. return MessageFormat
  115. .format(JGitText.get().largeObjectExceedsByteArray,
  116. getObjectName());
  117. }
  118. }
  119. /** Object size exceeds the caller's upper limit. */
  120. public static class ExceedsLimit extends LargeObjectException {
  121. private static final long serialVersionUID = 1L;
  122. private final long limit;
  123. private final long size;
  124. /**
  125. * Construct an exception for a particular size being exceeded.
  126. *
  127. * @param limit
  128. * the limit the caller imposed on the object.
  129. * @param size
  130. * the actual size of the object.
  131. */
  132. public ExceedsLimit(long limit, long size) {
  133. this.limit = limit;
  134. this.size = size;
  135. }
  136. @Override
  137. public String getMessage() {
  138. return MessageFormat.format(JGitText.get().largeObjectExceedsLimit,
  139. getObjectName(), Long.valueOf(limit), Long.valueOf(size));
  140. }
  141. }
  142. }