Changeset 13061 in josm for trunk/src/com/drew/lang/ByteArrayReader.java
- Timestamp:
- 2017-10-30T22:46:09+01:00 (8 years ago)
- File:
-
- 1 edited
-
trunk/src/com/drew/lang/ByteArrayReader.java (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/drew/lang/ByteArrayReader.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 24 26 import java.io.IOException; 25 26 import com.drew.lang.annotations.NotNull;27 27 28 28 /** … … 39 39 @NotNull 40 40 private final byte[] _buffer; 41 private final int _baseOffset; 41 42 43 @SuppressWarnings({ "ConstantConditions" }) 44 @com.drew.lang.annotations.SuppressWarnings(value = "EI_EXPOSE_REP2", justification = "Design intent") 42 45 public ByteArrayReader(@NotNull byte[] buffer) 46 { 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) 43 53 { 44 54 if (buffer == null) 45 55 throw new NullPointerException(); 56 if (baseOffset < 0) 57 throw new IllegalArgumentException("Must be zero or greater"); 46 58 47 59 _buffer = buffer; 60 _baseOffset = baseOffset; 61 } 62 63 @Override 64 public int toUnshiftedOffset(int localOffset) 65 { 66 return localOffset + _baseOffset; 48 67 } 49 68 … … 51 70 public long getLength() 52 71 { 53 return _buffer.length; 72 return _buffer.length - _baseOffset; 54 73 } 55 74 56 75 @Override 57 p rotectedbyte getByte(int index) throws IOException76 public byte getByte(int index) throws IOException 58 77 { 59 return _buffer[index]; 78 validateIndex(index, 1); 79 return _buffer[index + _baseOffset]; 60 80 } 61 81 … … 64 84 { 65 85 if (!isValidIndex(index, bytesRequested)) 66 throw new BufferBoundsException( index, bytesRequested, _buffer.length);86 throw new BufferBoundsException(toUnshiftedOffset(index), bytesRequested, _buffer.length); 67 87 } 68 88 … … 72 92 return bytesRequested >= 0 73 93 && index >= 0 74 && (long)index + (long)bytesRequested - 1L < _buffer.length;94 && (long)index + (long)bytesRequested - 1L < getLength(); 75 95 } 76 96 … … 82 102 83 103 byte[] bytes = new byte[count]; 84 System.arraycopy(_buffer, index, bytes, 0, count); 104 System.arraycopy(_buffer, index + _baseOffset, bytes, 0, count); 85 105 return bytes; 86 106 }
Note:
See TracChangeset
for help on using the changeset viewer.
