Path: blob/master/src/java.base/share/classes/jdk/internal/jimage/ImageStream.java
41159 views
/*1* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.jimage;2627import java.nio.ByteBuffer;28import java.nio.ByteOrder;29import java.util.Arrays;30import java.util.Objects;3132/**33* @implNote This class needs to maintain JDK 8 source compatibility.34*35* It is used internally in the JDK to implement jimage/jrtfs access,36* but also compiled and delivered as part of the jrtfs.jar to support access37* to the jimage file provided by the shipped JDK by tools running on JDK 8.38*/39public class ImageStream {40private ByteBuffer buffer;4142public ImageStream() {43this(1024, ByteOrder.nativeOrder());44}4546public ImageStream(int size) {47this(size, ByteOrder.nativeOrder());48}4950public ImageStream(byte[] bytes) {51this(bytes, ByteOrder.nativeOrder());52}5354public ImageStream(ByteOrder byteOrder) {55this(1024, byteOrder);56}5758public ImageStream(int size, ByteOrder byteOrder) {59buffer = ByteBuffer.allocate(size);60buffer.order(Objects.requireNonNull(byteOrder));61}6263public ImageStream(byte[] bytes, ByteOrder byteOrder) {64buffer = ByteBuffer.wrap(Objects.requireNonNull(bytes));65buffer.order(Objects.requireNonNull(byteOrder));66}6768public ImageStream(ByteBuffer buffer) {69this.buffer = Objects.requireNonNull(buffer);70}7172public ImageStream align(int alignment) {73int padding = (getSize() - 1) & ((1 << alignment) - 1);7475for (int i = 0; i < padding; i++) {76put((byte)0);77}7879return this;80}8182public void ensure(int needs) {83if (needs < 0) {84throw new IndexOutOfBoundsException("Bad value: " + needs);85}8687if (needs > buffer.remaining()) {88byte[] bytes = buffer.array();89ByteOrder byteOrder = buffer.order();90int position = buffer.position();91int newSize = needs <= bytes.length ? bytes.length << 1 : position + needs;92buffer = ByteBuffer.allocate(newSize);93buffer.order(byteOrder);94buffer.put(bytes, 0, position);95}96}9798public boolean hasByte() {99return buffer.remaining() != 0;100}101102public boolean hasBytes(int needs) {103return needs <= buffer.remaining();104}105106public void skip(int n) {107if (n < 0) {108throw new IndexOutOfBoundsException("skip value = " + n);109}110111buffer.position(buffer.position() + n);112}113114public int get() {115return buffer.get() & 0xFF;116}117118public void get(byte bytes[], int offset, int size) {119buffer.get(bytes, offset, size);120}121122public int getShort() {123return buffer.getShort();124}125126public int getInt() {127return buffer.getInt();128}129130public long getLong() {131return buffer.getLong();132}133134public ImageStream put(byte byt) {135ensure(1);136buffer.put(byt);137138return this;139}140141public ImageStream put(int byt) {142return put((byte)byt);143}144145public ImageStream put(byte bytes[], int offset, int size) {146ensure(size);147buffer.put(bytes, offset, size);148149return this;150}151152public ImageStream put(ImageStream stream) {153put(stream.buffer.array(), 0, stream.buffer.position());154155return this;156}157158public ImageStream putShort(short value) {159ensure(2);160buffer.putShort(value);161162return this;163}164165public ImageStream putShort(int value) {166return putShort((short)value);167}168169public ImageStream putInt(int value) {170ensure(4);171buffer.putInt(value);172173return this;174}175176public ImageStream putLong(long value) {177ensure(8);178buffer.putLong(value);179180return this;181}182183public ByteBuffer getBuffer() {184return buffer;185}186187public int getPosition() {188return buffer.position();189}190191public int getSize() {192return buffer.position();193}194195public byte[] getBytes() {196return buffer.array();197}198199public void setPosition(int offset) {200buffer.position(offset);201}202203public byte[] toArray() {204return Arrays.copyOf(buffer.array(), buffer.position());205}206}207208209