1 /*
2  * Copyright (C) 2010 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 libcore.io;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 
21 /**
22  * Iterates over big- or little-endian bytes. See {@link MemoryMappedFile#bigEndianIterator} and
23  * {@link MemoryMappedFile#littleEndianIterator}.
24  *
25  * @hide
26  */
27 public abstract class BufferIterator {
28     /**
29      * Seeks to the absolute position {@code offset}, measured in bytes from the start of the
30      * buffer.
31      */
32     @UnsupportedAppUsage
seek(int offset)33     public abstract void seek(int offset);
34 
35     /**
36      * Skips forwards or backwards {@code byteCount} bytes from the current position.
37      */
38     @UnsupportedAppUsage
skip(int byteCount)39     public abstract void skip(int byteCount);
40 
41     /**
42      * Returns the current position of the iterator within the buffer.
43      */
pos()44     public abstract int pos();
45 
46     /**
47      * Copies {@code byteCount} bytes from the current position into {@code bytes}, starting at
48      * {@code arrayOffset}, and advances the current position {@code byteCount} bytes.
49      *
50      * @throws IndexOutOfBoundsException if the read / write would be outside of the buffer / array
51      */
52     @UnsupportedAppUsage
readByteArray(byte[] bytes, int arrayOffset, int byteCount)53     public abstract void readByteArray(byte[] bytes, int arrayOffset, int byteCount);
54 
55     /**
56      * Returns the byte at the current position, and advances the current position one byte.
57      *
58      * @throws IndexOutOfBoundsException if the read would be outside of the buffer
59      */
60     @UnsupportedAppUsage
readByte()61     public abstract byte readByte();
62 
63     /**
64      * Returns the 32-bit int at the current position, and advances the current position four bytes.
65      *
66      * @throws IndexOutOfBoundsException if the read would be outside of the buffer
67      */
68     @UnsupportedAppUsage
readInt()69     public abstract int readInt();
70 
71     /**
72      * Copies {@code intCount} 32-bit ints from the current position into {@code ints}, starting at
73      * {@code arrayOffset}, and advances the current position {@code 4 * intCount} bytes.
74      *
75      * @throws IndexOutOfBoundsException if the read / write would be outside of the buffer / array
76      */
77     @UnsupportedAppUsage
readIntArray(int[] ints, int arrayOffset, int intCount)78     public abstract void readIntArray(int[] ints, int arrayOffset, int intCount);
79 
80     /**
81      * Copies {@code longCount} 64-bit ints from the current position into {@code longs}, starting
82      * at {@code arrayOffset}, and advances the current position {@code 8 * longCount} bytes.
83      *
84      * @throws IndexOutOfBoundsException if the read / write would be outside of the buffer / array
85      */
readLongArray(long[] longs, int arrayOffset, int longCount)86     public abstract void readLongArray(long[] longs, int arrayOffset, int longCount);
87 
88     /**
89      * Returns the 16-bit short at the current position, and advances the current position two bytes.
90      *
91      * @throws IndexOutOfBoundsException if the read would be outside of the buffer
92      */
readShort()93     public abstract short readShort();
94 }
95