package com.sun.tools.javac.util;
import java.io.*;
@Version("%W% %E%")
public class ByteBuffer {
public byte[] elems;
public int length;
public ByteBuffer() {
this(64);
}
public ByteBuffer(int initialSize) {
elems = new byte[initialSize];
length = 0;
}
private void copy(int size) {
byte[] newelems = new byte[size];
System.arraycopy(elems, 0, newelems, 0, elems.length);
elems = newelems;
}
public void appendByte(int b) {
if (length >= elems.length) copy(elems.length * 2);
elems[length++] = (byte)b;
}
public void appendBytes(byte[] bs, int start, int len) {
while (length + len > elems.length) copy(elems.length * 2);
System.arraycopy(bs, start, elems, length, len);
length += len;
}
public void appendBytes(byte[] bs) {
appendBytes(bs, 0, bs.length);
}
public void appendChar(int x) {
while (length + 1 >= elems.length) copy(elems.length * 2);
elems[length ] = (byte)((x >> 8) & 0xFF);
elems[length+1] = (byte)((x ) & 0xFF);
length = length + 2;
}
public void appendInt(int x) {
while (length + 3 >= elems.length) copy(elems.length * 2);
elems[length ] = (byte)((x >> 24) & 0xFF);
elems[length+1] = (byte)((x >> 16) & 0xFF);
elems[length+2] = (byte)((x >> 8) & 0xFF);
elems[length+3] = (byte)((x ) & 0xFF);
length = length + 4;
}
public void appendLong(long x) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(8);
DataOutputStream bufout = new DataOutputStream(buffer);
try {
bufout.writeLong(x);
appendBytes(buffer.toByteArray(), 0, 8);
} catch (IOException e) {
throw new AssertionError("write");
}
}
public void appendFloat(float x) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(4);
DataOutputStream bufout = new DataOutputStream(buffer);
try {
bufout.writeFloat(x);
appendBytes(buffer.toByteArray(), 0, 4);
} catch (IOException e) {
throw new AssertionError("write");
}
}
public void appendDouble(double x) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(8);
DataOutputStream bufout = new DataOutputStream(buffer);
try {
bufout.writeDouble(x);
appendBytes(buffer.toByteArray(), 0, 8);
} catch (IOException e) {
throw new AssertionError("write");
}
}
public void appendName(Name name) {
appendBytes(name.table.names, name.index, name.len);
}
public void reset() {
length = 0;
}
public Name toName(Name.Table names) {
return names.fromUtf(elems, 0, length);
}
}