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
19/**
20 * Implementation of {@code CodeOutput} that writes to a {@code short[]}.
21 */
22public final class ShortArrayCodeOutput extends BaseCodeCursor
23        implements CodeOutput {
24    /** array to write to */
25    private final short[] array;
26
27    /**
28     * Constructs an instance.
29     *
30     * @param maxSize the maximum number of code units that will be written
31     */
32    public ShortArrayCodeOutput(int maxSize) {
33        if (maxSize < 0) {
34            throw new IllegalArgumentException("maxSize < 0");
35        }
36
37        this.array = new short[maxSize];
38    }
39
40    /**
41     * Gets the array. The returned array contains exactly the data
42     * written (e.g. no leftover space at the end).
43     */
44    public short[] getArray() {
45        int cursor = cursor();
46
47        if (cursor == array.length) {
48            return array;
49        }
50
51        short[] result = new short[cursor];
52        System.arraycopy(array, 0, result, 0, cursor);
53        return result;
54    }
55
56    /** @inheritDoc */
57    public void write(short codeUnit) {
58        array[cursor()] = codeUnit;
59        advance(1);
60    }
61
62    /** @inheritDoc */
63    public void write(short u0, short u1) {
64        write(u0);
65        write(u1);
66    }
67
68    /** @inheritDoc */
69    public void write(short u0, short u1, short u2) {
70        write(u0);
71        write(u1);
72        write(u2);
73    }
74
75    /** @inheritDoc */
76    public void write(short u0, short u1, short u2, short u3) {
77        write(u0);
78        write(u1);
79        write(u2);
80        write(u3);
81    }
82
83    /** @inheritDoc */
84    public void write(short u0, short u1, short u2, short u3, short u4) {
85        write(u0);
86        write(u1);
87        write(u2);
88        write(u3);
89        write(u4);
90    }
91
92    /** @inheritDoc */
93    public void writeInt(int value) {
94        write((short) value);
95        write((short) (value >> 16));
96    }
97
98    /** @inheritDoc */
99    public void writeLong(long value) {
100        write((short) value);
101        write((short) (value >> 16));
102        write((short) (value >> 32));
103        write((short) (value >> 48));
104    }
105
106    /** @inheritDoc */
107    public void write(byte[] data) {
108        int value = 0;
109        boolean even = true;
110        for (byte b : data) {
111            if (even) {
112                value = b & 0xff;
113                even = false;
114            } else {
115                value |= b << 8;
116                write((short) value);
117                even = true;
118            }
119        }
120
121        if (!even) {
122            write((short) value);
123        }
124    }
125
126    /** @inheritDoc */
127    public void write(short[] data) {
128        for (short unit : data) {
129            write(unit);
130        }
131    }
132
133    /** @inheritDoc */
134    public void write(int[] data) {
135        for (int i : data) {
136            writeInt(i);
137        }
138    }
139
140    /** @inheritDoc */
141    public void write(long[] data) {
142        for (long l : data) {
143            writeLong(l);
144        }
145    }
146}
147