source: josm/trunk/src/com/drew/lang/ByteArrayReader.java@ 14592

Last change on this file since 14592 was 13061, checked in by Don-vip, 8 years ago

fix #15505 - update to metadata-extractor 2.10.1

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 3.2 KB
RevLine 
[6127]1/*
[13061]2 * Copyright 2002-2017 Drew Noakes
[6127]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 *
[8132]18 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
[6127]20 */
21
22package com.drew.lang;
23
[13061]24import com.drew.lang.annotations.NotNull;
25
[10862]26import java.io.IOException;
27
[6127]28/**
29 * Provides methods to read specific values from a byte array, with a consistent, checked exception structure for
30 * issues.
[8132]31 * <p>
[6127]32 * By default, the reader operates with Motorola byte order (big endianness). This can be changed by calling
[8132]33 * <code>setMotorolaByteOrder(boolean)</code>.
34 *
35 * @author Drew Noakes https://drewnoakes.com
[6127]36 * */
[8132]37public class ByteArrayReader extends RandomAccessReader
[6127]38{
39 @NotNull
40 private final byte[] _buffer;
[13061]41 private final int _baseOffset;
[6127]42
[13061]43 @SuppressWarnings({ "ConstantConditions" })
44 @com.drew.lang.annotations.SuppressWarnings(value = "EI_EXPOSE_REP2", justification = "Design intent")
[6127]45 public ByteArrayReader(@NotNull byte[] buffer)
46 {
[13061]47 this(buffer, 0);
48 }
49
50 @SuppressWarnings({ "ConstantConditions" })
51 @com.drew.lang.annotations.SuppressWarnings(value = "EI_EXPOSE_REP2", justification = "Design intent")
52 public ByteArrayReader(@NotNull byte[] buffer, int baseOffset)
53 {
[6127]54 if (buffer == null)
55 throw new NullPointerException();
[13061]56 if (baseOffset < 0)
57 throw new IllegalArgumentException("Must be zero or greater");
[8132]58
[6127]59 _buffer = buffer;
[13061]60 _baseOffset = baseOffset;
[6127]61 }
62
63 @Override
[13061]64 public int toUnshiftedOffset(int localOffset)
65 {
66 return localOffset + _baseOffset;
67 }
68
69 @Override
[6127]70 public long getLength()
71 {
[13061]72 return _buffer.length - _baseOffset;
[6127]73 }
74
75 @Override
[13061]76 public byte getByte(int index) throws IOException
[6127]77 {
[13061]78 validateIndex(index, 1);
79 return _buffer[index + _baseOffset];
[6127]80 }
81
82 @Override
[8132]83 protected void validateIndex(int index, int bytesRequested) throws IOException
[6127]84 {
[8132]85 if (!isValidIndex(index, bytesRequested))
[13061]86 throw new BufferBoundsException(toUnshiftedOffset(index), bytesRequested, _buffer.length);
[6127]87 }
88
89 @Override
[8132]90 protected boolean isValidIndex(int index, int bytesRequested) throws IOException
[6127]91 {
[8132]92 return bytesRequested >= 0
93 && index >= 0
[13061]94 && (long)index + (long)bytesRequested - 1L < getLength();
[6127]95 }
96
97 @Override
98 @NotNull
[8132]99 public byte[] getBytes(int index, int count) throws IOException
[6127]100 {
[8132]101 validateIndex(index, count);
[6127]102
103 byte[] bytes = new byte[count];
[13061]104 System.arraycopy(_buffer, index + _baseOffset, bytes, 0, count);
[6127]105 return bytes;
106 }
107}
Note: See TracBrowser for help on using the repository browser.