InvalidObjectIdException.java

  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. package org.eclipse.jgit.errors;

  13. import static java.nio.charset.StandardCharsets.US_ASCII;

  14. import java.text.MessageFormat;

  15. import org.eclipse.jgit.internal.JGitText;

  16. /**
  17.  * Thrown when an invalid object id is passed in as an argument.
  18.  */
  19. public class InvalidObjectIdException extends IllegalArgumentException {
  20.     private static final long serialVersionUID = 1L;

  21.     /**
  22.      * Create exception with bytes of the invalid object id.
  23.      *
  24.      * @param bytes containing the invalid id.
  25.      * @param offset in the byte array where the error occurred.
  26.      * @param length of the sequence of invalid bytes.
  27.      */
  28.     public InvalidObjectIdException(byte[] bytes, int offset, int length) {
  29.         super(msg(bytes, offset, length));
  30.     }

  31.     /**
  32.      * Constructor for InvalidObjectIdException
  33.      *
  34.      * @param id
  35.      *            the invalid id.
  36.      * @since 4.1
  37.      */
  38.     public InvalidObjectIdException(String id) {
  39.         super(MessageFormat.format(JGitText.get().invalidId, id));
  40.     }

  41.     private static String msg(byte[] bytes, int offset, int length) {
  42.         try {
  43.             return MessageFormat.format(
  44.                     JGitText.get().invalidId,
  45.                     new String(bytes, offset, length, US_ASCII));
  46.         } catch (StringIndexOutOfBoundsException e) {
  47.             return JGitText.get().invalidId0;
  48.         }
  49.     }
  50. }