Home | History | Annotate | Download | only in bytecode
      1 /*
      2  * Copyright 2016 Google Inc. All Rights Reserved.
      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 com.google.turbine.bytecode;
     18 
     19 import static com.google.common.base.Verify.verify;
     20 
     21 import com.google.common.io.ByteArrayDataInput;
     22 import com.google.common.io.ByteStreams;
     23 import java.io.ByteArrayInputStream;
     24 
     25 /** A {@link ByteArrayDataInput} wrapper that tracks the current byte array index. */
     26 public class ByteReader {
     27 
     28   private final byte[] bytes;
     29   private final IndexedByteArrayInputStream indexed;
     30   private final ByteArrayDataInput input;
     31 
     32   public ByteReader(byte[] bytes, int pos) {
     33     this.bytes = bytes;
     34     this.indexed = new IndexedByteArrayInputStream(bytes, pos, bytes.length);
     35     this.input = ByteStreams.newDataInput(indexed);
     36   }
     37 
     38   public ByteArrayDataInput seek(int i) {
     39     return ByteStreams.newDataInput(bytes, i);
     40   }
     41 
     42   /** The position in the input buffer. */
     43   public int pos() {
     44     return indexed.pos();
     45   }
     46 
     47   /** Reads an unsigned 8-bit integer. */
     48   public int u1() {
     49     return input.readUnsignedByte();
     50   }
     51 
     52   /** Reads an unsigned 16-bit integer in big-endian byte order. */
     53   public int u2() {
     54     return input.readUnsignedShort();
     55   }
     56 
     57   /** Reads an unsigned 32-bit integer in big-endian byte order. */
     58   public int u4() {
     59     return input.readInt();
     60   }
     61 
     62   /** Skips n bytes of input. */
     63   public void skip(int n) {
     64     int skipped = input.skipBytes(n);
     65     // this is only used with {@link ByteArrayInputStream}.
     66     verify(skipped == n, "wanted %s, read %s", n, skipped);
     67   }
     68 
     69   /** {@link #pos} is protected in {@link java.io.ByteArrayInputStream}. */
     70   static class IndexedByteArrayInputStream extends ByteArrayInputStream {
     71 
     72     IndexedByteArrayInputStream(byte[] buf, int offset, int length) {
     73       super(buf, offset, length);
     74     }
     75 
     76     int pos() {
     77       return pos;
     78     }
     79   }
     80 }
     81