Changeset 13061 in josm for trunk/src/com/drew/lang/SequentialReader.java
- Timestamp:
- 2017-10-30T22:46:09+01:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/drew/lang/SequentialReader.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"); … … 23 23 24 24 import com.drew.lang.annotations.NotNull; 25 import com.drew.lang.annotations.Nullable; 26 import com.drew.metadata.StringValue; 25 27 26 28 import java.io.EOFException; 27 29 import java.io.IOException; 28 30 import java.io.UnsupportedEncodingException; 31 import java.nio.charset.Charset; 29 32 30 33 /** 31 34 * @author Drew Noakes https://drewnoakes.com 32 35 */ 36 @SuppressWarnings("WeakerAccess") 33 37 public abstract class SequentialReader 34 38 { … … 37 41 private boolean _isMotorolaByteOrder = true; 38 42 43 public abstract long getPosition() throws IOException; 44 39 45 /** 40 46 * Gets the next byte in the sequence. … … 42 48 * @return The read byte value 43 49 */ 44 p rotectedabstract byte getByte() throws IOException;50 public abstract byte getByte() throws IOException; 45 51 46 52 /** … … 52 58 @NotNull 53 59 public abstract byte[] getBytes(int count) throws IOException; 60 61 /** 62 * Retrieves bytes, writing them into a caller-provided buffer. 63 * @param buffer The array to write bytes to. 64 * @param offset The starting position within buffer to write to. 65 * @param count The number of bytes to be written. 66 */ 67 public abstract void getBytes(@NotNull byte[] buffer, int offset, int count) throws IOException; 54 68 55 69 /** … … 70 84 */ 71 85 public abstract boolean trySkip(long n) throws IOException; 86 87 /** 88 * Returns an estimate of the number of bytes that can be read (or skipped 89 * over) from this {@link SequentialReader} without blocking by the next 90 * invocation of a method for this input stream. A single read or skip of 91 * this many bytes will not block, but may read or skip fewer bytes. 92 * <p> 93 * Note that while some implementations of {@link SequentialReader} like 94 * {@link SequentialByteArrayReader} will return the total remaining number 95 * of bytes in the stream, others will not. It is never correct to use the 96 * return value of this method to allocate a buffer intended to hold all 97 * data in this stream. 98 * 99 * @return an estimate of the number of bytes that can be read (or skipped 100 * over) from this {@link SequentialReader} without blocking or 101 * {@code 0} when it reaches the end of the input stream. 102 */ 103 public abstract int available(); 72 104 73 105 /** … … 284 316 } 285 317 318 @NotNull 319 public String getString(int bytesRequested, @NotNull Charset charset) throws IOException 320 { 321 byte[] bytes = getBytes(bytesRequested); 322 return new String(bytes, charset); 323 } 324 325 @NotNull 326 public StringValue getStringValue(int bytesRequested, @Nullable Charset charset) throws IOException 327 { 328 return new StringValue(getBytes(bytesRequested), charset); 329 } 330 286 331 /** 287 332 * Creates a String from the stream, ending where <code>byte=='\0'</code> or where <code>length==maxLength</code>. … … 293 338 */ 294 339 @NotNull 295 public String getNullTerminatedString(int maxLengthBytes) throws IOException 296 { 297 // NOTE currently only really suited to single-byte character strings 298 299 byte[] bytes = new byte[maxLengthBytes]; 340 public String getNullTerminatedString(int maxLengthBytes, Charset charset) throws IOException 341 { 342 return getNullTerminatedStringValue(maxLengthBytes, charset).toString(); 343 } 344 345 /** 346 * Creates a String from the stream, ending where <code>byte=='\0'</code> or where <code>length==maxLength</code>. 347 * 348 * @param maxLengthBytes The maximum number of bytes to read. If a <code>\0</code> byte is not reached within this limit, 349 * reading will stop and the string will be truncated to this length. 350 * @param charset The <code>Charset</code> to register with the returned <code>StringValue</code>, or <code>null</code> if the encoding 351 * is unknown 352 * @return The read string. 353 * @throws IOException The buffer does not contain enough bytes to satisfy this request. 354 */ 355 @NotNull 356 public StringValue getNullTerminatedStringValue(int maxLengthBytes, Charset charset) throws IOException 357 { 358 byte[] bytes = getNullTerminatedBytes(maxLengthBytes); 359 360 return new StringValue(bytes, charset); 361 } 362 363 /** 364 * Returns the sequence of bytes punctuated by a <code>\0</code> value. 365 * 366 * @param maxLengthBytes The maximum number of bytes to read. If a <code>\0</code> byte is not reached within this limit, 367 * the returned array will be <code>maxLengthBytes</code> long. 368 * @return The read byte array, excluding the null terminator. 369 * @throws IOException The buffer does not contain enough bytes to satisfy this request. 370 */ 371 @NotNull 372 public byte[] getNullTerminatedBytes(int maxLengthBytes) throws IOException 373 { 374 byte[] buffer = new byte[maxLengthBytes]; 300 375 301 376 // Count the number of non-null bytes 302 377 int length = 0; 303 while (length < b ytes.length && (bytes[length] = getByte()) != '\0')378 while (length < buffer.length && (buffer[length] = getByte()) != 0) 304 379 length++; 305 380 306 return new String(bytes, 0, length); 381 if (length == maxLengthBytes) 382 return buffer; 383 384 byte[] bytes = new byte[length]; 385 if (length > 0) 386 System.arraycopy(buffer, 0, bytes, 0, length); 387 return bytes; 307 388 } 308 389 }
Note:
See TracChangeset
for help on using the changeset viewer.