Changeset 8132 in josm for trunk/src/com/drew/lang/ByteArrayReader.java
- Timestamp:
- 2015-03-10T01:17:39+01:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/drew/lang/ByteArrayReader.java
r6127 r8132 1 1 /* 2 * Copyright 2002-201 2Drew Noakes2 * Copyright 2002-2015 Drew Noakes 3 3 * 4 4 * Licensed under the Apache License, Version 2.0 (the "License"); … … 16 16 * More information about this project is available at: 17 17 * 18 * http ://drewnoakes.com/code/exif/19 * http ://code.google.com/p/metadata-extractor/18 * https://drewnoakes.com/code/exif/ 19 * https://github.com/drewnoakes/metadata-extractor 20 20 */ 21 21 … … 24 24 import com.drew.lang.annotations.NotNull; 25 25 26 import java.io. UnsupportedEncodingException;26 import java.io.IOException; 27 27 28 28 /** 29 29 * Provides methods to read specific values from a byte array, with a consistent, checked exception structure for 30 30 * issues. 31 * <p />31 * <p> 32 32 * By default, the reader operates with Motorola byte order (big endianness). This can be changed by calling 33 * {@see setMotorolaByteOrder(boolean)}.34 * 35 * @author Drew Noakes http ://drewnoakes.com33 * <code>setMotorolaByteOrder(boolean)</code>. 34 * 35 * @author Drew Noakes https://drewnoakes.com 36 36 * */ 37 public class ByteArrayReader implements BufferReader37 public class ByteArrayReader extends RandomAccessReader 38 38 { 39 39 @NotNull 40 40 private final byte[] _buffer; 41 private boolean _isMotorolaByteOrder = true;42 41 43 42 @SuppressWarnings({ "ConstantConditions" }) … … 47 46 if (buffer == null) 48 47 throw new NullPointerException(); 49 48 50 49 _buffer = buffer; 51 50 } … … 57 56 } 58 57 59 60 58 @Override 61 p ublic void setMotorolaByteOrder(boolean motorolaByteOrder)59 protected byte getByte(int index) throws IOException 62 60 { 63 _isMotorolaByteOrder = motorolaByteOrder;64 }65 66 @Override67 public boolean isMotorolaByteOrder()68 {69 return _isMotorolaByteOrder;70 }71 72 @Override73 public short getUInt8(int index) throws BufferBoundsException74 {75 checkBounds(index, 1);76 77 return (short) (_buffer[index] & 255);78 }79 80 @Override81 public byte getInt8(int index) throws BufferBoundsException82 {83 checkBounds(index, 1);84 85 61 return _buffer[index]; 86 62 } 87 63 88 64 @Override 89 p ublic int getUInt16(int index) throws BufferBoundsException65 protected void validateIndex(int index, int bytesRequested) throws IOException 90 66 { 91 checkBounds(index, 2); 92 93 if (_isMotorolaByteOrder) { 94 // Motorola - MSB first 95 return (_buffer[index ] << 8 & 0xFF00) | 96 (_buffer[index + 1] & 0xFF); 97 } else { 98 // Intel ordering - LSB first 99 return (_buffer[index + 1] << 8 & 0xFF00) | 100 (_buffer[index ] & 0xFF); 101 } 67 if (!isValidIndex(index, bytesRequested)) 68 throw new BufferBoundsException(index, bytesRequested, _buffer.length); 102 69 } 103 70 104 71 @Override 105 p ublic short getInt16(int index) throws BufferBoundsException72 protected boolean isValidIndex(int index, int bytesRequested) throws IOException 106 73 { 107 checkBounds(index, 2); 108 109 if (_isMotorolaByteOrder) { 110 // Motorola - MSB first 111 return (short) (((short)_buffer[index ] << 8 & (short)0xFF00) | 112 ((short)_buffer[index + 1] & (short)0xFF)); 113 } else { 114 // Intel ordering - LSB first 115 return (short) (((short)_buffer[index + 1] << 8 & (short)0xFF00) | 116 ((short)_buffer[index ] & (short)0xFF)); 117 } 74 return bytesRequested >= 0 75 && index >= 0 76 && (long)index + (long)bytesRequested - 1L < (long)_buffer.length; 118 77 } 119 78 120 79 @Override 121 public long getUInt32(int index) throws BufferBoundsException 80 @NotNull 81 public byte[] getBytes(int index, int count) throws IOException 122 82 { 123 checkBounds(index, 4); 124 125 if (_isMotorolaByteOrder) { 126 // Motorola - MSB first (big endian) 127 return (((long)_buffer[index ]) << 24 & 0xFF000000L) | 128 (((long)_buffer[index + 1]) << 16 & 0xFF0000L) | 129 (((long)_buffer[index + 2]) << 8 & 0xFF00L) | 130 (((long)_buffer[index + 3]) & 0xFFL); 131 } else { 132 // Intel ordering - LSB first (little endian) 133 return (((long)_buffer[index + 3]) << 24 & 0xFF000000L) | 134 (((long)_buffer[index + 2]) << 16 & 0xFF0000L) | 135 (((long)_buffer[index + 1]) << 8 & 0xFF00L) | 136 (((long)_buffer[index ]) & 0xFFL); 137 } 138 } 139 140 @Override 141 public int getInt32(int index) throws BufferBoundsException 142 { 143 checkBounds(index, 4); 144 145 if (_isMotorolaByteOrder) { 146 // Motorola - MSB first (big endian) 147 return (_buffer[index ] << 24 & 0xFF000000) | 148 (_buffer[index + 1] << 16 & 0xFF0000) | 149 (_buffer[index + 2] << 8 & 0xFF00) | 150 (_buffer[index + 3] & 0xFF); 151 } else { 152 // Intel ordering - LSB first (little endian) 153 return (_buffer[index + 3] << 24 & 0xFF000000) | 154 (_buffer[index + 2] << 16 & 0xFF0000) | 155 (_buffer[index + 1] << 8 & 0xFF00) | 156 (_buffer[index ] & 0xFF); 157 } 158 } 159 160 @Override 161 public long getInt64(int index) throws BufferBoundsException 162 { 163 checkBounds(index, 8); 164 165 if (_isMotorolaByteOrder) { 166 // Motorola - MSB first 167 return ((long)_buffer[index ] << 56 & 0xFF00000000000000L) | 168 ((long)_buffer[index + 1] << 48 & 0xFF000000000000L) | 169 ((long)_buffer[index + 2] << 40 & 0xFF0000000000L) | 170 ((long)_buffer[index + 3] << 32 & 0xFF00000000L) | 171 ((long)_buffer[index + 4] << 24 & 0xFF000000L) | 172 ((long)_buffer[index + 5] << 16 & 0xFF0000L) | 173 ((long)_buffer[index + 6] << 8 & 0xFF00L) | 174 ((long)_buffer[index + 7] & 0xFFL); 175 } else { 176 // Intel ordering - LSB first 177 return ((long)_buffer[index + 7] << 56 & 0xFF00000000000000L) | 178 ((long)_buffer[index + 6] << 48 & 0xFF000000000000L) | 179 ((long)_buffer[index + 5] << 40 & 0xFF0000000000L) | 180 ((long)_buffer[index + 4] << 32 & 0xFF00000000L) | 181 ((long)_buffer[index + 3] << 24 & 0xFF000000L) | 182 ((long)_buffer[index + 2] << 16 & 0xFF0000L) | 183 ((long)_buffer[index + 1] << 8 & 0xFF00L) | 184 ((long)_buffer[index ] & 0xFFL); 185 } 186 } 187 188 @Override 189 public float getS15Fixed16(int index) throws BufferBoundsException 190 { 191 checkBounds(index, 4); 192 193 if (_isMotorolaByteOrder) { 194 float res = (_buffer[index ] & 255) << 8 | 195 (_buffer[index + 1] & 255); 196 int d = (_buffer[index + 2] & 255) << 8 | 197 (_buffer[index + 3] & 255); 198 return (float)(res + d/65536.0); 199 } else { 200 // this particular branch is untested 201 float res = (_buffer[index + 3] & 255) << 8 | 202 (_buffer[index + 2] & 255); 203 int d = (_buffer[index + 1] & 255) << 8 | 204 (_buffer[index ] & 255); 205 return (float)(res + d/65536.0); 206 } 207 } 208 209 @Override 210 public float getFloat32(int index) throws BufferBoundsException 211 { 212 return Float.intBitsToFloat(getInt32(index)); 213 } 214 215 @Override 216 public double getDouble64(int index) throws BufferBoundsException 217 { 218 return Double.longBitsToDouble(getInt64(index)); 219 } 220 221 @Override 222 @NotNull 223 public byte[] getBytes(int index, int count) throws BufferBoundsException 224 { 225 checkBounds(index, count); 83 validateIndex(index, count); 226 84 227 85 byte[] bytes = new byte[count]; … … 229 87 return bytes; 230 88 } 231 232 @Override233 @NotNull234 public String getString(int index, int bytesRequested) throws BufferBoundsException235 {236 return new String(getBytes(index, bytesRequested));237 }238 239 @Override240 @NotNull241 public String getString(int index, int bytesRequested, String charset) throws BufferBoundsException242 {243 byte[] bytes = getBytes(index, bytesRequested);244 try {245 return new String(bytes, charset);246 } catch (UnsupportedEncodingException e) {247 return new String(bytes);248 }249 }250 251 @Override252 @NotNull253 public String getNullTerminatedString(int index, int maxLengthBytes) throws BufferBoundsException254 {255 // NOTE currently only really suited to single-byte character strings256 257 checkBounds(index, maxLengthBytes);258 259 // Check for null terminators260 int length = 0;261 while ((index + length) < _buffer.length && _buffer[index + length] != '\0' && length < maxLengthBytes)262 length++;263 264 byte[] bytes = getBytes(index, length);265 return new String(bytes);266 }267 268 private void checkBounds(final int index, final int bytesRequested) throws BufferBoundsException269 {270 if (bytesRequested < 0 || index < 0 || (long)index + (long)bytesRequested - 1L >= (long)_buffer.length)271 throw new BufferBoundsException(_buffer, index, bytesRequested);272 }273 89 }
Note:
See TracChangeset
for help on using the changeset viewer.