1/* 2 * Copyright (C) 2014 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.layoutlib.bridge.libcore.io; 18 19import java.nio.ByteBuffer; 20 21import libcore.io.BufferIterator; 22 23/** 24 * Provides an implementation of {@link BufferIterator} over a {@link ByteBuffer}. 25 */ 26public class BridgeBufferIterator extends BufferIterator { 27 28 private final long mSize; 29 private final ByteBuffer mByteBuffer; 30 31 public BridgeBufferIterator(long size, ByteBuffer buffer) { 32 mSize = size; 33 mByteBuffer = buffer; 34 } 35 36 @Override 37 public void seek(int offset) { 38 assert offset <= mSize; 39 mByteBuffer.position(offset); 40 } 41 42 @Override 43 public int pos() { 44 return mByteBuffer.position(); 45 } 46 47 @Override 48 public void skip(int byteCount) { 49 int newPosition = mByteBuffer.position() + byteCount; 50 assert newPosition <= mSize; 51 mByteBuffer.position(newPosition); 52 } 53 54 @Override 55 public void readByteArray(byte[] dst, int dstOffset, int byteCount) { 56 assert dst.length >= dstOffset + byteCount; 57 mByteBuffer.get(dst, dstOffset, byteCount); 58 } 59 60 @Override 61 public byte readByte() { 62 return mByteBuffer.get(); 63 } 64 65 @Override 66 public int readInt() { 67 return mByteBuffer.getInt(); 68 } 69 70 @Override 71 public void readIntArray(int[] dst, int dstOffset, int intCount) { 72 while (--intCount >= 0) { 73 dst[dstOffset++] = mByteBuffer.getInt(); 74 } 75 } 76 77 @Override 78 public short readShort() { 79 return mByteBuffer.getShort(); 80 } 81} 82