View Javadoc
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.lfs.errors;
14  
15  import static java.nio.charset.StandardCharsets.US_ASCII;
16  
17  import java.text.MessageFormat;
18  
19  import org.eclipse.jgit.lfs.internal.LfsText;
20  
21  /**
22   * Thrown when an invalid long object id is passed in as an argument.
23   *
24   * @since 4.3
25   */
26  public class InvalidLongObjectIdException extends IllegalArgumentException {
27  	private static final long serialVersionUID = 1L;
28  
29  	/**
30  	 * Create exception with bytes of the invalid object id.
31  	 *
32  	 * @param bytes containing the invalid id.
33  	 * @param offset in the byte array where the error occurred.
34  	 * @param length of the sequence of invalid bytes.
35  	 */
36  	public InvalidLongObjectIdException(byte[] bytes, int offset, int length) {
37  		super(MessageFormat.format(LfsText.get().invalidLongId,
38  				asAscii(bytes, offset, length)));
39  	}
40  
41  	/**
42  	 * <p>Constructor for InvalidLongObjectIdException.</p>
43  	 *
44  	 * @param idString
45  	 *            String containing the invalid id
46  	 */
47  	public InvalidLongObjectIdException(String idString) {
48  		super(MessageFormat.format(LfsText.get().invalidLongId, idString));
49  	}
50  
51  	private static String asAscii(byte[] bytes, int offset, int length) {
52  		try {
53  			return new String(bytes, offset, length, US_ASCII);
54  		} catch (StringIndexOutOfBoundsException e) {
55  			return ""; //$NON-NLS-1$
56  		}
57  	}
58  }