Home | History | Annotate | Download | only in io
      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 libcore.io.Memory;
     20 
     21 /**
     22  * Iterates over big- or little-endian bytes on the native heap.
     23  * See {@link MemoryMappedFile#bigEndianIterator} and {@link MemoryMappedFile#littleEndianIterator}.
     24  *
     25  * @hide don't make this public without adding bounds checking.
     26  */
     27 public final class NioBufferIterator extends BufferIterator {
     28     private final long address;
     29     private final int size;
     30     private final boolean swap;
     31 
     32     private int position;
     33 
     34     NioBufferIterator(long address, int size, boolean swap) {
     35         this.address = address;
     36         this.size = size;
     37         this.swap = swap;
     38     }
     39 
     40     public void seek(int offset) {
     41         position = offset;
     42     }
     43 
     44     public void skip(int byteCount) {
     45         position += byteCount;
     46     }
     47 
     48     public void readByteArray(byte[] dst, int dstOffset, int byteCount) {
     49         Memory.peekByteArray(address + position, dst, dstOffset, byteCount);
     50         position += byteCount;
     51     }
     52 
     53     public byte readByte() {
     54         byte result = Memory.peekByte(address + position);
     55         ++position;
     56         return result;
     57     }
     58 
     59     public int readInt() {
     60         int result = Memory.peekInt(address + position, swap);
     61         position += SizeOf.INT;
     62         return result;
     63     }
     64 
     65     public void readIntArray(int[] dst, int dstOffset, int intCount) {
     66         Memory.peekIntArray(address + position, dst, dstOffset, intCount, swap);
     67         position += SizeOf.INT * intCount;
     68     }
     69 
     70     public short readShort() {
     71         short result = Memory.peekShort(address + position, swap);
     72         position += SizeOf.SHORT;
     73         return result;
     74     }
     75 }
     76