1 /*
2  * Copyright (C) 2011 The Android Open Source Project
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.android.dx.io.instructions;
18 
19 import java.io.EOFException;
20 
21 /**
22  * Implementation of {@code CodeInput} that reads from a {@code short[]}.
23  */
24 public final class ShortArrayCodeInput extends BaseCodeCursor
25         implements CodeInput {
26     /** source array to read from */
27     private final short[] array;
28 
29     /**
30      * Constructs an instance.
31      */
ShortArrayCodeInput(short[] array)32     public ShortArrayCodeInput(short[] array) {
33         if (array == null) {
34             throw new NullPointerException("array == null");
35         }
36 
37         this.array = array;
38     }
39 
40     /** {@inheritDoc} */
41     @Override
hasMore()42     public boolean hasMore() {
43         return cursor() < array.length;
44     }
45 
46     /** {@inheritDoc} */
47     @Override
read()48     public int read() throws EOFException {
49         try {
50             int value = array[cursor()];
51             advance(1);
52             return value & 0xffff;
53         } catch (ArrayIndexOutOfBoundsException ex) {
54             throw new EOFException();
55         }
56     }
57 
58     /** {@inheritDoc} */
59     @Override
readInt()60     public int readInt() throws EOFException {
61         int short0 = read();
62         int short1 = read();
63 
64         return short0 | (short1 << 16);
65     }
66 
67     /** {@inheritDoc} */
68     @Override
readLong()69     public long readLong() throws EOFException {
70         long short0 = read();
71         long short1 = read();
72         long short2 = read();
73         long short3 = read();
74 
75         return short0 | (short1 << 16) | (short2 << 32) | (short3 << 48);
76     }
77 }
78