View Javadoc
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  
11  package org.eclipse.jgit.errors;
12  
13  import java.text.MessageFormat;
14  
15  import org.eclipse.jgit.internal.JGitText;
16  import org.eclipse.jgit.lib.AnyObjectId;
17  import org.eclipse.jgit.lib.ObjectId;
18  
19  /**
20   * An object is too big to load into memory as a single byte array.
21   */
22  public class LargeObjectException extends RuntimeException {
23  	private static final long serialVersionUID = 1L;
24  
25  	private ObjectId objectId;
26  
27  	/**
28  	 * Create a large object exception, where the object isn't known.
29  	 */
30  	public LargeObjectException() {
31  		// Do nothing.
32  	}
33  
34  	/**
35  	 * Create a large object exception, where the object isn't known.
36  	 *
37  	 * @param cause
38  	 *            the cause
39  	 * @since 4.10
40  	 */
41  	public LargeObjectException(Throwable cause) {
42  		initCause(cause);
43  	}
44  
45  	/**
46  	 * Create a large object exception, naming the object that is too big.
47  	 *
48  	 * @param id
49  	 *            identity of the object that is too big to be loaded as a byte
50  	 *            array in this JVM.
51  	 */
52  	public LargeObjectException(AnyObjectId id) {
53  		setObjectId(id);
54  	}
55  
56  	/**
57  	 * Get identity of the object that is too large; may be null
58  	 *
59  	 * @return identity of the object that is too large; may be null
60  	 */
61  	public ObjectId getObjectId() {
62  		return objectId;
63  	}
64  
65  	/**
66  	 * Get the hex encoded name of the object, or 'unknown object'
67  	 *
68  	 * @return either the hex encoded name of the object, or 'unknown object'
69  	 */
70  	protected String getObjectName() {
71  		if (getObjectId() != null)
72  			return getObjectId().name();
73  		return JGitText.get().unknownObject;
74  	}
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  
87  	/** {@inheritDoc} */
88  	@Override
89  	public String getMessage() {
90  		return MessageFormat.format(JGitText.get().largeObjectException,
91  				getObjectName());
92  	}
93  
94  	/** An error caused by the JVM being out of heap space. */
95  	public static class OutOfMemory extends LargeObjectException {
96  		private static final long serialVersionUID = 1L;
97  
98  		/**
99  		 * Construct a wrapper around the original OutOfMemoryError.
100 		 *
101 		 * @param cause
102 		 *            the original root cause.
103 		 */
104 		public OutOfMemory(OutOfMemoryError cause) {
105 			initCause(cause);
106 		}
107 
108 		@Override
109 		public String getMessage() {
110 			return MessageFormat.format(JGitText.get().largeObjectOutOfMemory,
111 					getObjectName());
112 		}
113 	}
114 
115 	/** Object size exceeds JVM limit of 2 GiB per byte array. */
116 	public static class ExceedsByteArrayLimit extends LargeObjectException {
117 		private static final long serialVersionUID = 1L;
118 
119 		@Override
120 		public String getMessage() {
121 			return MessageFormat
122 					.format(JGitText.get().largeObjectExceedsByteArray,
123 							getObjectName());
124 		}
125 	}
126 
127 	/** Object size exceeds the caller's upper limit. */
128 	public static class ExceedsLimit extends LargeObjectException {
129 		private static final long serialVersionUID = 1L;
130 
131 		private final long limit;
132 
133 		private final long size;
134 
135 		/**
136 		 * Construct an exception for a particular size being exceeded.
137 		 *
138 		 * @param limit
139 		 *            the limit the caller imposed on the object.
140 		 * @param size
141 		 *            the actual size of the object.
142 		 */
143 		public ExceedsLimit(long limit, long size) {
144 			this.limit = limit;
145 			this.size = size;
146 		}
147 
148 		@Override
149 		public String getMessage() {
150 			return MessageFormat.format(JGitText.get().largeObjectExceedsLimit,
151 					getObjectName(), Long.valueOf(limit), Long.valueOf(size));
152 		}
153 	}
154 }