[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 |
|
---|
[10862] | 24 | import java.io.IOException;
|
---|
| 25 |
|
---|
[6127] | 26 | import com.drew.lang.annotations.NotNull;
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * Provides methods to read specific values from a byte array, with a consistent, checked exception structure for
|
---|
| 30 | * issues.
|
---|
[8132] | 31 | * <p>
|
---|
[6127] | 32 | * By default, the reader operates with Motorola byte order (big endianness). This can be changed by calling
|
---|
[8132] | 33 | * <code>setMotorolaByteOrder(boolean)</code>.
|
---|
| 34 | *
|
---|
| 35 | * @author Drew Noakes https://drewnoakes.com
|
---|
[6127] | 36 | * */
|
---|
[8132] | 37 | public class ByteArrayReader extends RandomAccessReader
|
---|
[6127] | 38 | {
|
---|
| 39 | @NotNull
|
---|
| 40 | private final byte[] _buffer;
|
---|
| 41 |
|
---|
| 42 | public ByteArrayReader(@NotNull byte[] buffer)
|
---|
| 43 | {
|
---|
| 44 | if (buffer == null)
|
---|
| 45 | throw new NullPointerException();
|
---|
[8132] | 46 |
|
---|
[6127] | 47 | _buffer = buffer;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | @Override
|
---|
| 51 | public long getLength()
|
---|
| 52 | {
|
---|
| 53 | return _buffer.length;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | @Override
|
---|
[8132] | 57 | protected byte getByte(int index) throws IOException
|
---|
[6127] | 58 | {
|
---|
| 59 | return _buffer[index];
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | @Override
|
---|
[8132] | 63 | protected void validateIndex(int index, int bytesRequested) throws IOException
|
---|
[6127] | 64 | {
|
---|
[8132] | 65 | if (!isValidIndex(index, bytesRequested))
|
---|
| 66 | throw new BufferBoundsException(index, bytesRequested, _buffer.length);
|
---|
[6127] | 67 | }
|
---|
| 68 |
|
---|
| 69 | @Override
|
---|
[8132] | 70 | protected boolean isValidIndex(int index, int bytesRequested) throws IOException
|
---|
[6127] | 71 | {
|
---|
[8132] | 72 | return bytesRequested >= 0
|
---|
| 73 | && index >= 0
|
---|
[10862] | 74 | && (long)index + (long)bytesRequested - 1L < _buffer.length;
|
---|
[6127] | 75 | }
|
---|
| 76 |
|
---|
| 77 | @Override
|
---|
| 78 | @NotNull
|
---|
[8132] | 79 | public byte[] getBytes(int index, int count) throws IOException
|
---|
[6127] | 80 | {
|
---|
[8132] | 81 | validateIndex(index, count);
|
---|
[6127] | 82 |
|
---|
| 83 | byte[] bytes = new byte[count];
|
---|
| 84 | System.arraycopy(_buffer, index, bytes, 0, count);
|
---|
| 85 | return bytes;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|