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