BaseCodeCursor.java revision 579d7739c53a2707ad711a2d2cae46d7d782f061
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 * Base implementation of {@link CodeCursor}.
23 */
24public abstract class BaseCodeCursor implements CodeCursor {
25    /** base address map */
26    private final AddressMap baseAddressMap;
27
28    /** next index within {@link #array} to read from or write to */
29    private int cursor;
30
31    /**
32     * Constructs an instance.
33     */
34    public BaseCodeCursor() {
35        this.baseAddressMap = new AddressMap();
36        this.cursor = 0;
37    }
38
39    /** @inheritDoc */
40    public final int cursor() {
41        return cursor;
42    }
43
44    /** @inheritDoc */
45    public final int baseAddressForCursor() {
46        int mapped = baseAddressMap.get(cursor);
47        return (mapped >= 0) ? mapped : cursor;
48    }
49
50    /** @inheritDoc */
51    public final void setBaseAddress(int targetAddress, int baseAddress) {
52        baseAddressMap.put(targetAddress, baseAddress);
53    }
54
55    /**
56     * Advance the cursor by the indicated amount.
57     */
58    protected final void advance(int amount) {
59        cursor += amount;
60    }
61}
62