Changeset 13061 in josm for trunk/src/com/drew/lang/RandomAccessReader.java
- Timestamp:
- 2017-10-30T22:46:09+01:00 (8 years ago)
- File:
-
- 1 edited
-
trunk/src/com/drew/lang/RandomAccessReader.java (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/drew/lang/RandomAccessReader.java
r10862 r13061 1 1 /* 2 * Copyright 2002-201 6Drew Noakes2 * Copyright 2002-2017 Drew Noakes 3 3 * 4 4 * Licensed under the Apache License, Version 2.0 (the "License"); … … 22 22 package com.drew.lang; 23 23 24 import com.drew.lang.annotations.NotNull;25 26 24 import java.io.IOException; 27 25 import java.io.UnsupportedEncodingException; 26 import java.nio.charset.Charset; 27 28 import com.drew.lang.annotations.NotNull; 29 import com.drew.lang.annotations.Nullable; 30 import com.drew.metadata.StringValue; 28 31 29 32 /** … … 36 39 * <ul> 37 40 * <li>{@link ByteArrayReader}</li> 38 * <li>{@link RandomAccessStreamReader}</li>39 41 * </ul> 40 42 * … … 45 47 private boolean _isMotorolaByteOrder = true; 46 48 49 public abstract int toUnshiftedOffset(int localOffset); 50 47 51 /** 48 52 * Gets the byte value at the specified byte <code>index</code>. … … 57 61 * @throws IOException if the byte is unable to be read 58 62 */ 59 p rotectedabstract byte getByte(int index) throws IOException;63 public abstract byte getByte(int index) throws IOException; 60 64 61 65 /** … … 89 93 * Returns the length of the data source in bytes. 90 94 * <p> 91 * This is a simple operation for implementations (such as {@link RandomAccessFileReader} and95 * This is a simple operation for implementations (such as 92 96 * {@link ByteArrayReader}) that have the entire data source available. 93 97 * <p> 94 * Users of this method must be aware that sequentially accessed implementations such as95 * {@link RandomAccessStreamReader}will have to read and buffer the entire data source in98 * Users of this method must be aware that sequentially accessed implementations 99 * will have to read and buffer the entire data source in 96 100 * order to determine the length. 97 101 * … … 207 211 if (_isMotorolaByteOrder) { 208 212 // Motorola - MSB first 209 return (short) (( (short)getByte(index ) << 8 & (short)0xFF00) |210 ( (short)getByte(index + 1) & (short)0xFF));213 return (short) ((getByte(index ) << 8 & (short)0xFF00) | 214 (getByte(index + 1) & (short)0xFF)); 211 215 } else { 212 216 // Intel ordering - LSB first 213 return (short) (( (short)getByte(index + 1) << 8 & (short)0xFF00) |214 ( (short)getByte(index ) & (short)0xFF));217 return (short) ((getByte(index + 1) << 8 & (short)0xFF00) | 218 (getByte(index ) & (short)0xFF)); 215 219 } 216 220 } … … 229 233 if (_isMotorolaByteOrder) { 230 234 // Motorola - MSB first (big endian) 231 return (( (int)getByte(index )) << 16 & 0xFF0000) |232 (( (int)getByte(index + 1)) << 8 & 0xFF00) |233 (( (int)getByte(index + 2)) & 0xFF);235 return ((getByte(index )) << 16 & 0xFF0000) | 236 ((getByte(index + 1)) << 8 & 0xFF00) | 237 ((getByte(index + 2)) & 0xFF); 234 238 } else { 235 239 // Intel ordering - LSB first (little endian) 236 return (( (int)getByte(index + 2)) << 16 & 0xFF0000) |237 (( (int)getByte(index + 1)) << 8 & 0xFF00) |238 (( (int)getByte(index )) & 0xFF);240 return ((getByte(index + 2)) << 16 & 0xFF0000) | 241 ((getByte(index + 1)) << 8 & 0xFF00) | 242 ((getByte(index )) & 0xFF); 239 243 } 240 244 } … … 256 260 (((long)getByte(index + 1)) << 16 & 0xFF0000L) | 257 261 (((long)getByte(index + 2)) << 8 & 0xFF00L) | 258 (( (long)getByte(index + 3)) & 0xFFL);262 ((getByte(index + 3)) & 0xFFL); 259 263 } else { 260 264 // Intel ordering - LSB first (little endian) … … 262 266 (((long)getByte(index + 2)) << 16 & 0xFF0000L) | 263 267 (((long)getByte(index + 1)) << 8 & 0xFF00L) | 264 (( (long)getByte(index )) & 0xFFL);268 ((getByte(index )) & 0xFFL); 265 269 } 266 270 } … … 312 316 ((long)getByte(index + 5) << 16 & 0xFF0000L) | 313 317 ((long)getByte(index + 6) << 8 & 0xFF00L) | 314 ( (long)getByte(index + 7) & 0xFFL);318 (getByte(index + 7) & 0xFFL); 315 319 } else { 316 320 // Intel ordering - LSB first … … 322 326 ((long)getByte(index + 2) << 16 & 0xFF0000L) | 323 327 ((long)getByte(index + 1) << 8 & 0xFF00L) | 324 ( (long)getByte(index ) & 0xFFL);328 (getByte(index ) & 0xFFL); 325 329 } 326 330 } … … 365 369 366 370 @NotNull 367 public String getString(int index, int bytesRequested) throws IOException 368 { 369 return new String(getBytes(index, bytesRequested)); 370 } 371 372 @NotNull 373 public String getString(int index, int bytesRequested, String charset) throws IOException 371 public StringValue getStringValue(int index, int bytesRequested, @Nullable Charset charset) throws IOException 372 { 373 return new StringValue(getBytes(index, bytesRequested), charset); 374 } 375 376 @NotNull 377 public String getString(int index, int bytesRequested, @NotNull Charset charset) throws IOException 378 { 379 return new String(getBytes(index, bytesRequested), charset.name()); 380 } 381 382 @NotNull 383 public String getString(int index, int bytesRequested, @NotNull String charset) throws IOException 374 384 { 375 385 byte[] bytes = getBytes(index, bytesRequested); … … 392 402 */ 393 403 @NotNull 394 public String getNullTerminatedString(int index, int maxLengthBytes) throws IOException 395 { 396 // NOTE currently only really suited to single-byte character strings 397 398 byte[] bytes = getBytes(index, maxLengthBytes); 404 public String getNullTerminatedString(int index, int maxLengthBytes, @NotNull Charset charset) throws IOException 405 { 406 return new String(getNullTerminatedBytes(index, maxLengthBytes), charset.name()); 407 } 408 409 @NotNull 410 public StringValue getNullTerminatedStringValue(int index, int maxLengthBytes, @Nullable Charset charset) throws IOException 411 { 412 byte[] bytes = getNullTerminatedBytes(index, maxLengthBytes); 413 414 return new StringValue(bytes, charset); 415 } 416 417 /** 418 * Returns the sequence of bytes punctuated by a <code>\0</code> value. 419 * 420 * @param index The index within the buffer at which to start reading the string. 421 * @param maxLengthBytes The maximum number of bytes to read. If a <code>\0</code> byte is not reached within this limit, 422 * the returned array will be <code>maxLengthBytes</code> long. 423 * @return The read byte array, excluding the null terminator. 424 * @throws IOException The buffer does not contain enough bytes to satisfy this request. 425 */ 426 @NotNull 427 public byte[] getNullTerminatedBytes(int index, int maxLengthBytes) throws IOException 428 { 429 byte[] buffer = getBytes(index, maxLengthBytes); 399 430 400 431 // Count the number of non-null bytes 401 432 int length = 0; 402 while (length < b ytes.length && bytes[length] !='\0')433 while (length < buffer.length && buffer[length] != 0) 403 434 length++; 404 435 405 return new String(bytes, 0, length); 436 if (length == maxLengthBytes) 437 return buffer; 438 439 byte[] bytes = new byte[length]; 440 if (length > 0) 441 System.arraycopy(buffer, 0, bytes, 0, length); 442 return bytes; 406 443 } 407 444 }
Note:
See TracChangeset
for help on using the changeset viewer.
