1 | /*
|
---|
2 | * Copyright 2002-2016 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 | public SequentialByteArrayReader(@NotNull byte[] bytes)
|
---|
40 | {
|
---|
41 | this(bytes, 0);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public SequentialByteArrayReader(@NotNull byte[] bytes, int baseIndex)
|
---|
45 | {
|
---|
46 | if (bytes == null)
|
---|
47 | throw new NullPointerException();
|
---|
48 |
|
---|
49 | _bytes = bytes;
|
---|
50 | _index = baseIndex;
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | protected byte getByte() throws IOException
|
---|
55 | {
|
---|
56 | if (_index >= _bytes.length) {
|
---|
57 | throw new EOFException("End of data reached.");
|
---|
58 | }
|
---|
59 | return _bytes[_index++];
|
---|
60 | }
|
---|
61 |
|
---|
62 | @NotNull
|
---|
63 | @Override
|
---|
64 | public byte[] getBytes(int count) throws IOException
|
---|
65 | {
|
---|
66 | if (_index + count > _bytes.length) {
|
---|
67 | throw new EOFException("End of data reached.");
|
---|
68 | }
|
---|
69 |
|
---|
70 | byte[] bytes = new byte[count];
|
---|
71 | System.arraycopy(_bytes, _index, bytes, 0, count);
|
---|
72 | _index += count;
|
---|
73 |
|
---|
74 | return bytes;
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public void skip(long n) throws IOException
|
---|
79 | {
|
---|
80 | if (n < 0) {
|
---|
81 | throw new IllegalArgumentException("n must be zero or greater.");
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (_index + n > _bytes.length) {
|
---|
85 | throw new EOFException("End of data reached.");
|
---|
86 | }
|
---|
87 |
|
---|
88 | _index += n;
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public boolean trySkip(long n) throws IOException
|
---|
93 | {
|
---|
94 | if (n < 0) {
|
---|
95 | throw new IllegalArgumentException("n must be zero or greater.");
|
---|
96 | }
|
---|
97 |
|
---|
98 | _index += n;
|
---|
99 |
|
---|
100 | if (_index > _bytes.length) {
|
---|
101 | _index = _bytes.length;
|
---|
102 | return false;
|
---|
103 | }
|
---|
104 |
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 | }
|
---|