1 /*
2 * Copyright (C) 2009, Jonas Fonseca <fonseca@diku.dk>
3 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
4 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
5 *
6 * This program and the accompanying materials are made available under the
7 * terms of the Eclipse Distribution License v. 1.0 which is available at
8 * https://www.eclipse.org/org/documents/edl-v10.php.
9 *
10 * SPDX-License-Identifier: BSD-3-Clause
11 */
12
13 package org.eclipse.jgit.errors;
14
15 import static java.nio.charset.StandardCharsets.US_ASCII;
16
17 import java.text.MessageFormat;
18
19 import org.eclipse.jgit.internal.JGitText;
20
21 /**
22 * Thrown when an invalid object id is passed in as an argument.
23 */
24 public class InvalidObjectIdException extends IllegalArgumentException {
25 private static final long serialVersionUID = 1L;
26
27 /**
28 * Create exception with bytes of the invalid object id.
29 *
30 * @param bytes containing the invalid id.
31 * @param offset in the byte array where the error occurred.
32 * @param length of the sequence of invalid bytes.
33 */
34 public InvalidObjectIdException(byte[] bytes, int offset, int length) {
35 super(msg(bytes, offset, length));
36 }
37
38 /**
39 * Constructor for InvalidObjectIdException
40 *
41 * @param id
42 * the invalid id.
43 * @since 4.1
44 */
45 public InvalidObjectIdException(String id) {
46 super(MessageFormat.format(JGitText.get().invalidId, id));
47 }
48
49 private static String msg(byte[] bytes, int offset, int length) {
50 try {
51 return MessageFormat.format(
52 JGitText.get().invalidId,
53 new String(bytes, offset, length, US_ASCII));
54 } catch (StringIndexOutOfBoundsException e) {
55 return JGitText.get().invalidId0;
56 }
57 }
58 }