Fully refactored Buffer class from 194
Code:
/* Class35_Sub2_Sub3 - Decompiled by JODE
* Visit http://jode.sourceforge.net/
*/
import sign.signlink;
import java.math.BigInteger;
public final class Buffer extends DoublyLinkedNodeChild {
public byte[] payload;
public int offset;
public int bitOffset;
private static int[] crcTable = new int[256];
private static final int[] bitMasks;
public IsaacCipher outgoingCipher;
private static int availableMode0Buffers;
private static int availableMode1Buffers;
private static int availableMode2Buffers;
private static DoublyLinkedList mode0Pool;
private static DoublyLinkedList mode1Pool;
private static DoublyLinkedList mode2Pool;
public static Buffer newPooledBuffer(int sizeMode) {
synchronized (mode1Pool) {
Buffer buffer = null;
if (sizeMode == 0 && availableMode0Buffers > 0) {
availableMode0Buffers--;
buffer = (Buffer) mode0Pool.popHead();
} else if (sizeMode == 1 && availableMode1Buffers > 0) {
availableMode1Buffers--;
buffer = (Buffer) mode1Pool.popHead();
} else if (sizeMode == 2 && availableMode2Buffers > 0) {
availableMode2Buffers--;
buffer = (Buffer) mode2Pool.popHead();
}
if (buffer != null) {
buffer.offset = 0;
return buffer;
}
}
Buffer buffer = new Buffer();
buffer.offset = 0;
if (sizeMode == 0) {
buffer.payload = new byte[100];
} else if (sizeMode == 1) {
buffer.payload = new byte[5000];
} else {
buffer.payload = new byte[30000];
}
return buffer;
}
public void release() {
synchronized (mode1Pool) {
offset = 0;
if (payload.length == 100 && availableMode0Buffers < 1000) {
mode0Pool.insertHead(this);
availableMode0Buffers++;
return;
}
if (payload.length == 5000 && availableMode1Buffers < 250) {
mode1Pool.insertHead(this);
availableMode1Buffers++;
return;
}
if (payload.length == 30000 && availableMode2Buffers < 50) {
mode2Pool.insertHead(this);
availableMode2Buffers++;
}
}
}
private Buffer() {
}
public Buffer(byte[] payload) {
this.payload = payload;
offset = 0;
}
public void writeCipheredByte(int i) {
payload[offset++] = (byte) (i + outgoingCipher.next());
}
public void writeByte(int i) {
payload[offset++] = (byte) i;
}
public void writeShort(int i) {
payload[offset++] = (byte) (i >> 8);
payload[offset++] = (byte) i;
}
public void write24Bit(int i) {
payload[offset++] = (byte) (i >> 16);
payload[offset++] = (byte) (i >> 8);
payload[offset++] = (byte) i;
}
public void writeInt(int i) {
payload[offset++] = (byte) (i >> 24);
payload[offset++] = (byte) (i >> 16);
payload[offset++] = (byte) (i >> 8);
payload[offset++] = (byte) i;
}
public void writeLong(long l) {
try {
payload[offset++] = (byte) (int) (l >> 56);
payload[offset++] = (byte) (int) (l >> 48);
payload[offset++] = (byte) (int) (l >> 40);
payload[offset++] = (byte) (int) (l >> 32);
payload[offset++] = (byte) (int) (l >> 24);
payload[offset++] = (byte) (int) (l >> 16);
payload[offset++] = (byte) (int) (l >> 8);
payload[offset++] = (byte) (int) l;
} catch (RuntimeException runtimeexception) {
signlink.reporterror(
"94285, " + l + " "
+ runtimeexception.toString());
throw new RuntimeException();
}
}
public void writeString(String string) {
string.getBytes(0, string.length(), payload, offset);
offset += string.length();
payload[offset++] = (byte) 10;
}
public void write(byte[] is, int off, int len) {
for (int ptr = off; ptr < off + len; ptr++) {
payload[offset++] = is[ptr];
}
}
public void writeVarByteFrameSize(int i) {
payload[offset - i - 1] = (byte) i;
}
public int readUByte() {
return payload[offset++] & 0xff;
}
public byte readByte() {
return payload[offset++];
}
public int readShort() {
offset += 2;
return (((payload[offset - 2] & 0xff) << 8)
+ (payload[offset - 1] & 0xff));
}
public int readForceSignedShort() {
offset += 2;
int val = (((payload[offset - 2] & 0xff) << 8)
+ (payload[offset - 1] & 0xff));
if (val > 32767) {
val -= 65536;
}
return val;
}
public int read24Bit() {
offset += 3;
return (((payload[offset - 3] & 0xff) << 16)
+ ((payload[offset - 2] & 0xff) << 8)
+ (payload[offset - 1] & 0xff));
}
public int readInt() {
offset += 4;
return (((payload[offset - 4] & 0xff) << 24)
+ ((payload[offset - 3] & 0xff) << 16)
+ ((payload[offset - 2] & 0xff) << 8)
+ (payload[offset - 1] & 0xff));
}
public long readLong() {
long highdd = (long) readInt() & 0xffffffffL;
long lowdd = (long) readInt() & 0xffffffffL;
return (highdd << 32) + lowdd;
}
public String readString() {
int i = offset;
while (payload[offset++] != 10) {/* empty */}
return new String(payload, i, offset - i - 1);
}
public byte[] readStringAsBytes() {
int oldOff = offset;
while (payload[offset++] != 10) {/* empty */}
byte[] strBytes = new byte[offset - oldOff - 1];
for (int payloadPtr = oldOff; payloadPtr < offset - 1; payloadPtr++) {
strBytes[payloadPtr - oldOff] = payload[payloadPtr];
}
return strBytes;
}
public void read(byte[] buf, int len, int off) {
for (int bufPtr = off; bufPtr < off + len; bufPtr++) {
buf[bufPtr] = payload[offset++];
}
}
public void convertToBitAccess(int i) {
bitOffset = offset * 8;
}
public int readBits(int bitCount) {
int i_14 = bitOffset >> 3;
int bitsToRead = 8 - (bitOffset & 0x7);
int val = 0;
bitOffset += bitCount;
for (/**/; bitCount > bitsToRead; bitsToRead = 8) {
val += (payload[i_14++] & bitMasks[bitsToRead]) << bitCount - bitsToRead;
bitCount -= bitsToRead;
}
if (bitCount == bitsToRead) {
val += payload[i_14] & bitMasks[bitsToRead];
} else {
val += payload[i_14] >> bitsToRead - bitCount & bitMasks[bitCount];
}
return val;
}
public void convertToByteAccess() {
offset = (bitOffset + 7) / 8;
}
public int readSmartA() {
int i = payload[offset] & 0xff;
if (i < 128) {
return readUByte() - 64;
}
return readShort() - 49152;
}
public int readSmartB() {
int i = payload[offset] & 0xff;
if (i < 128) {
return readUByte();
}
return readShort() - 32768;
}
public void applyRsa(BigInteger exponent, BigInteger modulus
) {
int oldSize = offset;
offset = 0;
byte[] decodedBuffer = new byte[oldSize];
read(decodedBuffer, oldSize, 0);
BigInteger decodedBigInt = new BigInteger(decodedBuffer);
BigInteger encodedBigInt = decodedBigInt.modPow(exponent,
modulus);
byte[] encodedBuffer = encodedBigInt.toByteArray();
offset = 0;
writeByte(encodedBuffer.length);
write(encodedBuffer, 0, encodedBuffer.length);
}
static {
for (int tablePtr = 0; tablePtr < 256; tablePtr++) {
int generatedCrc = tablePtr;
for (int i_23 = 0; i_23 < 8; i_23++) {
if ((generatedCrc & 0x1) == 1) {
generatedCrc = generatedCrc >>> 1 ^ ~0x12477cdf;
} else {
generatedCrc >>>= 1;
}
}
crcTable[tablePtr] = generatedCrc;
}
bitMasks = new int[]{
0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,
2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287,
1048575, 2097151, 4194303, 8388607, 16777215, 33554431, 67108863,
134217727, 268435455, 536870911, 1073741823, 2147483647, -1};
mode0Pool = new DoublyLinkedList(5);
mode1Pool = new DoublyLinkedList(5);
mode2Pool = new DoublyLinkedList(5);
}
}