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