View Javadoc

1   /*
2    * Copyright  2000-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License"); 
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License. 
15   *
16   */
17  package org.apache.bcel.util;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.DataInputStream;
21  
22  /***
23   * Utility class that implements a sequence of bytes which can be read
24   * via the `readByte()' method. This is used to implement a wrapper for the 
25   * Java byte code stream to gain some more readability.
26   *
27   * @version $Id: ByteSequence.java 386056 2006-03-15 11:31:56Z tcurdt $
28   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
29   */
30  public final class ByteSequence extends DataInputStream {
31  
32      private ByteArrayStream byte_stream;
33  
34  
35      public ByteSequence(byte[] bytes) {
36          super(new ByteArrayStream(bytes));
37          byte_stream = (ByteArrayStream) in;
38      }
39  
40  
41      public final int getIndex() {
42          return byte_stream.getPosition();
43      }
44  
45  
46      final void unreadByte() {
47          byte_stream.unreadByte();
48      }
49  
50      private static final class ByteArrayStream extends ByteArrayInputStream {
51  
52          ByteArrayStream(byte[] bytes) {
53              super(bytes);
54          }
55  
56  
57          final int getPosition() {
58              return pos;
59          } // is protected in ByteArrayInputStream
60  
61  
62          final void unreadByte() {
63              if (pos > 0) {
64                  pos--;
65              }
66          }
67      }
68  }