package com.thoughtworks.xstream.io.binary; import com.thoughtworks.xstream.io.StreamException; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; /* loaded from: classes.dex */ public abstract class Token { private static final byte ID_EIGHT_BYTES = 32; private static final byte ID_FOUR_BYTES = 24; private static final byte ID_MASK = 56; private static final byte ID_ONE_BYTE = 8; private static final String ID_SPLITTED = "\u0000‡\u0000"; private static final byte ID_TWO_BYTES = 16; private static final int MAX_UTF8_LENGTH = 65535; public static final byte TYPE_ATTRIBUTE = 5; public static final byte TYPE_END_NODE = 4; public static final byte TYPE_MAP_ID_TO_VALUE = 2; private static final byte TYPE_MASK = 7; public static final byte TYPE_START_NODE = 3; public static final byte TYPE_VALUE = 6; public static final byte TYPE_VERSION = 1; protected long id = -1; private final byte type; protected String value; public static class EndNode extends Token { public EndNode() { super((byte) 4); } @Override // com.thoughtworks.xstream.io.binary.Token public void readFrom(DataInput dataInput, byte b) { } @Override // com.thoughtworks.xstream.io.binary.Token public void writeTo(DataOutput dataOutput, byte b) { } } public static class Formatter { private Token contructToken(byte b) { if (b == 2) { return new MapIdToValue(); } if (b == 3) { return new StartNode(); } if (b == 4) { return new EndNode(); } if (b == 5) { return new Attribute(); } if (b == 6) { return new Value(); } throw new StreamException("Unknown token type"); } public Token read(DataInput dataInput) throws IOException { byte readByte = dataInput.readByte(); byte b = (byte) (readByte & Token.TYPE_MASK); byte b2 = (byte) (readByte & Token.ID_MASK); Token contructToken = contructToken(b); contructToken.readFrom(dataInput, b2); return contructToken; } public void write(DataOutput dataOutput, Token token) throws IOException { long id = token.getId(); byte b = id <= 255 ? Token.ID_ONE_BYTE : id <= 65535 ? Token.ID_TWO_BYTES : id <= 4294967295L ? Token.ID_FOUR_BYTES : Token.ID_EIGHT_BYTES; dataOutput.write(token.getType() + b); token.writeTo(dataOutput, b); } } public Token(byte b) { this.type = b; } public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } Token token = (Token) obj; if (this.id != token.id || this.type != token.type) { return false; } String str = this.value; String str2 = token.value; if (str != null) { if (str.equals(str2)) { return true; } } else if (str2 == null) { return true; } return false; } public long getId() { return this.id; } public byte getType() { return this.type; } public String getValue() { return this.value; } public int hashCode() { int i = this.type * 29; long j = this.id; int i2 = (i + ((int) (j ^ (j >>> 32)))) * 29; String str = this.value; return i2 + (str != null ? str.hashCode() : 0); } public abstract void readFrom(DataInput dataInput, byte b) throws IOException; protected long readId(DataInput dataInput, byte b) throws IOException { if (b == 8) { return dataInput.readByte() + 128; } if (b == 16) { return dataInput.readShort() - Short.MIN_VALUE; } if (b == 24) { return dataInput.readInt() - Integer.MIN_VALUE; } if (b == 32) { return dataInput.readLong() - Long.MIN_VALUE; } StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("Unknown idType "); stringBuffer.append((int) b); throw new Error(stringBuffer.toString()); } protected String readString(DataInput dataInput) throws IOException { String readUTF = dataInput.readUTF(); if (!ID_SPLITTED.equals(readUTF)) { return readUTF; } byte[] bArr = new byte[dataInput.readInt()]; dataInput.readFully(bArr); return new String(bArr, "utf-8"); } public String toString() { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(getClass().getName()); stringBuffer.append(" [id="); stringBuffer.append(this.id); stringBuffer.append(", value='"); stringBuffer.append(this.value); stringBuffer.append("']"); return stringBuffer.toString(); } protected void writeId(DataOutput dataOutput, long j, byte b) throws IOException { if (j < 0) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("id must not be negative "); stringBuffer.append(j); throw new IOException(stringBuffer.toString()); } if (b == 8) { dataOutput.writeByte(((byte) j) - 128); return; } if (b == 16) { dataOutput.writeShort(((short) j) - 32768); return; } if (b == 24) { dataOutput.writeInt(((int) j) - 2147483648); } else { if (b == 32) { dataOutput.writeLong(j - Long.MIN_VALUE); return; } StringBuffer stringBuffer2 = new StringBuffer(); stringBuffer2.append("Unknown idType "); stringBuffer2.append((int) b); throw new Error(stringBuffer2.toString()); } } protected void writeString(DataOutput dataOutput, String str) throws IOException { byte[] bytes = str.length() > 16383 ? str.getBytes("utf-8") : new byte[0]; if (bytes.length <= MAX_UTF8_LENGTH) { dataOutput.writeUTF(str); return; } dataOutput.writeUTF(ID_SPLITTED); dataOutput.writeInt(bytes.length); dataOutput.write(bytes); } public abstract void writeTo(DataOutput dataOutput, byte b) throws IOException; public static class StartNode extends Token { public StartNode(long j) { super((byte) 3); this.id = j; } @Override // com.thoughtworks.xstream.io.binary.Token public void readFrom(DataInput dataInput, byte b) throws IOException { this.id = readId(dataInput, b); } @Override // com.thoughtworks.xstream.io.binary.Token public void writeTo(DataOutput dataOutput, byte b) throws IOException { writeId(dataOutput, this.id, b); } public StartNode() { super((byte) 3); } } public static class Value extends Token { public Value(String str) { super((byte) 6); this.value = str; } @Override // com.thoughtworks.xstream.io.binary.Token public void readFrom(DataInput dataInput, byte b) throws IOException { this.value = readString(dataInput); } @Override // com.thoughtworks.xstream.io.binary.Token public void writeTo(DataOutput dataOutput, byte b) throws IOException { writeString(dataOutput, this.value); } public Value() { super((byte) 6); } } public static class Attribute extends Token { public Attribute(long j, String str) { super((byte) 5); this.id = j; this.value = str; } @Override // com.thoughtworks.xstream.io.binary.Token public void readFrom(DataInput dataInput, byte b) throws IOException { this.id = readId(dataInput, b); this.value = readString(dataInput); } @Override // com.thoughtworks.xstream.io.binary.Token public void writeTo(DataOutput dataOutput, byte b) throws IOException { writeId(dataOutput, this.id, b); writeString(dataOutput, this.value); } public Attribute() { super((byte) 5); } } public static class MapIdToValue extends Token { public MapIdToValue(long j, String str) { super((byte) 2); this.id = j; this.value = str; } @Override // com.thoughtworks.xstream.io.binary.Token public void readFrom(DataInput dataInput, byte b) throws IOException { this.id = readId(dataInput, b); this.value = readString(dataInput); } @Override // com.thoughtworks.xstream.io.binary.Token public void writeTo(DataOutput dataOutput, byte b) throws IOException { writeId(dataOutput, this.id, b); writeString(dataOutput, this.value); } public MapIdToValue() { super((byte) 2); } } }