12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.util; 23 24 import java.io.ByteArrayInputStream; 25 import java.io.DataInputStream; 26 27 /** 28 * Utility class that implements a sequence of bytes which can be read 29 * via the `readByte()' method. This is used to implement a wrapper for the 30 * Java byte code stream to gain some more readability. 31 * 32 * @version $Id$ 33 */ 34 public final class ByteSequence extends DataInputStream { 35 36 private final ByteArrayStream byteStream; 37 38 39 public ByteSequence(final byte[] bytes) { 40 super(new ByteArrayStream(bytes)); 41 byteStream = (ByteArrayStream) in; 42 } 43 44 45 public final int getIndex() { 46 return byteStream.getPosition(); 47 } 48 49 50 final void unreadByte() { 51 byteStream.unreadByte(); 52 } 53 54 private static final class ByteArrayStream extends ByteArrayInputStream { 55 56 ByteArrayStream(final byte[] bytes) { 57 super(bytes); 58 } 59 60 final int getPosition() { 61 // pos is protected in ByteArrayInputStream 62 return pos; 63 } 64 65 final void unreadByte() { 66 if (pos > 0) { 67 pos--; 68 } 69 } 70 } 71 } | 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.util; 23 24 import java.io.ByteArrayInputStream; 25 import java.io.DataInputStream; 26 27 /** 28 * Utility class that implements a sequence of bytes which can be read 29 * via the `readByte()' method. This is used to implement a wrapper for the 30 * Java byte code stream to gain some more readability. 31 * 32 */ 33 public final class ByteSequence extends DataInputStream { 34 35 private final ByteArrayStream byteStream; 36 37 38 public ByteSequence(final byte[] bytes) { 39 super(new ByteArrayStream(bytes)); 40 byteStream = (ByteArrayStream) in; 41 } 42 43 44 public int getIndex() { 45 return byteStream.getPosition(); 46 } 47 48 49 void unreadByte() { 50 byteStream.unreadByte(); 51 } 52 53 private static final class ByteArrayStream extends ByteArrayInputStream { 54 55 ByteArrayStream(final byte[] bytes) { 56 super(bytes); 57 } 58 59 int getPosition() { 60 // pos is protected in ByteArrayInputStream 61 return pos; 62 } 63 64 void unreadByte() { 65 if (pos > 0) { 66 pos--; 67 } 68 } 69 } 70 } |