| 1 | /*
|
|---|
| 2 | * Copyright 2002-2012 Drew Noakes
|
|---|
| 3 | *
|
|---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 5 | * you may not use this file except in compliance with the License.
|
|---|
| 6 | * You may obtain a copy of the License at
|
|---|
| 7 | *
|
|---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 9 | *
|
|---|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
|---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 13 | * See the License for the specific language governing permissions and
|
|---|
| 14 | * limitations under the License.
|
|---|
| 15 | *
|
|---|
| 16 | * More information about this project is available at:
|
|---|
| 17 | *
|
|---|
| 18 | * http://drewnoakes.com/code/exif/
|
|---|
| 19 | * http://code.google.com/p/metadata-extractor/
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | package com.drew.lang;
|
|---|
| 23 |
|
|---|
| 24 | import com.drew.lang.annotations.NotNull;
|
|---|
| 25 |
|
|---|
| 26 | import java.io.IOException;
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * A checked replacement for IndexOutOfBoundsException. Used by BufferReader.
|
|---|
| 30 | *
|
|---|
| 31 | * @author Drew Noakes http://drewnoakes.com
|
|---|
| 32 | */
|
|---|
| 33 | public final class BufferBoundsException extends Exception
|
|---|
| 34 | {
|
|---|
| 35 | private static final long serialVersionUID = 2911102837808946396L;
|
|---|
| 36 |
|
|---|
| 37 | public BufferBoundsException(@NotNull byte[] buffer, int index, int bytesRequested)
|
|---|
| 38 | {
|
|---|
| 39 | super(getMessage(buffer, index, bytesRequested));
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | public BufferBoundsException(final String message)
|
|---|
| 43 | {
|
|---|
| 44 | super(message);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | public BufferBoundsException(final String message, final IOException innerException)
|
|---|
| 48 | {
|
|---|
| 49 | super(message, innerException);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | private static String getMessage(@NotNull byte[] buffer, int index, int bytesRequested)
|
|---|
| 53 | {
|
|---|
| 54 | if (index < 0)
|
|---|
| 55 | return String.format("Attempt to read from buffer using a negative index (%s)", index);
|
|---|
| 56 |
|
|---|
| 57 | return String.format("Attempt to read %d byte%s from beyond end of buffer (requested index: %d, max index: %d)",
|
|---|
| 58 | bytesRequested, bytesRequested==1?"":"s", index, buffer.length - 1);
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|