[6127] | 1 | /*
|
---|
[10862] | 2 | * Copyright 2002-2016 Drew Noakes
|
---|
[6127] | 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 | *
|
---|
[8132] | 18 | * https://drewnoakes.com/code/exif/
|
---|
| 19 | * https://github.com/drewnoakes/metadata-extractor
|
---|
[6127] | 20 | */
|
---|
| 21 |
|
---|
| 22 | package com.drew.lang;
|
---|
| 23 |
|
---|
| 24 | import java.io.IOException;
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
[8132] | 27 | * A checked replacement for {@link IndexOutOfBoundsException}. Used by {@link RandomAccessReader}.
|
---|
| 28 | *
|
---|
| 29 | * @author Drew Noakes https://drewnoakes.com
|
---|
[6127] | 30 | */
|
---|
[8132] | 31 | public final class BufferBoundsException extends IOException
|
---|
[6127] | 32 | {
|
---|
| 33 | private static final long serialVersionUID = 2911102837808946396L;
|
---|
| 34 |
|
---|
[8132] | 35 | public BufferBoundsException(int index, int bytesRequested, long bufferLength)
|
---|
[6127] | 36 | {
|
---|
[8132] | 37 | super(getMessage(index, bytesRequested, bufferLength));
|
---|
[6127] | 38 | }
|
---|
| 39 |
|
---|
| 40 | public BufferBoundsException(final String message)
|
---|
| 41 | {
|
---|
| 42 | super(message);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[8132] | 45 | private static String getMessage(int index, int bytesRequested, long bufferLength)
|
---|
[6127] | 46 | {
|
---|
| 47 | if (index < 0)
|
---|
[8132] | 48 | return String.format("Attempt to read from buffer using a negative index (%d)", index);
|
---|
[6127] | 49 |
|
---|
[8132] | 50 | if (bytesRequested < 0)
|
---|
| 51 | return String.format("Number of requested bytes cannot be negative (%d)", bytesRequested);
|
---|
| 52 |
|
---|
| 53 | if ((long)index + (long)bytesRequested - 1L > (long)Integer.MAX_VALUE)
|
---|
| 54 | return String.format("Number of requested bytes summed with starting index exceed maximum range of signed 32 bit integers (requested index: %d, requested count: %d)", index, bytesRequested);
|
---|
| 55 |
|
---|
| 56 | return String.format("Attempt to read from beyond end of underlying data source (requested index: %d, requested count: %d, max index: %d)",
|
---|
| 57 | index, bytesRequested, bufferLength - 1);
|
---|
[6127] | 58 | }
|
---|
| 59 | }
|
---|