File: RandomAccessFileReader.java

package info (click to toggle)
libmetadata-extractor-java 2.6.4-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,124 kB
  • ctags: 2,408
  • sloc: java: 15,158; xml: 110; sh: 93; makefile: 18
file content (332 lines) | stat: -rw-r--r-- 10,307 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
 * Copyright 2002-2012 Drew Noakes
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 * More information about this project is available at:
 *
 *    http://drewnoakes.com/code/exif/
 *    http://code.google.com/p/metadata-extractor/
 */

package com.drew.lang;

import com.drew.lang.annotations.NotNull;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;

/**
 * Provides methods to read specific values from a byte array, with a consistent, checked exception structure for
 * issues.
 * <p/>
 * By default, the reader operates with Motorola byte order (big endianness).  This can be changed by calling
 * {@see setMotorolaByteOrder(boolean)}.
 * 
 * @author Drew Noakes http://drewnoakes.com
 * */
public class RandomAccessFileReader implements BufferReader
{
    @NotNull
    private final RandomAccessFile _file;
    private final long _length;
    private int _currentIndex;
    private boolean _isMotorolaByteOrder = true;

    @SuppressWarnings({ "ConstantConditions" })
    @com.drew.lang.annotations.SuppressWarnings(value = "EI_EXPOSE_REP2", justification = "Design intent")
    public RandomAccessFileReader(@NotNull RandomAccessFile file) throws IOException
    {
        if (file == null)
            throw new NullPointerException();
        
        _file = file;
        _length = _file.length();
    }

    @Override
    public long getLength()
    {
        return _length;
    }


    @Override
    public void setMotorolaByteOrder(boolean motorolaByteOrder)
    {
        _isMotorolaByteOrder = motorolaByteOrder;
    }

    @Override
    public boolean isMotorolaByteOrder()
    {
        return _isMotorolaByteOrder;
    }

    private byte read() throws BufferBoundsException
    {
        final int b;
        try {
            b = _file.read();
        } catch (IOException e) {
            throw new BufferBoundsException("IOException reading from file.", e);
        }

        if (b < 0)
            throw new BufferBoundsException("Unexpected end of file encountered.");
        assert(b <= 0xff);
        _currentIndex++;
        return (byte) b;
    }

    private void seek(final int index) throws BufferBoundsException
    {
        if (index == _currentIndex)
            return;
        try {
            _file.seek(index);
            _currentIndex = index;
        } catch (IOException e) {
            throw new BufferBoundsException("IOException seeking in file.", e);
        }
    }

    @Override
    public short getUInt8(int index) throws BufferBoundsException
    {
        checkBounds(index, 1);
        seek(index);

        return (short) (read() & 255);
    }

    @Override
    public byte getInt8(int index) throws BufferBoundsException
    {
        checkBounds(index, 1);
        seek(index);

        return read();
    }

    @Override
    public int getUInt16(int index) throws BufferBoundsException
    {
        checkBounds(index, 2);
        seek(index);

        if (_isMotorolaByteOrder) {
            // Motorola - MSB first
            return (read() << 8 & 0xFF00) |
                   (read()      & 0xFF);
        } else {
            // Intel ordering - LSB first
            return (read()      & 0xFF) |
                   (read() << 8 & 0xFF00);
        }
    }

    @Override
    public short getInt16(int index) throws BufferBoundsException
    {
        checkBounds(index, 2);
        seek(index);

        if (_isMotorolaByteOrder) {
            // Motorola - MSB first
            return (short) (((short) read() << 8 & (short)0xFF00) |
                            ((short) read()      & (short)0xFF));
        } else {
            // Intel ordering - LSB first
            return (short) (((short) read()      & (short)0xFF) |
                            ((short) read() << 8 & (short)0xFF00));
        }
    }

    @Override
    public long getUInt32(int index) throws BufferBoundsException
    {
        checkBounds(index, 4);
        seek(index);

        if (_isMotorolaByteOrder) {
            // Motorola - MSB first (big endian)
            return (((long) read()) << 24 & 0xFF000000L) |
                   (((long) read()) << 16 & 0xFF0000L) |
                   (((long) read()) << 8  & 0xFF00L) |
                   (((long) read())       & 0xFFL);
        } else {
            // Intel ordering - LSB first (little endian)
            return (((long) read())       & 0xFFL) |
                   (((long) read()) << 8  & 0xFF00L) |
                   (((long) read()) << 16 & 0xFF0000L) |
                   (((long) read()) << 24 & 0xFF000000L);
        }
    }

    @Override
    public int getInt32(int index) throws BufferBoundsException
    {
        checkBounds(index, 4);
        seek(index);

        if (_isMotorolaByteOrder) {
            // Motorola - MSB first (big endian)
            return (read() << 24 & 0xFF000000) |
                   (read() << 16 & 0xFF0000) |
                   (read() << 8  & 0xFF00) |
                   (read()       & 0xFF);
        } else {
            // Intel ordering - LSB first (little endian)
            return (read()       & 0xFF) |
                   (read() << 8  & 0xFF00) |
                   (read() << 16 & 0xFF0000) |
                   (read() << 24 & 0xFF000000);

        }
    }

    @Override
    public long getInt64(int index) throws BufferBoundsException
    {
        checkBounds(index, 8);
        seek(index);

        if (_isMotorolaByteOrder) {
            // Motorola - MSB first
            return ((long) read() << 56 & 0xFF00000000000000L) |
                   ((long) read() << 48 & 0xFF000000000000L) |
                   ((long) read() << 40 & 0xFF0000000000L) |
                   ((long) read() << 32 & 0xFF00000000L) |
                   ((long) read() << 24 & 0xFF000000L) |
                   ((long) read() << 16 & 0xFF0000L) |
                   ((long) read() << 8  & 0xFF00L) |
                   ((long) read()       & 0xFFL);
        } else {
            // Intel ordering - LSB first
            return ((long) read()       & 0xFFL) |
                   ((long) read() << 8  & 0xFF00L) |
                   ((long) read() << 16 & 0xFF0000L) |
                   ((long) read() << 24 & 0xFF000000L) |
                   ((long) read() << 32 & 0xFF00000000L) |
                   ((long) read() << 40 & 0xFF0000000000L) |
                   ((long) read() << 48 & 0xFF000000000000L) |
                   ((long) read() << 56 & 0xFF00000000000000L);

        }
    }

    @Override
    public float getS15Fixed16(int index) throws BufferBoundsException
    {
        checkBounds(index, 4);
        seek(index);

        if (_isMotorolaByteOrder) {
            float res = (read() & 255) << 8 |
                        (read() & 255);
            int d =     (read() & 255) << 8 |
                        (read() & 255);
            return (float)(res + d/65536.0);
        } else {
            // this particular branch is untested
            int d =     (read() & 255) |
                        (read() & 255) << 8;
            float res = (read() & 255) |
                        (read() & 255) << 8;
            return (float)(res + d/65536.0);
        }
    }

    @Override
    public float getFloat32(int index) throws BufferBoundsException
    {
        return Float.intBitsToFloat(getInt32(index));
    }

    @Override
    public double getDouble64(int index) throws BufferBoundsException
    {
        return Double.longBitsToDouble(getInt64(index));
    }
    
    @Override
    @NotNull
    public byte[] getBytes(int index, int count) throws BufferBoundsException
    {
        checkBounds(index, count);
        seek(index);

        byte[] bytes = new byte[count];
        final int bytesRead;
        try {
            bytesRead = _file.read(bytes);
            _currentIndex += bytesRead;
        } catch (IOException e) {
            throw new BufferBoundsException("Unexpected end of file encountered.", e);
        }
        if (bytesRead != count)
            throw new BufferBoundsException("Unexpected end of file encountered.");
        return bytes;
    }

    @Override
    @NotNull
    public String getString(int index, int bytesRequested) throws BufferBoundsException
    {
        return new String(getBytes(index, bytesRequested));
    }

    @Override
    @NotNull
    public String getString(int index, int bytesRequested, String charset) throws BufferBoundsException
    {
        checkBounds(index, bytesRequested);

        byte[] bytes = getBytes(index, bytesRequested);
        try {
            return new String(bytes, charset);
        } catch (UnsupportedEncodingException e) {
            return new String(bytes);
        }
    }

    @Override
    @NotNull
    public String getNullTerminatedString(int index, int maxLengthBytes) throws BufferBoundsException
    {
        // NOTE currently only really suited to single-byte character strings

        checkBounds(index, maxLengthBytes);
        seek(index);

        // Check for null terminators
        int length = 0;
        while ((index + length) < _length && read() != '\0' && length < maxLengthBytes)
            length++;

        byte[] bytes = getBytes(index, length);
        return new String(bytes);
    }

    private void checkBounds(final int index, final int bytesRequested) throws BufferBoundsException
    {
        if (bytesRequested < 0)
            throw new BufferBoundsException("Requested negative number of bytes.");
        if (index < 0)
            throw new BufferBoundsException("Requested data from a negative index within the file.");
        if ((long)index + (long)bytesRequested - 1L >= _length)
            throw new BufferBoundsException("Requested data from beyond the end of the file.");
    }
}