InvalidLongObjectIdException.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.lfs.errors;

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

  14. import java.text.MessageFormat;

  15. import org.eclipse.jgit.lfs.internal.LfsText;

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

  23.     /**
  24.      * Create exception with bytes of the invalid object id.
  25.      *
  26.      * @param bytes containing the invalid id.
  27.      * @param offset in the byte array where the error occurred.
  28.      * @param length of the sequence of invalid bytes.
  29.      */
  30.     public InvalidLongObjectIdException(byte[] bytes, int offset, int length) {
  31.         super(MessageFormat.format(LfsText.get().invalidLongId,
  32.                 asAscii(bytes, offset, length)));
  33.     }

  34.     /**
  35.      * <p>Constructor for InvalidLongObjectIdException.</p>
  36.      *
  37.      * @param idString
  38.      *            String containing the invalid id
  39.      */
  40.     public InvalidLongObjectIdException(String idString) {
  41.         super(MessageFormat.format(LfsText.get().invalidLongId, idString));
  42.     }

  43.     private static String asAscii(byte[] bytes, int offset, int length) {
  44.         try {
  45.             return new String(bytes, offset, length, US_ASCII);
  46.         } catch (StringIndexOutOfBoundsException e) {
  47.             return ""; //$NON-NLS-1$
  48.         }
  49.     }
  50. }