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