Debian Codegraph @

src/com/drew/lang/RandomAccessFileReader.java

raw symbols SCIP
1 /*
2 * Copyright 2002-2019 Drew Noakes and contributors
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 import java.io.RandomAccessFile;
28
29 /**
30 * Provides methods to read specific values from a {@link RandomAccessFile}, with a consistent, checked exception structure for
31 * issues.
32 *
33 * @author Drew Noakes https://drewnoakes.com
34 * */
35 public class RandomAccessFileReader extends RandomAccessReader
36 {
37 @NotNull
38 private final RandomAccessFile _file;
39 private final long _length;
40 private int _currentIndex;
41
42 private final int _baseOffset;
43
44 @SuppressWarnings({ "ConstantConditions" })
45 @com.drew.lang.annotations.SuppressWarnings(value = "EI_EXPOSE_REP2", justification = "Design intent")
46 public RandomAccessFileReader(@NotNull RandomAccessFile file) throws IOException
47 {
48 this(file, 0);
49 }
50
51 @SuppressWarnings({ "ConstantConditions" })
52 @com.drew.lang.annotations.SuppressWarnings(value = "EI_EXPOSE_REP2", justification = "Design intent")
53 public RandomAccessFileReader(@NotNull RandomAccessFile file, int baseOffset) throws IOException
54 {
55 if (file == null)
56 throw new NullPointerException();
57
58 _file = file;
59 _baseOffset = baseOffset;
60 _length = _file.length();
61 }
62
63 @Override
64 public int toUnshiftedOffset(int localOffset)
65 {
66 return localOffset + _baseOffset;
67 }
68
69 @Override
70 public long getLength()
71 {
72 return _length;
73 }
74
75 @Override
76 public byte getByte(int index) throws IOException
77 {
78 if (index != _currentIndex)
79 seek(index);
80
81 final int b = _file.read();
82 if (b < 0)
83 throw new BufferBoundsException("Unexpected end of file encountered.");
84 assert (b <= 0xff);
85 _currentIndex++;
86 return (byte)b;
87 }
88
89 @Override
90 @NotNull
91 public byte[] getBytes(int index, int count) throws IOException
92 {
93 validateIndex(index, count);
94
95 if (index != _currentIndex)
96 seek(index);
97
98 byte[] bytes = new byte[count];
99 final int bytesRead = _file.read(bytes);
100 _currentIndex += bytesRead;
101 if (bytesRead != count)
102 throw new BufferBoundsException("Unexpected end of file encountered.");
103 return bytes;
104 }
105
106 private void seek(final int index) throws IOException
107 {
108 if (index == _currentIndex)
109 return;
110
111 _file.seek(index);
112 _currentIndex = index;
113 }
114
115 @Override
116 protected boolean isValidIndex(int index, int bytesRequested) throws IOException
117 {
118 return bytesRequested >= 0
119 && index >= 0
120 && (long)index + (long)bytesRequested - 1L < _length;
121 }
122
123 @Override
124 protected void validateIndex(final int index, final int bytesRequested) throws IOException
125 {
126 if (!isValidIndex(index, bytesRequested))
127 throw new BufferBoundsException(index, bytesRequested, _length);
128 }
129 }