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
17package com.android.dx.io.instructions;
18
19import java.io.EOFException;
20
21/**
22 * Implementation of {@code CodeInput} that reads from a {@code short[]}.
23 */
24public 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     */
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    public boolean hasMore() {
42        return cursor() < array.length;
43    }
44
45    /** @inheritDoc */
46    public int read() throws EOFException {
47        try {
48            int value = array[cursor()];
49            advance(1);
50            return value & 0xffff;
51        } catch (ArrayIndexOutOfBoundsException ex) {
52            throw new EOFException();
53        }
54    }
55
56    /** @inheritDoc */
57    public int readInt() throws EOFException {
58        int short0 = read();
59        int short1 = read();
60
61        return short0 | (short1 << 16);
62    }
63
64    /** @inheritDoc */
65    public long readLong() throws EOFException {
66        long short0 = read();
67        long short1 = read();
68        long short2 = read();
69        long short3 = read();
70
71        return short0 | (short1 << 16) | (short2 << 32) | (short3 << 48);
72    }
73}
74