1 | /*
|
---|
2 | * Copyright 2002-2017 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.EOFException;
|
---|
27 | import java.io.IOException;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | *
|
---|
31 | * @author Drew Noakes https://drewnoakes.com
|
---|
32 | */
|
---|
33 | public class SequentialByteArrayReader extends SequentialReader
|
---|
34 | {
|
---|
35 | @NotNull
|
---|
36 | private final byte[] _bytes;
|
---|
37 | private int _index;
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public long getPosition()
|
---|
41 | {
|
---|
42 | return _index;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public SequentialByteArrayReader(@NotNull byte[] bytes)
|
---|
46 | {
|
---|
47 | this(bytes, 0);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @SuppressWarnings("ConstantConditions")
|
---|
51 | public SequentialByteArrayReader(@NotNull byte[] bytes, int baseIndex)
|
---|
52 | {
|
---|
53 | if (bytes == null)
|
---|
54 | throw new NullPointerException();
|
---|
55 |
|
---|
56 | _bytes = bytes;
|
---|
57 | _index = baseIndex;
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public byte getByte() throws IOException
|
---|
62 | {
|
---|
63 | if (_index >= _bytes.length) {
|
---|
64 | throw new EOFException("End of data reached.");
|
---|
65 | }
|
---|
66 | return _bytes[_index++];
|
---|
67 | }
|
---|
68 |
|
---|
69 | @NotNull
|
---|
70 | @Override
|
---|
71 | public byte[] getBytes(int count) throws IOException
|
---|
72 | {
|
---|
73 | if (_index + count > _bytes.length) {
|
---|
74 | throw new EOFException("End of data reached.");
|
---|
75 | }
|
---|
76 |
|
---|
77 | byte[] bytes = new byte[count];
|
---|
78 | System.arraycopy(_bytes, _index, bytes, 0, count);
|
---|
79 | _index += count;
|
---|
80 |
|
---|
81 | return bytes;
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public void getBytes(@NotNull byte[] buffer, int offset, int count) throws IOException
|
---|
86 | {
|
---|
87 | if (_index + count > _bytes.length) {
|
---|
88 | throw new EOFException("End of data reached.");
|
---|
89 | }
|
---|
90 |
|
---|
91 | System.arraycopy(_bytes, _index, buffer, offset, count);
|
---|
92 | _index += count;
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public void skip(long n) throws IOException
|
---|
97 | {
|
---|
98 | if (n < 0) {
|
---|
99 | throw new IllegalArgumentException("n must be zero or greater.");
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (_index + n > _bytes.length) {
|
---|
103 | throw new EOFException("End of data reached.");
|
---|
104 | }
|
---|
105 |
|
---|
106 | _index += n;
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public boolean trySkip(long n) throws IOException
|
---|
111 | {
|
---|
112 | if (n < 0) {
|
---|
113 | throw new IllegalArgumentException("n must be zero or greater.");
|
---|
114 | }
|
---|
115 |
|
---|
116 | _index += n;
|
---|
117 |
|
---|
118 | if (_index > _bytes.length) {
|
---|
119 | _index = _bytes.length;
|
---|
120 | return false;
|
---|
121 | }
|
---|
122 |
|
---|
123 | return true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Override
|
---|
127 | public int available() {
|
---|
128 | return _bytes.length - _index;
|
---|
129 | }
|
---|
130 | }
|
---|