LargeObjectException.java

  1. /*
  2.  * Copyright (C) 2010, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.errors;

  11. import java.text.MessageFormat;

  12. import org.eclipse.jgit.internal.JGitText;
  13. import org.eclipse.jgit.lib.AnyObjectId;
  14. import org.eclipse.jgit.lib.ObjectId;

  15. /**
  16.  * An object is too big to load into memory as a single byte array.
  17.  */
  18. public class LargeObjectException extends RuntimeException {
  19.     private static final long serialVersionUID = 1L;

  20.     private ObjectId objectId;

  21.     /**
  22.      * Create a large object exception, where the object isn't known.
  23.      */
  24.     public LargeObjectException() {
  25.         // Do nothing.
  26.     }

  27.     /**
  28.      * Create a large object exception, where the object isn't known.
  29.      *
  30.      * @param cause
  31.      *            the cause
  32.      * @since 4.10
  33.      */
  34.     public LargeObjectException(Throwable cause) {
  35.         initCause(cause);
  36.     }

  37.     /**
  38.      * Create a large object exception, naming the object that is too big.
  39.      *
  40.      * @param id
  41.      *            identity of the object that is too big to be loaded as a byte
  42.      *            array in this JVM.
  43.      */
  44.     public LargeObjectException(AnyObjectId id) {
  45.         setObjectId(id);
  46.     }

  47.     /**
  48.      * Get identity of the object that is too large; may be null
  49.      *
  50.      * @return identity of the object that is too large; may be null
  51.      */
  52.     public ObjectId getObjectId() {
  53.         return objectId;
  54.     }

  55.     /**
  56.      * Get the hex encoded name of the object, or 'unknown object'
  57.      *
  58.      * @return either the hex encoded name of the object, or 'unknown object'
  59.      */
  60.     protected String getObjectName() {
  61.         if (getObjectId() != null)
  62.             return getObjectId().name();
  63.         return JGitText.get().unknownObject;
  64.     }

  65.     /**
  66.      * Set the identity of the object, if its not already set.
  67.      *
  68.      * @param id
  69.      *            the id of the object that is too large to process.
  70.      */
  71.     public void setObjectId(AnyObjectId id) {
  72.         if (objectId == null)
  73.             objectId = id.copy();
  74.     }

  75.     /** {@inheritDoc} */
  76.     @Override
  77.     public String getMessage() {
  78.         return MessageFormat.format(JGitText.get().largeObjectException,
  79.                 getObjectName());
  80.     }

  81.     /** An error caused by the JVM being out of heap space. */
  82.     public static class OutOfMemory extends LargeObjectException {
  83.         private static final long serialVersionUID = 1L;

  84.         /**
  85.          * Construct a wrapper around the original OutOfMemoryError.
  86.          *
  87.          * @param cause
  88.          *            the original root cause.
  89.          */
  90.         public OutOfMemory(OutOfMemoryError cause) {
  91.             initCause(cause);
  92.         }

  93.         @Override
  94.         public String getMessage() {
  95.             return MessageFormat.format(JGitText.get().largeObjectOutOfMemory,
  96.                     getObjectName());
  97.         }
  98.     }

  99.     /** Object size exceeds JVM limit of 2 GiB per byte array. */
  100.     public static class ExceedsByteArrayLimit extends LargeObjectException {
  101.         private static final long serialVersionUID = 1L;

  102.         @Override
  103.         public String getMessage() {
  104.             return MessageFormat
  105.                     .format(JGitText.get().largeObjectExceedsByteArray,
  106.                             getObjectName());
  107.         }
  108.     }

  109.     /** Object size exceeds the caller's upper limit. */
  110.     public static class ExceedsLimit extends LargeObjectException {
  111.         private static final long serialVersionUID = 1L;

  112.         private final long limit;

  113.         private final long size;

  114.         /**
  115.          * Construct an exception for a particular size being exceeded.
  116.          *
  117.          * @param limit
  118.          *            the limit the caller imposed on the object.
  119.          * @param size
  120.          *            the actual size of the object.
  121.          */
  122.         public ExceedsLimit(long limit, long size) {
  123.             this.limit = limit;
  124.             this.size = size;
  125.         }

  126.         @Override
  127.         public String getMessage() {
  128.             return MessageFormat.format(JGitText.get().largeObjectExceedsLimit,
  129.                     getObjectName(), Long.valueOf(limit), Long.valueOf(size));
  130.         }
  131.     }
  132. }