Input.java revision 00fc68adf2e39aeb9fed35293f2576bbe729ec4b
1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2010 Ben Gruver (JesusFreke)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29package org.jf.dexlib.Util;
30
31/**
32 * Interface for a source for binary input. This is similar to
33 * <code>java.util.DataInput</code>, but no <code>IOExceptions</code>
34 * are declared, and multibyte input is defined to be little-endian.
35 */
36public interface Input {
37    /**
38     * Gets the current cursor position. This is the same as the number of
39     * bytes read from this instance.
40     *
41     * @return &gt;= 0; the cursor position
42     */
43    public int getCursor();
44
45    /**
46     * Sets the current cursor position.
47     *
48     * @return &gt;= 0; the cursor position
49     */
50    public void setCursor(int cursor);
51
52    /**
53     * Asserts that the cursor is the given value.
54     *
55     * @param expectedCursor the expected cursor value
56     * @throws RuntimeException thrown if <code>getCursor() !=
57     * expectedCursor</code>
58     */
59    public void assertCursor(int expectedCursor);
60
61    /**
62     * Reads a <code>byte</code> from this instance.
63     *
64     * @return the byte value that was read
65     */
66    public byte readByte();
67
68    /**
69     * Reads a <code>short</code> from this instance.
70     *
71     * @return the short value that was read, as an int
72     */
73    public int readShort();
74
75    /**
76     * Reads an <code>int</code> from this instance.
77     *
78     * @return the unsigned int value that was read
79     */
80    public int readInt();
81
82    /**
83     * Reads a <code>long</code> from this instance.
84     *
85     * @return the long value that was read
86     */
87    public long readLong();
88
89
90    /**
91     * Reads a DWARFv3-style signed LEB128 integer. For details,
92     * see the "Dalvik Executable Format" document or DWARF v3 section
93     * 7.6.
94     *
95     * @return the integer value that was read
96     */
97    public int readSignedLeb128();
98
99    /**
100     * Reads a DWARFv3-style unsigned LEB128 integer. For details,
101     * see the "Dalvik Executable Format" document or DWARF v3 section
102     * 7.6.
103     *
104     * @return the integer value that was read
105     */
106    public int readUnsignedLeb128();
107
108
109    /**
110     * Reads a unsigned value as a DWARFv3-style LEB128 integer. It specifically
111     * checks for the case when the value was incorrectly formatted as a signed
112     * LEB128, and returns the appropriate unsigned value, but negated
113     * @return If the value was formatted as a ULEB128, it returns the actual unsigned
114     * value. Otherwise, if the value was formatted as a signed LEB128, it negates the
115     * "correct" unsigned value and returns that
116     */
117    public int readUnsignedOrSignedLeb128();
118
119    /**
120     * reads a <code>byte[]</code> from this instance.
121     *
122     * @param bytes non-null; the buffer to read the data into
123     * @param offset &gt;= 0; offset into <code>bytes</code> for the first
124     * byte to write
125     * @param length &gt;= 0; number of bytes to read
126     */
127    public void read(byte[] bytes, int offset, int length);
128
129    /**
130     * reads a <code>byte[]</code> from this instance. This is just
131     * a convenient shorthand for <code>read(bytes, 0, bytes.length)</code>.
132     *
133     * @param bytes non-null; the buffer to read the data into
134     */
135    public void read(byte[] bytes);
136
137
138    /**
139     * reads a <code>byte[]</code> from this instance
140     *
141     * @param length &gt;= 0; number of bytes to read
142     * @return a byte array containing <code>length</code> bytes
143     */
144    public byte[] readBytes(int length);
145
146    /**
147     * reads a <code>byte[]</code> from this instance, from the current cursor up to but not including
148     * the next null (0) byte. The terminating null byte is read and discarded, so that after the read,
149     * the cursor is positioned at the byte immediately after the terminating null
150     */
151    public byte[] readNullTerminatedBytes();
152
153    /**
154     * Skips the given number of bytes.
155     *
156     * @param count &gt;= 0; the number of bytes to skip
157     */
158    public void skipBytes(int count);
159
160    /**
161     * Skip extra bytes if necessary to force alignment of the output
162     * cursor as given.
163     *
164     * @param alignment &gt; 0; the alignment; must be a power of two
165     */
166    public void alignTo(int alignment);
167}
168